java中如何把一段字符串写入文件中

就把下面的Str下到文件中,该怎么改呢?谢谢了

String str="i love china!"
File txt=new File("C:\Documents and Settings\Administrator\桌面\nihao.txt");
if(!txt.exists()){
txt.createNewFile();
}
byte bytes[]=new byte[512];
int b=Integer.parseInt(str);
FileOutputStream fos=new FileOutputStream(txt);
fos.write(bytes,0,b);
fos.close();

第1个回答  2015-09-21
使用Java中的File类,url为文件的绝对地址,str为输入的字符串内容。
代码如下图所示:

String str="i love china!"

File txt=new File("url");
if(!txt.exists()){
txt.createNewFile();
}
byte bytes[]=new byte[512];
bytes=str.getBytes(); //新加的
int b=str.length(); //改
FileOutputStream fos=new FileOutputStream(txt);
fos.write(bytes,0,b);
fos.close();
第2个回答  2009-12-25
String str="i love china!"
File txt=new File("C:\Documents and Settings\Administrator\桌面\nihao.txt");
if(!txt.exists()){
txt.createNewFile();
}
byte bytes[]=new byte[512];
bytes=str.getBytes(); //新加的
int b=str.length(); //改
FileOutputStream fos=new FileOutputStream(txt);
fos.write(bytes,0,b);
fos.close();本回答被提问者采纳
相似回答