Java foundation - file read

Said first create test.txt in the E drive, says "hello world"

Then write the test class as follows FileIOTest

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class FileIOTest {
    public static void main(String[] args) {
        File file = new File("E:/test.txt");
        FileInputStream is = null;
        try {
            is = new FileInputStream(file);
            int temp;
            while ((temp = is.read()) != -1) {
                System.out.print((char) temp);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

Results are as follows

 

 

Guess you like

Origin www.cnblogs.com/niudaben/p/11901415.html