如题所述
javaåå ¥æ件å°æå®æ件夹çæ¹æ³ä¸»è¦æ两ç§ï¼å©ç¨PrintStreamåå©ç¨StringBuffer
ä¾å¦å°ææ¬âI'm the text to be writeâåå ¥å°æ件夹D:/testä¸ï¼å¹¶å½å为test.txtï¼å两ç§æ¹å¼ç®åå®ç°ä»£ç å¦ä¸ï¼
1. å©ç¨PrintStreamåæ件
public void PrintStreamDemo(){try {
FileOutputStream out=new FileOutputStream("D:/test.txt");
PrintStream p=new PrintStream(out);
p.println("I'm the text to be write");
} catch (FileNotFoundException e){
e.printStackTrace();
}
}
2. å©ç¨StringBufferåæ件
File file=new File("D:/test.txt");
if(!file.exists())
file.createNewFile();
FileOutputStream out=new FileOutputStream(file,true);
StringBuffer sb=new StringBuffer();
sb.append("I'm the text to be write");
out.write(sb.toString().getBytes("utf-8"));
}
out.close();
}
æ示ï¼å©ç¨StringBufferåæ件å¯ä»¥è®¾å®ä½¿ç¨ä½ç§ç¼ç ï¼ææ解å³ä¸æé®é¢ã
温馨提示:答案为网友推荐,仅供参考
第1个回答 推荐于2017-11-22
FileWriter 的write(String str)即可,注意如果你不想覆盖之前的内容,这个
FileWriter fw = new FileWriter(File file,boolean append),这里的append 应赋上true
而且因为字符流都是使用了缓冲区的,所以记得flush或close本回答被提问者采纳
FileWriter fw = new FileWriter(File file,boolean append),这里的append 应赋上true
而且因为字符流都是使用了缓冲区的,所以记得flush或close本回答被提问者采纳
第2个回答 2016-04-17
io流,好简单,你百度下就出来好多