Java文件读写操作

来源:互联网 发布:免费的一级域名 编辑:程序博客网 时间:2024/06/07 09:15

代码

  1. package fileOperation;
  2.     
  3. import java.io.BufferedReader;
  4. import java.io.BufferedWriter;
  5. import java.io.File;
  6. import java.io.FileOutputStream;
  7. import java.io.FileReader;
  8. import java.io.FileWriter;
  9. import java.io.IOException;
  10. import java.io.OutputStreamWriter;
  11.     
  12. public class FileOperation {
  13.     
  14.     public static void main(String[] args) {
  15.         String filePath = "D:/test/test.txt";
  16.         String content = "第一行\n第二行\n";
  17.         String content2 = "第三行\n第四行\n";
  18.         createFile(filePath);
  19.         writeFile(filePath, content);
  20.         writeFile(filePath, content2);
  21.         readFile(filePath);
  22.     }
  23.     
  24.     /*
  25.     *按行读取文件
  26.     */
  27.     public static void readFile(String filePath) {
  28.         File file = new File(filePath);
  29.         BufferedReader reader = null;
  30.         try {
  31.             reader = new BufferedReader(new FileReader(file));
  32.             String tempString = null;
  33.             while ((tempString = reader.readLine()) != null) {
  34.                 System.out.println(tempString);
  35.             }
  36.             reader.close();
  37.         } catch (IOException e) {
  38.             e.printStackTrace();
  39.         } finally {
  40.             if (reader != null) {
  41.                 try {
  42.                     reader.close();
  43.                 } catch (IOException e1) {
  44.                 }
  45.             }
  46.         }
  47.     }
  48.     
  49.     /*
  50.     *以追加方式写文件
  51.     */
  52.     public static void writeFile(String filePath, String conent) {
  53.         BufferedWriter out = null;
  54.         try {
  55.             out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath, true)));
  56.             out.write(conent);
  57.         } catch (Exception e) {
  58.             e.printStackTrace();
  59.         } finally {
  60.             try {
  61.                 if (out != null) {
  62.                     out.close();
  63.                 }
  64.             } catch (IOException e) {
  65.                 e.printStackTrace();
  66.             }
  67.         }
  68.     }
  69.     
  70.     /*
  71.     *创建文件
  72.     */
  73.     public static void createFile(String filePath) {
  74.         File file = new File(filePath);
  75.         if (file.exists()) {
  76.             //System.out.println("文件已存在");
  77.         } else {
  78.             try {
  79.                 File fileParent = file.getParentFile();
  80.                 if (fileParent != null) {
  81.                     if (!fileParent.exists()) {
  82.                         fileParent.mkdirs();
  83.                     }
  84.                 }
  85.                 file.createNewFile();
  86.             } catch (IOException e) {
  87.                 e.printStackTrace();
  88.             }
  89.         }
  90.     }
  91.     
  92.     /*
  93.     *以追加方式写文件,效率低
  94.     */
  95.     public static void writeFileByFileWriter(String filePath, String content) {
  96.         FileWriter writer = null;
  97.         try {
  98.             writer = new FileWriter(new File(filePath), true);
  99.             writer.write(content);
  100.         } catch (IOException e) {
  101.             e.printStackTrace();
  102.         } finally {
  103.             try {
  104.                 if (writer != null) {
  105.                     writer.close();
  106.                 }
  107.             } catch (IOException e) {
  108.                 e.printStackTrace();
  109.             }
  110.         }
  111.     }
  112.     
  113. }

    结果

    1. 第一行
    2. 第二行
    3. 第三行
    4. 第四行
      原创粉丝点击