- 注册时间
- 2011-3-21
- 最后登录
- 2022-3-22
- 在线时间
- 1191 小时
- 阅读权限
- 200
- 积分
- 9457
- 帖子
- 1256
- 精华
- 0
- UID
- 1
|
在 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 |
|