当前位置: 代码迷 >> Java相关 >> [求助],创建文件的问题
  详细解决方案

[求助],创建文件的问题

热度:406   发布时间:2004-08-06 20:52:00.0
[求助],创建文件的问题
用什么命令可以在硬盘上创建一个文件呀?
搜索更多相关的解决方案: 文件  

----------------解决方案--------------------------------------------------------
我拷不会吧
----------------解决方案--------------------------------------------------------

File file = new File("path and file name you want to save");

FileOutputStream fos = new FileOutputStream(file); fos.write(//byte you want to write); fos.close();

或者用FileWriter如果写入的是char流,方便


----------------解决方案--------------------------------------------------------

what i will do is ask the user to specify a path which they want to create the file. The program can be written in a few ways, i.e. using different class, they are gernally three of them, FileWriter, BufferedWriter and PrintWriter, in here i will use PrintWriter, because this is the easiest way.

public class Write{

public static void main(String[] args) throws IOException{

//to use the PrintWriter class which must pass in a FileWriter object as parameter

PrinteWriter pr = new PrintWriter(new FileWriter(agrs[0])); /*args[0] is the first file name specified by the user in the command line, if the file name doesnt exist, it will create a new one*/

pr.println("Whatever you wanna type.");

pr.print("Pretty much the same as using System.out.");

pr.close(); // this is very important

}

}


----------------解决方案--------------------------------------------------------
  相关解决方案