您的位置:首页 > 编程语言 > Java开发

Java 将字符串换行不覆盖写入txt文件

2014-11-18 11:53 756 查看
Java的一些小常识应用

/**

* 讲字符串换行覆盖写入txt
* @param string
*/
public static void writeTxt(String string){
byte[] buff = new byte[] {};
FileOutputStream fos = null;
String filePath = "C:\\微博URL.txt";
String str = string+System.getProperty("line.separator");
File file = new File(filePath);
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
buff = str.getBytes();
fos = new FileOutputStream(file, true);
fos.write(buff);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: