fileutil.java

来自「这是孙卫琴在"Java与面向对象编程"一书的源代码。」· Java 代码 · 共 71 行

JAVA
71
字号
import java.io.*;
public class FileUtil {
  /** 从一个文件中逐行读取字符串,采用本地平台的字符编码 */
  public void readFile(String fileName)throws IOException{
    readFile(fileName,null);
  }

/** 从一个文件中逐行读取字符串,参数charsetName指定文件的字符编码 */
  public void readFile(String fileName, String charsetName)throws IOException{
    InputStream in=new FileInputStream(fileName);
    InputStreamReader reader;
    if(charsetName==null)
      reader=new InputStreamReader(in);
    else
      reader=new InputStreamReader(in,charsetName);
    BufferedReader br=new BufferedReader(reader);
    String data;
    while((data=br.readLine())!=null)  //逐行读取数据
      System.out.println(data);

    br.close();
 }

 /** 把一个文件中的字符内容拷贝到另一个文件中,并且进行了相关的字符编码转换 */
 public void copyFile(String from, String charsetFrom,String to,String charsetTo)throws IOException{
    InputStream in=new FileInputStream(from);
    InputStreamReader reader;
    if(charsetFrom==null)
      reader=new InputStreamReader(in);
    else
      reader=new InputStreamReader(in,charsetFrom);
    BufferedReader br=new BufferedReader(reader);

    OutputStream out=new FileOutputStream(to);
    OutputStreamWriter writer=new OutputStreamWriter(out,charsetTo);
    BufferedWriter bw=new BufferedWriter(writer);
    PrintWriter pw=new PrintWriter(bw,true);

    String data;
    while((data=br.readLine())!=null)
      pw.println(data);  //向目标文件逐行写数据

    br.close();
    pw.close();
  }

  public static void main(String args[])throws IOException{
     FileUtil util=new FileUtil ();
     
//按照本地平台的字符编码读取字符
     util.readFile("D:\\test.txt");
     
//把test.txt文件中的字符内容拷贝到out.txt中,out.txt采用UTF-8编码
     util.copyFile("D:\\test.txt",null,"D:\\out.txt","UTF-8");
     
//按照本地平台的字符编码读取字符,读到错误的数据
     util.readFile("D:\\out.txt");  
     
//按照UTF-8字符编码读取字符
     util.readFile("D:\\out.txt","UTF-8");
  }
}



/****************************************************
 * 作者:孙卫琴                                     *
 * 来源:<<Java面向对象编程>>                       *
 * 技术支持网址:www.javathinker.org                *
 ***************************************************/

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?