• Java中文件读写


     1.1 运用字节流对文件直接写入

     1 import java.io.File;
     2 import java.io.FileOutputStream;
     3 import java.io.IOException;
     4 
     5 
     6 public class FileDemo {
     7      static File file = new File("F:\test.txt");
     8          public static void main(String[] args) throws IOException {
     9             
    10              /*
    11               * 运用字节流对文件进行直接写入
    12               * 写入文件file不存在时,自动创建
    13               */
    14              FileOutputStream out = new FileOutputStream(file,true); //第二个参数表示不覆盖原文件,直接在文件后面追加内容
    15              String str = "test!
    ";    //写入的字符串,"
    "为换行
    16              out.write(str.getBytes());   //将字节数组写入文件流
    17              out.flush();                 //刷新输出流和强制任何缓冲流可写
    18              out.close();                  //关闭流           
    19          }
    20 }

      打开F盘根目录下的test.txt,可以看到文件写入成功!见下图:

      

      1.2 运用字节流对文件进行直接读取

     1 import java.io.File;
     2 import java.io.FileInputStream;
     3 import java.io.FileNotFoundException;
     4 import java.io.FileOutputStream;
     5 import java.io.IOException;
     6 
     7 
     8 public class FileDemo {
     9      static File file = new File("F:\test.txt");
    10      
    11      public static void main(String[] args)
    12      {
    13          /*
    14           * 利用字节流对文件直接读取
    15           */
    16          FileInputStream in = null;  //声明流对象
    17          try
    18          {
    19              in = new FileInputStream(file);
    20              byte[] data = new byte[1024];        //定义字节类型的数组,大小(1024)根据文件实际大小确定
    21              int count = 0;                        //初始化下标
    22              int n = in.read();                    //读取流中的第一个字节数据
    23              while(n != -1)                        //判断是否到达流的末尾
    24              {
    25                 data[count++] = (byte)n;        //有效数据存储,并增加下标
    26                 n = in.read();                    //读取下一个
    27              }
    28              String content = new String(data, 0, count);    //解析数据     
    29              System.out.println(content);
    30          }catch(Exception e)
    31          {
    32              e.printStackTrace();
    33          }
    34          finally
    35          {
    36              try {
    37                 in.close();
    38             } catch (IOException e) {
    39                 // TODO Auto-generated catch block
    40                 e.printStackTrace();
    41             }
    42          }     
    43      }
    44 }

      2.1 运用字符流对文件进行写入

     1 import java.io.File;
     2 import java.io.FileWriter;
     3 import java.io.IOException;
     4 
     5 
     6 public class ReadFile {
     7     /*
     8      * 字符流写入文件
     9      */
    10     static File file = new File("F:\test2.txt");
    11     public static void main(String[] args) throws IOException
    12     {
    13         //不存在该文件会自动创建
    14         FileWriter fw = new FileWriter(file, true);    //第二个参数表示不覆盖原文件,直接在文件后面追加内容
    15         fw.write("Test!
    ");
    16         fw.flush();
    17         fw.close();
    18     }
    19 
    20 }

      2.2 运用字符流对文件进行读取

     1 import java.io.File;
     2 import java.io.FileNotFoundException;
     3 import java.io.FileReader;
     4 import java.io.FileWriter;
     5 import java.io.IOException;
     6 
     7 
     8 public class ReadFile {
     9     
    10     static File file = new File("F:\test.txt");
    11     public static void main(String[] args) throws IOException
    12     {
    13         FileReader fr = new FileReader(file);
    14         int count = 0;
    15         String content = "";
    16         while((count = fr.read()) != -1)
    17         {
    18             content += (char)count;
    19         }
    20         System.out.println(content);
    21     }
    22 }
  • 相关阅读:
    Life Forms POJ
    Maximum repetition substring POJ
    Extend to Palindrome UVA
    Sequence POJ
    HDU-6705 path (bfs)
    632C. The Smallest String Concatenation(注意 :stl sort函数坑点--- coredump问题 )
    2018 ICPC青岛网络赛 B. Red Black Tree(倍增lca)
    基于哈希表实现页面置换算法
    解决xpath中文乱码
    解决oh-my-zsh中git分支显示乱码问题
  • 原文地址:https://www.cnblogs.com/xuepeng0521/p/3745322.html
Copyright © 2020-2023  润新知