Scripts 学盟
标题:
Java 写入文本文件
[打印本页]
作者:
Alvin
时间:
2011-4-8 11:29:40
标题:
Java 写入文本文件
在 Java 开发中,也常常有写入文本文件的需求
这里只是个参考代码
package org.iscripts.common.io;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
/**
* @author 林俊海(ialvin.cn) 广东·普宁·里湖
*/
public class TextUtils {
public static void appendToFile(String content, File file) throws IOException {
TextUtils.appendToFile(content, file, System.getProperty("file.encoding"));
}
public static void appendToFile(String content, File file, String encoding) throws IOException {
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true), encoding));
writer.write(content);
} finally {
if (writer != null)
writer.close();
}
}
public static void saveToFile(String content, File file) throws IOException {
TextUtils.saveToFile(content, file, System.getProperty("file.encoding"));
}
public static void saveToFile(String content, File file, String encoding) throws IOException {
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, false), encoding));
writer.write(content);
} finally {
if (writer != null)
writer.close();
}
}
}
复制代码
附:《Java 读取文本文件内容》-
http://www.iscripts.org/bbs/viewthread.php?tid=33
作者:
那个谁
时间:
2011-9-13 08:06:28
木有人顶下!!!!
欢迎光临 Scripts 学盟 (http://www.iscripts.org/)
Powered by Discuz! X2