Java写入文本文件内容_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > Java写入文本文件内容

Java写入文本文件内容

 2012/1/5 14:00:04  zuobiaoyu  http://zuobiaoyu.iteye.com  我要评论(0)
  • 摘要:importjava.io.BufferedWriter;importjava.io.File;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.OutputStreamWriter;/***@author*/publicclassTextUtils{publicstaticvoidappendToFile(Stringcontent,Filefile)throwsIOException
  • 标签:文件 Java 内容

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

/**
* @author
*/
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();
        }
    }
}
发表评论
用户名: 匿名