zoukankan      html  css  js  c++  java
  • Java 文件IO

    文件IO

    Java IO
        IO流用来处理设备之间的数据传输 Java对数据的操作是通过流的方式 Java用于操作流的对象都在IO包中
        按操作数据分为 字节流和字符流
            字符流的由来:
            其实就是:字节流读取文字字节数据后,不直接操作而是先查指定的编码表。获取对应的文字。
            再对这个文字进行操作。简单说:字节流+编码表
        按流向分为 输入流和输出流
            输入流和输出流相对于内存设备而言
            将外设中的数据读取到内存中:输入
            将内存的数写入到外设中:输出。

    字节流的两个顶层父类:
    1,InputStream  2,OutputStream

    字符流的两个顶层父类:
    1,Reader 2,Writer

    Demo1 将一些文字存储到硬盘一个文件中

    public class FileWriterDemo {
    
      private static final String LINE_SEPARATOR = System.getProperty("line.separator");
    
      public static void main(String[] args) throws IOException {
    
        // 创建一个可以往文件中写入字符数据的字符输出流对象。
        /*
         * 既然是往一个文件中写入文字数据,那么在创建对象时,就必须明确该文件(用于存储数据的目的地)。
         * 如果文件不存在,则会自动创建。 如果文件存在,则会被覆盖。
         * 如果构造函数中加入true,可以实现对文件进行续写!
         */
        FileWriter fw = new FileWriter("demo.txt", true);
    
        /*
         * 调用Writer对象中的write(string)方法,写入数据。
         * 其实数据写入到临时存储缓冲区中。
         */
        fw.write("abcde" + LINE_SEPARATOR + "hahaha");
        // fw.write("xixi");
    
        // fw.flush(); 进行刷新,将数据直接写到目的地中。
    
        fw.close(); //关闭流,关闭资源。在关闭前会先调用flush刷新缓冲中的数据到目的地。
    
        // fw.write("haha");// java.io.IOException: Stream closed
    
      }
    
    }

    Demo2 读取一个文本文件。将读取到的字符打印到控制台

    public class FileReaderDemo {
      public static void main(String[] args) throws IOException {
    
        // 1,创建读取字符数据的流对象。
        //在创建读取流对象时,必须要明确被读取的文件。一定要确定该文件是存在的。
        //用一个读取流关联一个已存在文件。
        FileReader fr = new FileReader("F:\demo.txt");
    
        int ch = 0;
    
        while ((ch = fr.read()) != -1) {
          System.out.println((char) ch);
        }
    
        /*
         * //用Reader中的read方法读取字符。 int ch = fr.read(); System.out.println((char)ch);
         * int ch1 = fr.read(); System.out.println(ch1); int ch2 = fr.read();
         * System.out.println(ch2);
         */
    
        fr.close();
      }
    }

    Demo3 读取一个文本文件 采用字符数组的方法

    public class FileReaderDemo2 {
      public static void main(String[] args) throws IOException {
    
        FileReader fr = new FileReader("F:\demo.txt");
        
        //使用read(char[])读取文本文件数据。先创建字符数组。
        char[] buf = new char[1024];
        
        int len = 0;
        
        while((len=fr.read(buf))!=-1){
          System.out.println(new String(buf,0,len));
        }
        
        /*
        int num = fr.read(buf);//将读取到的字符存储到数组中。
        System.out.println(num+":"+new String(buf,0,num));
        int num1 = fr.read(buf);//将读取到的字符存储到数组中。
        System.out.println(num1+":"+new String(buf,0,num1));
        int num2 = fr.read(buf);//将读取到的字符存储到数组中。
        System.out.println(num2+":"+new String(buf));
        */
        
        fr.close();
      }
    }

    Demo4 文本文件复制

    public class CopyTextTest {
      public static void main(String[] args) throws IOException {
    
        // 1,读取一个已有的文本文件,使用字符读取流和文件相关联。
        FileReader fr = new FileReader("F:\demo.txt");
        // 2,创建一个目的,用于存储读到数据。
        FileWriter fw = new FileWriter("F:\copydemo.txt");
        // 3,频繁的读写操作。
        int ch = 0;
        while ((ch = fr.read()) != -1) {
          fw.write(ch);
        }
        // 4,关闭流资源。
    
        fw.close();
        fr.close();
      }
    }

    Demo5 文本文件复制加缓存

    public class CopyTextTest_2 {
      private static final int BUFFER_SIZE = 1024;
    
      public static void main(String[] args) {
    
        FileReader fr = null;
        FileWriter fw = null;
        try {
          fr = new FileReader("F:\demo.txt");
          fw = new FileWriter("F:\democopy2.txt");
    
          // 创建一个临时容器,用于缓存读取到的字符。
          char[] buf = new char[BUFFER_SIZE];// 这就是缓冲区。
    
          // 定义一个变量记录读取到的字符数,(其实就是往数组里装的字符个数 )
          int len = 0;
    
          while ((len = fr.read(buf)) != -1) {
            fw.write(buf, 0, len);
          }
    
        } catch (Exception e) {
          // System.out.println("读写失败");
          throw new RuntimeException("读写失败");
        } finally {
          if (fw != null)
            try {
              fw.close();
            } catch (IOException e) {
    
              e.printStackTrace();
            }
          if (fr != null)
            try {
              fr.close();
            } catch (IOException e) {
    
              e.printStackTrace();
            }
        }
      }
    }

    字符流的缓存区
        缓存区的出现提高了对数据的读写效率
        对应类 BufferedReader BufferedWriter
        缓存区要结合流才可以使用 在流的基础上对流的功能进行了增强

    Demo6 缓存写

    public class BufferedWriterDemo {
      private static final String LINE_SEPARATOR = System.getProperty("line.separator");
    
      public static void main(String[] args) throws IOException {
    
        FileWriter fw = new FileWriter("F:\buf.txt");
    
        // 为了提高写入的效率。使用了字符流的缓冲区。
        // 创建了一个字符写入流的缓冲区对象,并和指定要被缓冲的流对象相关联
        BufferedWriter bufw = new BufferedWriter(fw);
    
        // 使用缓冲区的写入方法将数据先写入到缓冲区中。
        // bufw.write("abcdefq"+LINE_SEPARATOR+"hahahha");
        // bufw.write("xixiixii");
        // bufw.newLine();
        // bufw.write("heheheheh");
    
        for (int x = 1; x <= 4; x++) {
          bufw.write("abcdef" + x);
          bufw.newLine();
          bufw.flush();
        }
    
        // 使用缓冲区的刷新方法将数据刷目的地中。
        // bufw.flush();
    
        // 关闭缓冲区。其实关闭的就是被缓冲的流对象。
        bufw.close();
    
        // fw.write("hehe");
    
        // fw.close();
      }
    }

    Demo7 缓存读

    public class BufferedReaderDemo {
      public static void main(String[] args) throws IOException {
    
        FileReader fr = new FileReader("F:\buf.txt");
    
        BufferedReader bufr = new BufferedReader(fr);
    
        String line = null;
    
        while ((line = bufr.readLine()) != null) {
          System.out.println(line);
        }
        /*
         * String line1 = bufr.readLine(); System.out.println(line1); String line2 =
         * bufr.readLine(); System.out.println(line2); String line3 =
         * bufr.readLine(); System.out.println(line3); String line4 =
         * bufr.readLine(); System.out.println(line4); String line5 =
         * bufr.readLine(); System.out.println(line5);
         */
    
        bufr.close();
    
      }
    
    }

    Demo8 缓存复制

    public class CopyTextByBufTest {
      public static void main(String[] args) throws IOException {
    
        FileReader fr = new FileReader("F:\buf.txt");
        BufferedReader bufr = new BufferedReader(fr);
    
        FileWriter fw = new FileWriter("F:\buf_copy.txt");
        BufferedWriter bufw = new BufferedWriter(fw);
    
        String line = null;
        while ((line = bufr.readLine()) != null) {
          bufw.write(line);
          bufw.newLine();
          bufw.flush();
        }
    
        /*
         * int ch = 0;
         * 
         * while((ch=bufr.read())!=-1){
         * 
         * bufw.write(ch); }
         */
        bufw.close();
        bufr.close();
      }
    
    }

    Demo9 自定义缓存区

    public class MyBufferedReader extends Reader {
      private Reader r;
    
      // 定义一个数组作为缓冲区。
      private char[] buf = new char[1024];
      // 定义一个指针用于操作这个数组中的元素。当操作到最后一个元素后,指针应该归零。
      private int pos = 0;
      // 定义一个计数器用于记录缓冲区中的数据个数。 当该数据减到0,就从源中继续获取数据到缓冲区中。
      private int count = 0;
    
      MyBufferedReader(Reader r) {
        this.r = r;
      }
    
      //该方法从缓冲区中一次取一个字符。
      public int myRead() throws IOException {
    
        if (count == 0) {
          count = r.read(buf);
          pos = 0;
        }
        if (count < 0)
          return -1;
    
        char ch = buf[pos++];
    
        count--;
    
        return ch;
    
      }
    
      public String myReadLine() throws IOException {
    
        StringBuilder sb = new StringBuilder();
    
        int ch = 0;
        while ((ch = myRead()) != -1) {
    
          if (ch == '
    ')
            continue;
          if (ch == '
    ')
            return sb.toString();
          // 将从缓冲区中读到的字符,存储到缓存行数据的缓冲区中。
          sb.append((char) ch);
    
        }
    
        if (sb.length() != 0)
          return sb.toString();
        return null;
      }
    
      public void myClose() throws IOException {
    
        r.close();
      }
    
      @Override
      public int read(char[] cbuf, int off, int len) throws IOException {
    
        return 0;
      }
    
      @Override
      public void close() throws IOException {
      }
    }
    public class MyBufferedReaderDemo {
      public static void main(String[] args) throws IOException {
    
        FileReader fr = new FileReader("F:\buf.txt");
    
        MyBufferedReader bufr = new MyBufferedReader(fr);
    
        String line = null;
    
        while ((line = bufr.myReadLine()) != null) {
          System.out.println(line);
        }
    
        bufr.myClose();
      }
    }

    装饰设计模式
        对一组对象的功能进行增强时,就可以使用该模式进行问题的解决。
        
    装饰和继承都能实现一样的特点:进行功能的扩展增强。

    有什么区别呢?

    首先有一个继承体系。
    Writer
        |--TextWriter:用于操作文本
        |--MediaWriter:用于操作媒体。
        
    想要对操作的动作进行效率的提高。
    按照面向对象,可以通过继承对具体的进行功能的扩展。
    效率提高需要加入缓冲技术。
        
    Writer
        |--TextWriter:用于操作文本
            |--BufferTextWriter:加入了缓冲技术的操作文本的对象。
        |--MediaWriter:用于操作媒体。
            |--BufferMediaWriter:

    但是这样做好像并不理想 如果这个体系进行功能扩展,又多了流对象。
    那么这个流要提高效率,是不是也要产生子类呢?是。这时就会发现只为提高功能,进行的继承,
    导致继承体系越来越臃肿。不够灵活。

    重新思考这个问题?
    既然加入的都是同一种技术--缓冲。
    前一种是让缓冲和具体的对象相结合。
    可不可以将缓冲进行单独的封装,哪个对象需要缓冲就将哪个对象和缓冲关联。

    class Buffer{
        Buffer(TextWriter w)
        {}
        
        Buffer(MediaWirter w)
        {
        
        }
    }
    class BufferWriter extends Writer{
        BufferWriter(Writer w)
        {
        }
    }

    Writer
        |--TextWriter:用于操作文本
        |--MediaWriter:用于操作媒体。
        |--BufferWriter:用于提高效率。
        
    装饰比继承灵活。

    特点:装饰类和被装饰类都必须所属同一个接口或者父类。

    Demo10 装饰设计模式例子

    public class PersonDemo {
    
        public static void main(String[] args) {
    
            Person p = new Person();
    //        p.chifan();
            
            NewPerson p1 = new NewPerson(p);
            p1.chifan();
            
            NewPerson2 p2 = new NewPerson2();
            p2.chifan();
        }
    
    }
    
    class Person{
        void chifan(){
            System.out.println("吃饭");
        }
    }
    //这个类的出现是为了增强Person而出现的。
    class NewPerson{
        private Person p ;
        NewPerson(Person p){
            this.p = p;
        }
        
        public void chifan(){
            System.out.println("开胃酒");
            p.chifan();
            System.out.println("甜点");
            
        }
    
    }
    
    class NewPerson2 extends Person{
        public void chifan(){
            System.out.println("开胃酒");
            super.chifan();
            System.out.println("甜点");
        }
    }

    Demo11 LineNumberReaderDemo

    public class LineNumberReaderDemo {
      public static void main(String[] args) throws IOException {
    
        FileReader fr = new FileReader("F:\demo.txt");
        LineNumberReader lnr = new LineNumberReader(fr);
        
        String line = null;
        lnr.setLineNumber(100);
        while((line=lnr.readLine())!=null){
          System.out.println(lnr.getLineNumber()+":"+line);
        }
        
        lnr.close();
      }
    }

    字节流
        基本操作与字符流类相同 但它不仅可以操作字符 还可以操作其它媒体文件

    Demo12 写文件

    public class ByteStreamDemo {
      public static void main(String[] args) throws IOException {
    
        // 1,创建字节输出流对象。用于操作文件.
        FileOutputStream fos = new FileOutputStream("F:\bytedemo.txt");
    
        // 2,写数据。直接写入到了目的地中。
        fos.write("abcdefg".getBytes());
    
        // fos.flush();
        fos.close();// 关闭资源动作要完成。
      }
    }

    Demo13 拷贝文件

    public class CopyMp3Test {
    
      public static void main(String[] args) throws IOException {
    
        copy_1();
        copy_2();
        copy_3();
        // copy_4();
    
      }
    
      // 千万不要用,效率没有!
      public static void copy_4() throws IOException {
        FileInputStream fis = new FileInputStream("F:\0.zip");
        FileOutputStream fos = new FileOutputStream("F:\4.zip");
    
        long start = System.currentTimeMillis();
    
        int ch = 0;
    
        while ((ch = fis.read()) != -1) {
          fos.write(ch);
        }
    
        long end = System.currentTimeMillis();
        System.out.println("拷贝完成4---" + (start - end));
    
        fos.close();
        fis.close();
      }
    
      // 不建议。 测试速度还可以
      public static void copy_3() throws IOException {
        FileInputStream fis = new FileInputStream("F:\0.zip");
        FileOutputStream fos = new FileOutputStream("F:\3.zip");
    
        long start = System.currentTimeMillis();
    
        byte[] buf = new byte[fis.available()];
        fis.read(buf);
        fos.write(buf);
    
        long end = System.currentTimeMillis();
        System.out.println("拷贝完成3---" + (start - end));
    
        fos.close();
        fis.close();
      }
    
      // 速度一般
      public static void copy_2() throws IOException {
    
        FileInputStream fis = new FileInputStream("F:\0.zip");
        BufferedInputStream bufis = new BufferedInputStream(fis);
    
        FileOutputStream fos = new FileOutputStream("F:\2.zip");
        BufferedOutputStream bufos = new BufferedOutputStream(fos);
    
        long start = System.currentTimeMillis();
    
        int ch = 0;
    
        while ((ch = bufis.read()) != -1) {
          bufos.write(ch);
        }
    
        long end = System.currentTimeMillis();
        System.out.println("拷贝完成2---" + (start - end));
    
        bufos.close();
        bufis.close();
      }
    
      // 速度最快
      public static void copy_1() throws IOException {
    
        FileInputStream fis = new FileInputStream("F:\0.zip");
        FileOutputStream fos = new FileOutputStream("F:\1.zip");
    
        byte[] buf = new byte[1024];
    
        int len = 0;
    
        long start = System.currentTimeMillis();
    
        while ((len = fis.read(buf)) != -1) {
          fos.write(buf, 0, len);
        }
    
        long end = System.currentTimeMillis();
    
        System.out.println("拷贝完成1---" + (start - end));
    
        fos.close();
        fis.close();
      }
    }

     Demo14 读取键盘输入

    public class ReadKey {
      public static void main(String[] args) throws IOException {
    
        // readKey();
        // System.out.println((int)'
    ');
        // System.out.println((int)'
    ');
    
        readKey2();
    
      }
    
      public static void readKey2() throws IOException {
    
        /*
         * 获取用户键盘录入的数据, 并将数据变成大写显示在控制台上, 如果用户输入的是over,结束键盘录入。
         * 
         * 思路: 1,因为键盘录入只读取一个字节,要判断是否是over,需要将读取到的字节拼成字符串。 2,那就需要一个容器。StringBuilder.
         * 3,在用户回车之前将录入的数据变成字符串判断即可。
         */
    
        // 1,创建容器。
        StringBuilder sb = new StringBuilder();
    
        // 2,获取键盘读取流。
        InputStream in = System.in;
    
        // 3,定义变量记录读取到的字节,并循环获取。
        int ch = 0;
    
        while ((ch = in.read()) != -1) {
    
          // 在存储之前需要判断是否是换行标记 ,因为换行标记不存储。
          if (ch == '
    ')
            continue;
          if (ch == '
    ') {
            String temp = sb.toString();
            if ("over".equals(temp))
              break;
            System.out.println(temp.toUpperCase());
            sb.delete(0, sb.length());
          } else
            // 将读取到的字节存储到StringBuilder中。
            sb.append((char) ch);
    
          // System.out.println(ch);
        }
    
      }
    
      public static void readKey() throws IOException {
    
        InputStream in = System.in;
    
        int ch = in.read();// 阻塞式方法。
        System.out.println(ch);
        int ch1 = in.read();// 阻塞式方法。
        System.out.println(ch1);
        int ch2 = in.read();// 阻塞式方法。
        System.out.println(ch2);
    
        // in.close();
    
        // InputStream in2 = System.in;
        // int ch3 = in2.read();
    
      }
    }

    流的操作规律:

    之所以要弄清楚这个规律,是因为流对象太多,开发时不知道用哪个对象合适。

    想要知道开发时用到哪些对象。只要通过四个明确即可。

    1,明确源和目的(汇)
        源:InputStream  Reader
        目的:OutputStream  Writer

    2,明确数据是否是纯文本数据。
        源:是纯文本:Reader
            否:InputStream
        目的:是纯文本 Writer
            否:OutputStream
        
        到这里,就可以明确需求中具体要使用哪个体系。

    3,明确具体的设备。
        源设备:
            硬盘:File
            键盘:System.in
            内存:数组
            网络:Socket流
            
        目的设备:
            硬盘:File
            控制台:System.out
            内存:数组
            网络:Socket流

    4,是否需要其他额外功能。
        1,是否需要高效(缓冲区);
            是,就加上buffer.
        2,转换。

    需求1:复制一个文本文件。
        1,明确源和目的。
            源:InputStream Reader
            目的:OutputStream  Writer
        2,是否是纯文本?
            是!
            源:Reader
            目的:Writer
            
        3,明确具体设备。
            源:
                硬盘:File
            目的:
                硬盘:File
        
            FileReader fr = new FileReader("a.txt");
            FileWriter fw = new FileWriter("b.txt");
            
        4,需要额外功能吗?
            需要,需要高效。
            BufferedReader bufr = new BufferedReader(new FileReader("a.txt"));
            BufferedWriter bufw = new BufferedWriter(new FileWriter("b.txt"));

    需求2:读取键盘录入信息,并写入到一个文件中。
            
        1,明确源和目的。
            源:InputStream Reader
            目的:OutputStream  Writer
        2,是否是纯文本呢?
            是,
            源:Reader
            目的:Writer
        3,明确设备
            源:
                键盘。System.in
            目的:
                硬盘。File
                
            InputStream in = System.in;
            FileWriter fw = new FileWriter("b.txt");
            这样做可以完成,但是麻烦。将读取的字节数据转成字符串。再由字符流操作。
        4,需要额外功能吗?
            需要。转换。    将字节流转成字符流。因为名确的源是Reader,这样操作文本数据做便捷。
                所以要将已有的字节流转成字符流。使用字节-->字符 。InputStreamReader
            InputStreamReader isr = new InputStreamReader(System.in);
            FileWriter fw = new FileWriter("b.txt");
            
            还需要功能吗?
            需要:想高效。
            BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
            BufferedWriter bufw = new BufferedWriter(new FileWriter("b.txt"));
                    
        
    需求3:将一个文本文件数据显示在控制台上。
        1,明确源和目的。
            源:InputStream Reader
            目的:OutputStream  Writer
        2,是否是纯文本呢?
            是,
            源:Reader
            目的:Writer
        3,明确具体设备
            源:
                硬盘:File
            目的:
                控制台:System.out
                
            FileReader fr = new FileReader("a.txt");
            OutputStream out = System.out;//PrintStream
        4,需要额外功能吗?
            需要,转换。
            FileReader fr= new FileReader("a.txt");
            OutputStreamWriter osw = new OutputStreamWriter(System.out);
            需要,高效。
            BufferedReader bufr = new BufferedReader(new FileReader("a.txt"));
            BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(System.out));
            

    需求4:读取键盘录入数据,显示在控制台上。
        1,明确源和目的。
            源:InputStream Reader
            目的:OutputStream  Writer
        2,是否是纯文本呢?
            是,
            源:Reader
            目的:Writer
        3,明确设备。
            源:
                键盘:System.in
            目的:
                控制台:System.out
            
            InputStream in = System.in;
            OutputStream out = System.out;
            
        4,明确额外功能?
            需要转换,因为都是字节流,但是操作的却是文本数据。
            所以使用字符流操作起来更为便捷。
            InputStreamReader isr = new InputStreamReader(System.in);
            OutputStreamWriter osw = new OutputStreamWriter(System.out);
            
            为了将其高效。
            BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
            BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(System.out));
            

    5,将一个中文字符串数据按照指定的编码表写入到一个文本文件中.
        
        1,目的。OutputStream,Writer
        2,是纯文本,Writer。
        3,设备:硬盘File
        FileWriter fw = new FileWriter("a.txt");
        fw.write("你好");
        
        注意:既然需求中已经明确了指定编码表的动作。
        那就不可以使用FileWriter,因为FileWriter内部是使用默认的本地码表。
        只能使用其父类。OutputStreamWriter.
        OutputStreamWriter接收一个字节输出流对象,既然是操作文件,那么该对象应该是FileOutputStream
        
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("a.txt"),charsetName);
        
        需要高效吗?
        BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("a.txt"),charsetName));

    什么时候使用转换流呢?

    转换流:
    InputStreamReader :字节到字符的桥梁。解码。
    OutputStreamWriter:字符到字节的桥梁。编码。

        1,源或者目的对应的设备是字节流,但是操作的却是文本数据,可以使用转换作为桥梁。
            提高对文本操作的便捷。
        2,一旦操作文本涉及到具体的指定编码表时,必须使用转换流 。

    转换Demo1

    public class TransStreamDemo {
      public static void main(String[] args) throws IOException {
    
        // 字节流。
        InputStream in = System.in;
    
        // 将字节转成字符的桥梁。装换流。
        InputStreamReader isr = new InputStreamReader(in);
    
        // int ch = isr.read();
        // System.out.println((char)ch);
    
        // 字符流。
        BufferedReader bufr = new BufferedReader(isr);
    
        OutputStream out = System.out;
    
        OutputStreamWriter osw = new OutputStreamWriter(out);
    
        BufferedWriter bufw = new BufferedWriter(osw);
    
        String line = null;
    
        while ((line = bufr.readLine()) != null) {
          if ("over".equals(line))
            break;
          // System.out.println(line.toUpperCase());
          // osw.write(line.toUpperCase()+"
    ");
          // osw.flush();
    
          bufw.write(line.toUpperCase());
          bufw.newLine();
          bufw.flush();
        }
    
      }
    
    }

    转换Demo2

    public class TransStreamDemo2 {
    public static void main(String[] args) throws IOException {
    
        BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("F:\b.txt")));
        
        
        String line = null;
        
        while((line=bufr.readLine())!=null){
          if("over".equals(line))
            break;
          
          bufw.write(line.toUpperCase());
          bufw.newLine();
          bufw.flush();
        }
        
        
      }
    }

    转换Demo3

    public class TransStreamDemo3 {
      public static void main(String[] args) throws IOException {
    
        readText_2();
      }
    
      public static void readText_2() throws IOException, FileNotFoundException {
    
        InputStreamReader isr = new InputStreamReader(new FileInputStream("F:\demo.txt"), "utf-8");
        char[] buf = new char[10];
        int len = isr.read(buf);
        String str = new String(buf, 0, len);
        System.out.println(str);
    
        isr.close();
      }
    
      public static void readText_1() throws IOException {
    
        FileReader fr = new FileReader("gbk_1.txt");
    
        char[] buf = new char[10];
        int len = fr.read(buf);
        String str = new String(buf, 0, len);
        System.out.println(str);
    
        fr.close();
    
      }
    
      public static void writeText_3() throws IOException {
    
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("u8_1.txt"), "UTF-8");
    
        osw.write("你好");
        osw.close();
    
      }
    
      public static void writeText_2() throws IOException {
    
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("gbk_3.txt"), "GBK");
    
        // OutputStreamWriter osw = new OutputStreamWriter(new
        // FileOutputStream("gbk_3.txt"),"GBK");
        // FileWriter fw = new FileWriter("gbk_1.txt");
    
        /*
         * 这两句代码的功能是等同的。 FileWriter:其实就是转换流指定了本机默认码表的体现。而且这个转换流的子类对象,可以方便操作文本文件。
         * 简单说:操作文件的字节流+本机默认的编码表。 这是按照默认码表来操作文件的便捷类。
         * 
         * 如果操作文本文件需要明确具体的编码。FileWriter就不行了。必须用转换流。
         */
    
        osw.write("你好");
    
        osw.close();
    
      }
    
      public static void writeText_1() throws IOException {
    
        FileWriter fw = new FileWriter("gbk_1.txt");
    
        fw.write("你好");
    
        fw.close();
      }
    }
  • 相关阅读:
    数据库专题(SQLServer、MySQL)
    第九节:IdentityServer4简介及客户端模式和用户密码模式的应用
    第八节:理解认证和授权、Oauth2.0及四种授权模式、OpenId Connect
    第七节:基于Ocelot网关层的微服务校验(手写jwt校验中间件和利用IdentityModel.Tokens.Jwt校验)
    第六节:Ocelot集成Polly熔断降级,以及缓存、限流、自身负载等其它功能
    第五节:基于Ocelot网关简介、路由功能、集成Consul使用
    第四节:Polly基于控制台和Web端用法(熔断、降级、重试、超时处理等)
    第三节:基于Consul做服务的配置中心
    第二节:Consul简介及服务注册、发现、健康检查
    第一节:微服务准备(Webapi复习、PostMan的使用、项目启动方式、业务代码准备)
  • 原文地址:https://www.cnblogs.com/huanyi0723/p/5007423.html
Copyright © 2011-2022 走看看