File stream operations

package com.mepu;

import org.junit.Test;

import java.io.*;

/**
 * @author: 艾康
 * @date: 2019/12/31 17:03
 * @description: TODO
 * @modifiedBy:
 * @version: 1.0
 * io流
 */
public class IOTest {
    /*
    读入
     */
    @Test
    public void test1() {
        //获取到file
        File file = new File("Hello.txt");
        FileReader stream = null;
        try{
             // Get the character stream object 
            Stream = new new the FileReader (File);
             // use the read data read data char 
            char [] = CF2 new new  char [1024 ];
             int len;
             the while ((len = Stream.Read (CF2) !) = -1 ) { 
                String STR = new new String (CF2, 0 , len); 
                System.out.println (STR); 
            } 
        } the catch (IOException E) { 
            e.printStackTrace (); 
        } the finally { 
        } 
        //Close stream object 
        IF (! Stream = null ) {
             the try { 
                stream.close (); 
            } the catch (IOException E) { 
                e.printStackTrace (); 
            } 
        } 
    } 
    / * 
    Write 
     * / 
    @Test 
    public  void test2 () {
         / / create file objects are created directly when creating the object does not exist 
        FileWriter FileWriter = null ;
         the try { 
            File file = new new File ( "Hello1.txt" );
             //Create a character output stream, if you do not pass the second parameter, it defaults to false if additional coverage when an incoming file to true 
            FileWriter = new new FileWriter (File, to true );
             // write data 
            fileWriter.write ( "bye 2019 \ n" ); 
            FileWriter.write ( "2020 the Hello \ n-" ); 
        } the catch (IOException E) { 
            e.printStackTrace (); 
        } the finally {
             // Close stream 
            the try { 
                fileWriter.close (); 
            } the catch (IOException E) { 
                E .printStackTrace (); 
            } 
        }
    } 
    // byte stream 
    @Test
     public  void Test3 () { 
        the FileInputStream inputStream = null ;
         the try {
             // Create a file object 
            File file = new new File ( "Hello.txt" );
             // Create input stream of bytes 
            inputStream = new new the FileInputStream (File);
             // read the data 
            byte [] Buffer = new new  byte [. 5 ];
             int len;
             the while (! (len = InputStream.read (Buffer)) = -. 1 ) { 
                String S= new String(buffer, 0, len);
                System.out.print(s);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭流
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    /*
    Stream buffer 
     * / 
    @Test 
    public  void Test4 () { 
        BufferedInputStream Stream = null ;
         the try {
             // Create a file object 
            File File = new new File ( "Hello.txt" );
             // Create a byte stream objects 
            the FileInputStream FileInputStream = new new the FileInputStream ( File);
             // create a buffered input stream object 
            stream = new new BufferedInputStream (FileInputStream);
             byte [] bytes = new new  byte [. 5 ];
             int len;
            while ((len = stream.read(bytes)) != -1){
                String s = new String(bytes, 0, len);
                System.out.println(s);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭流对象
            try {
                stream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /*
    转换流
     */
    @Test
    public void test5(){
        try {
            FileInputStream inputStream = new FileInputStream("Hello.txt");
            //设置字符集
            InputStreamReader streamReader = new InputStreamReader(inputStream, "UTF-8");
            char[] c = new char[5];
            int len;
            while ((len = streamReader.read(c))!=-1){
                String s = new String(c, 0, len);
                System.out.println(s);
            }
            streamReader.close();
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        }
    }
}

Guess you like

Origin www.cnblogs.com/aikang525/p/12128820.html