首页 > 代码库 > JAVA 创建TXT文件,写入文件内容,读取文件内容

JAVA 创建TXT文件,写入文件内容,读取文件内容

  1 package com.abin.facade.ws.mail.function;
  2 
  3 import java.io.BufferedReader;
  4 import java.io.File;
  5 import java.io.FileOutputStream;
  6 import java.io.FileReader;
  7 import java.io.RandomAccessFile;
  8 
  9 public class FileOperation {
 10  
 11  /**
 12   * 创建文件
 13   * @param fileName
 14   * @return
 15   */
 16  public static boolean createFile(File fileName)throws Exception{
 17   boolean flag=false;
 18   try{
 19    if(!fileName.exists()){
 20     fileName.createNewFile();
 21     flag=true;
 22    }
 23   }catch(Exception e){
 24    e.printStackTrace();
 25   }
 26   return true;
 27  } 
 28  
 29  /**
 30   * 读TXT文件内容
 31   * @param fileName
 32   * @return
 33   */
 34  public static String readTxtFile(File fileName)throws Exception{
 35   String result=null;
 36   FileReader fileReader=null;
 37   BufferedReader bufferedReader=null;
 38   try{
 39    fileReader=new FileReader(fileName);
 40    bufferedReader=new BufferedReader(fileReader);
 41    try{
 42     String read=null;
 43     while((read=bufferedReader.readLine())!=null){
 44      result=result+read+"\r\n";
 45     }
 46    }catch(Exception e){
 47     e.printStackTrace();
 48    }
 49   }catch(Exception e){
 50    e.printStackTrace();
 51   }finally{
 52    if(bufferedReader!=null){
 53     bufferedReader.close();
 54    }
 55    if(fileReader!=null){
 56     fileReader.close();
 57    }
 58   }
 59   System.out.println("读取出来的文件内容是:"+"\r\n"+result);
 60   return result;
 61  }
 62  
 63  
 64  public static boolean writeTxtFile(String content,File  fileName)throws Exception{
 65   RandomAccessFile mm=null;
 66   boolean flag=false;
 67   FileOutputStream o=null;
 68   try {
 69    o = new FileOutputStream(fileName);
 70       o.write(content.getBytes("GBK"));
 71       o.close();
 72 //   mm=new RandomAccessFile(fileName,"rw");
 73 //   mm.writeBytes(content);
 74    flag=true;
 75   } catch (Exception e) {
 76    // TODO: handle exception
 77    e.printStackTrace();
 78   }finally{
 79    if(mm!=null){
 80     mm.close();
 81    }
 82   }
 83   return flag;
 84  }
 85 
 86 
 87 
 88 public static void contentToTxt(String filePath, String content) {
 89         String str = new String(); //原有txt内容
 90         String s1 = new String();//内容更新
 91         try {
 92             File f = new File(filePath);
 93             if (f.exists()) {
 94                 System.out.print("文件存在");
 95             } else {
 96                 System.out.print("文件不存在");
 97                 f.createNewFile();// 不存在则创建
 98             }
 99             BufferedReader input = new BufferedReader(new FileReader(f));
100 
101             while ((str = input.readLine()) != null) {
102                 s1 += str + "\n";
103             }
104             System.out.println(s1);
105             input.close();
106             s1 += content;
107 
108             BufferedWriter output = new BufferedWriter(new FileWriter(f));
109             output.write(s1);
110             output.close();
111         } catch (Exception e) {
112             e.printStackTrace();
113 
114         }
115     }
116 
117 }

 

JAVA 创建TXT文件,写入文件内容,读取文件内容