java读、写、查找替换txt文件,解决乱码。

来源:互联网 发布:数据库系统的核心是 编辑:程序博客网 时间:2024/06/12 00:34

    写此博文,作为自己的备忘,也供其它人参考。有不足之处,请指出。

     java读、写、查找替换txt文件,解决中文乱码。代码如下:

 

import java.io.*;
/**
 * 创建TXT文件并进行读、写、修改操作。
 * @author zxcq
 *
 */
public class ReadWriteFile {
 
 public static BufferedReader bufread;
 //指定文件路径和名称
 private static String path = "D:/suncity.txt";
 private static File filename = new File(path);
 private static String readStr="";
 
 /**
  * 创建文本文件
  */
 public static void creatTxtFile()throws IOException{
  if(!filename.exists()){
   filename.createNewFile();
   System.err.println(filename+"已经创建!");
  }
 }
 
 /**
  * 读取文本文件
  */
 public static String readTxtFile(){
  String read;
  try{
   FileInputStream fis = new FileInputStream(filename);
   InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
   BufferedReader br = new BufferedReader(isr);
   
   try{
    while((read=br.readLine())!= null){
     readStr = readStr + read+"\r\n";
    }
   }catch(IOException e){
    e.printStackTrace();
   }
  }catch(FileNotFoundException e){
   e.printStackTrace();
  }catch(IOException e){
   e.printStackTrace();
  }
  System.out.println("文件内容是:"+"\r\n"+readStr);
  return readStr;
 }
 
 /**
  * 写文件
  */
 public static void writeTxtFile(String newStr)throws IOException{
  //先读取原有文件内容,然后进行写入操作
  String filein = newStr + "\r\n"+readStr+"\r\n";
  try{
   OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(filename),"UTF-8");
   BufferedWriter writer = new BufferedWriter(write);
   writer.write(filein);
   writer.close();
  }catch(Exception e){
   System.out.println("写文件操作出错!");
      e.printStackTrace();
  }
 }
 
 /**
  * 将文件中指定内容的第一行替换为其它内容。
  * @param oldStr
  *                 查找内容
  * @param replaceStr 
  *                 替换内容
  */
 public static void replaceTxtByStr(String oldStr,String replaceStr){
  String temp = "";
  try{
//   File file = new File(path);
   FileInputStream fis = new FileInputStream(filename);
   InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
   BufferedReader br = new BufferedReader(isr);
   StringBuffer buf = new StringBuffer();
   
   //保存该行前面的内容
   for (int j = 1;(temp = br.readLine())!=null&&!temp.equals(oldStr);j++){
    buf=buf.append(temp);
    buf = buf.append(System.getProperty("line.separator"));
   }
   //将内容插入
   buf = buf.append(replaceStr);
   
   //保存该行后面的内容
   while((temp=br.readLine())!= null){
    buf = buf.append(System.getProperty("line.separator"));
    buf = buf.append(temp);
   }
   
   br.close();
   FileOutputStream fos = new FileOutputStream(filename);
//   PrintWriter pw = new PrintWriter(fos);
//   pw.write(buf.toString().toCharArray());
//   pw.flush();
//   pw.close();
   
   OutputStreamWriter write = new OutputStreamWriter(fos,"UTF-8");
   BufferedWriter writer = new BufferedWriter(write);
   writer.write(buf.toString());
   writer.close();
   
  }catch(IOException e){
   e.printStackTrace();
  }
 }
 
 /**
  * main 方法测试
  * @param s
  * @throws IOException
  */
 public static void main(String[] s)throws IOException{
  ReadWriteFile.creatTxtFile();
  ReadWriteFile.readTxtFile();
//  ReadWriteFile.writeTxtFile("2008我aa");
//  ReadWriteFile.writeTxtFile("我");
//  ReadWriteFile.replaceTxtByStr("我","他");
  
 }

}

原创粉丝点击