JavaSE IO streams learning summary

IO flow

io stream into a byte stream and character stream

Byte stream: input stream InputStream common parent class has a subclass FileInputStream BufferedInputStream

Output stream OutputStream common parent class has a subclass FileOutputStream BfferedOutputStream

Character stream: Reader common parent input stream subclasses have BufferedReader InputStreamReader

Writer common parent class output stream subclass has BufferedWriter OutputStreamWriter

InputStream和OutputStream

This method of each read and write a byte

File

A copy of the case file

private static void copy() throws FileNotFoundException, IOException {
        FileInputStream fis = new FileInputStream("暴躁源泉.jpg");
        FileOutputStream fos = new FileOutputStream("copy.jpg");
        
        int b ;
        while((b = fis.read())!= -1) {
            fos.write(b);
        }      
        //以上三行为核心代码
        fis.close();
        fos.close();
    }

This method is equivalent to a one byte transport, efficiency is very slow.

For this method to improve

public static void main(String[] args) throws IOException {
        //copy();
        //copy2();
        FileInputStream fis = new FileInputStream("A*Teens - Upside Down.mp3");
        FileOutputStream fos = new FileOutputStream("copy2.mp3");
        
        byte[] arr = new byte[fis.available()];
        fis.read(arr);
        fos.write(arr);
        
        fis.close();
        fos.close();
        
    }

This method is equivalent to a total number of bytes of transport bytes size fis, high efficiency compared to methods 1.1. But if too many bytes, memory is likely to cause collapse.

This method will be modified

    public static void main(String[] args) throws IOException {
        //demo1();
        //demo2();
        FileInputStream fis = new FileInputStream("A*Teens - Upside Down.mp3");
        FileOutputStream fos = new FileOutputStream("yyy.mp3");
        
        
        byte[] arr = new byte[1024*8];
        int len;
        while((len = fis.read(arr)) != -1) {
            fos.write(arr,0,len);
        }
        
        
        fis.close();
        fos.close();
    }

This method takes an array arr [8 * 1024] the size of the memory, transported so many bytes of one-time, large enough not likely to cause memory overflow.

BufferedInputStream


    private static void demo1() throws FileNotFoundException, IOException {
        // TODO Auto-generated method stub
        FileInputStream fis = new FileInputStream("A*Teens - Upside Down.mp3");
        FileOutputStream fos = new FileOutputStream("copy。mp3");
        BufferedInputStream bis = new BufferedInputStream(fis);
        BufferedOutputStream  bos = new BufferedOutputStream(fos);
        
        int b ;
        while((b=bis.read()) != -1) {
            bos.write(b);
        }
        
        bos.close();
        bis.close();
    }

Fis need to define the object, the object passed after the fis BufferedInputStream

or

BufferedInputStream bis =
    new BufferedInputStream(new FileInputStream("A*Teens - Upside Down.mp3"));
BufferedOutputStream bos =
    new BufferedOutputStream(new FileOutputStream("copy.mp3"));
        

Reader和Writer

Character stream compared to the byte stream, based on a character basis, depending on the coding mode (GDK, UTF-8) have different compilation mode.

        FileReader fr = new FileReader("xxx.txt");
        int c;
        
        while((c = fr.read()) != -1) {
            System.out.print((char)c);
        }
        
        fr.close();
        FileReader fr = new FileReader("xxx.txt");
        FileWriter fw = new FileWriter("yyy.txt");
        
        char[] arr = new char[1024];
        int len;
        while((len = fr.read(arr)) != -1) {
            fw.write(arr,0,len);
        }
        
        fr.close();
        fw.close();

Just because the character can not turn over 1024 bytes, it is determined whether the current len ​​arr role in some characters in length, the empty fr.read (arr) -1 to len is the length of the remaining arr.

BufferedReader

BufferedReader br = new BufferedReader(new FileReader("zzz.txt"));

Added readline (Reader), and newline (Writer) method.

Respectively read the string of the current line, line feed, and when it is written.

    String line;
        while((line = br.readLine())!=null) {
            bw.write(line);
            bw.newLine();
        }
LinNumberReader
    LineNumberReader lnr = new LineNumberReader(new FileReader("zzz.txt"));
        
        String line;
        lnr.setLineNumber(100);
        while((line = lnr.readLine())!= null)
        {
            System.out.println(lnr.getLineNumber()+":"+line);
        }
        lnr.close();

lnr initial value 1, the current line number reality. Brackets can additionally copy.

About encoding format change

When the encoding format when two different files

BufferedReader br =
                new BufferedReader(new InputStreamReader(new FileInputStream("utf-8.txt"),"utf-8"));
        BufferedWriter bw =
                new BufferedWriter(new OutputStreamWriter(new FileOutputStream("gbk.txt"),"gbk"));
        

After FileInputStream is defined string plus the current corresponding to the encoding format.

Find files recursively small case


import java.io.File;
import java.util.Scanner;

public class Test5 {

    public static void main(String[] args) {
        File dir = getDir();
        printfile(dir);
    }
    
    public static File getDir() {
        Scanner in = new Scanner(System.in);
        System.out.println("请输入一个文件夹路径");
        while(true) {
            String line = in.nextLine();
            File dir = new File(line);
            if(!dir.exists()) {
                System.out.println("您录入的文件夹路劲不存在,请重新输入");
            }else if(dir.isFile()){
                System.out.println("是文件");
            }else
            {
                return dir;
            }
        }
    }
    
    
    public static void printfile(File dir) {
        File[] subFiles =dir.listFiles();
        for (File subFile : subFiles) {
            if(subFile.isFile() && subFile.getName().endsWith(".txt")){
                System.out.println(subFile);
            }else if(subFile.isDirectory()){
                printfile(subFile);
            }
        }
    }
}

Guess you like

Origin www.cnblogs.com/xijue/p/12342717.html