java怎么将字符串写入到文件

如题所述

使用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();
温馨提示:答案为网友推荐,仅供参考
相似回答