File file = new File("fileName");
并不是创建了一个文件。而是打开了一个内存到文件的句柄。
可以理解为 java程序和本地磁盘上的这个文件连接了。
之后可以对这个文件做操作。
如果你要创建一个文件。并且写入内容。
必须用输出流。
比如你的xml是一个String .叫xmlstr
String xmlstr="aaa";
File file = new File("d:\\fileName.doc");
byte []strtemp =xmlstr.getBytes();
//来源你字符串的输入流
InputStream in =new ByteArrayInputStream(strtemp);
OutputStream out =null;
try {
//到你文件的输出流
out= new FileOutputStream(file );
byte[] temp =new byte[1024];
//读输入,写到输出
while(in.read(temp)!=-1){
out.write(temp);
}
out.flush();
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
if(in!=null){
in.close();
}
if(out!=null){
out.close();
}
}
catch (IOException e) {
}
}
通过流的方式把文件写到你要的路径下才算创建了一个文件。
File file = new File("fileName");
fileName必须是路径加文件名称吧,如果不写,有可能创建文件到一个没有权限的目录,导致创建不成功