ZJU-java advanced notes week 7 (input and output)

  1. Stream is the way Java handles input and output
  2. The basic class of the stream-
    InputStream
    OutputStream in byte form
public class Main {
    
    
    public static void main(String[] args){
    
    
        System.out.println("请输入:");
        //定义一个字节数组
        byte[] buffer = new byte[1024];
        try {
    
    
            int len = System.in.read(buffer);
            String s = new String(buffer,0,len);
            System.out.println("读到了"+len+"字节");//回车也算一个,只不过看不见
            System.out.println(s);
            System.out.println("s的长度是"+s.length());
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
    }
}
输出
请输入:
画眉深浅入时无
读到了22字节
画眉深浅入时无

s的长度是8
  1. Directly read and write files-file stream basic class
    FileInputStream
    FileOutputStream
public class Main {
    
    
    public static void main(String[] args){
    
    
        System.out.println("请输入:");
        //定义一个字节数组
        byte[] buffer = new byte[1024];
        try {
    
    
            System.in.read(buffer);
            FileOutputStream out = new FileOutputStream("a.dat");
            out.write(buffer);
            out.close();
            
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
    }
}
输出
控制台无输出,产生了一个a.dat文件
  1. Filter stream
    The streams we saw earlier can only handle a single byte,
    but when it comes to an int with four bytes?

Filter flow-add a layer of filter flow on the basis of the existing file flow

public class Main {
    
    
    public static void main(String[] args){
    
    
        //定义一个字节数组
        byte[] buffer = new byte[1024];
        try {
    
    
            //写文件
            DataOutputStream out = new DataOutputStream(//处理基本数据类型
                                        new BufferedOutputStream(//起缓冲作用
                                                new FileOutputStream("b.dat")));
            int i = 0xcafebabe;//十六进制(使用十进制也仍然会以十六进制存储)
            out.writeInt(i);
            out.close();
            //再读文件(以十进制输出,给人来看)
            DataInputStream in = new DataInputStream(
                    new BufferedInputStream(
                            new FileInputStream("b.dat")));
            int j = in.readInt();
            System.out.println(j);
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
    }
}
  1. The basic text processing classes
    InputStream/OutputStream can only handle bytes
    DataInputStream/ DataOutputStream can only handle basic data types

Binary data adopts DataInputStream/DataOutputStream,
text data adopts Reader/Writer
(dat people cannot read directly, they need tool software, a.txt people can read directly)

public class Main {
    
    
    public static void main(String[] args){
    
    
        try {
    
    
            //写文件
            PrintWriter out = new PrintWriter(
                                    new BufferedWriter(//起缓冲作用
                                            new OutputStreamWriter(//起桥梁作用
                                                    new FileOutputStream("b.txt"))));
            int i = 123456;
            out.println(i);
            out.close();
            //读源代码文件
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(
                            new FileInputStream("src/IOstream/Main.java")));
            String line;
            //当没有读到文件末尾
            while((line = in.readLine())!= null){
    
    
                System.out.println(line);
            }
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
    }
}
  1. PrintWriter controls the output
     format("format",…);
     printf("format",…);//It is basically the same as C
     print (various basic types);
     println (various basic types);

Create a Scanner object on InputStream or Reader, which
can parse out various basic types expressed in text from the text in the stream
next...()

How to choose a class to read data

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44997802/article/details/108572651