JAVA IO

image.png

availble() 这个INPUT还有多少东西可以读。但是这个值不是准确的值,不能做严谨的操作。

image.png

read() 为啥不是返回BYTE 而是 INT?
因为如果READ到头了,可以返回-1

read(byte[]) 尽量把BYTE Array 填满

public static void main(String[] args) throws IOException {
        FileOutputStream out = null;
        try (FileInputStream in = new FileInputStream("a.txt")){
            out = new FileOutputStream("b.txt");
            int c;
            while ((c = in.read()) != -1) {
                System.out.print((char) c);
                out.write(c);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (out != null)
                out.close();
        }
    }
public static void main(String[] args) throws IOException {
        try (FileInputStream in = new FileInputStream("a.txt")){
            byte[] bytes = new byte[700];
            int n = in.read(bytes);
            System.out.println("n = " + n);
            for (byte b : bytes) {
                System.out.print((char)b);
            }
        }
    }
image.png

如果要读中文,需要用file reader & file writer ; 因为他是16位16位读的。
unicode 是16位, 2^16

public static void main(String[] args) throws IOException {
        try (FileReader in = new FileReader("a.txt")){
            int c;
            while ((c = in.read()) != -1) {
                System.out.print((char) c);
            }
        }
    }
image.png
public static void main(String[] args) throws IOException {
        try(InputStreamReader cin = new InputStreamReader(System.in)) {
            System.out.println("Enter char, 'q' to quit.");
            StringBuffer userInput = new StringBuffer();
            while (true) {
                char c = (char) cin.read();
                if (c == 'q') break;
                userInput.append(c);
            }
            System.out.println(userInput);
        }
    }
image.png
public static void main(String[] args) throws IOException {
        try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("a.txt")))){
            String line = null;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
        }
    }
class MyBufferedReader {
    private FileInputStream in;
    private StringBuffer buffer;

    public MyBufferedReader(FileInputStream in) {
        this.in = in;
        buffer = new StringBuffer();
    }
    
    public String nextLine() throws IOException {
        while (true) {
            int c = in.read();
            if (c == -1 || c == '\n') break;
            buffer.append((char) c);
        }
        String output = buffer.toString();
        buffer = new StringBuffer();
        return output;
    }
}

键盘按行读

public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            String line = scanner.nextLine();
            if (line.toLowerCase().equals("exit"))
                break;
            System.out.println("Input text : " + line);
        }
    }
image.png
public static void main(String[] args) {
        String dirname = "F:/";
        File d = new File(dirname);
        String[] path = d.list();
        for (String p : path) {
            System.out.println(p);
        }
    }

你可能感兴趣的:(JAVA IO)