Java基础-文件读取

说先在E盘下创建test.txt,内容为“hello world”

然后编写测试类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();
            }
        }

    }
}

运行结果如下

猜你喜欢

转载自www.cnblogs.com/niudaben/p/11901415.html
今日推荐