文件流

一、获取指定路径的文件内容

public static void main(String[] args) {
        try {
            String encoding="UTF-8";
            File file=new File("C:/Users/Administrator/Desktop/新建文本文档.txt");
            if(file.isFile() && file.exists()){ //判断文件是否存在
                InputStreamReader read = new InputStreamReader(
                new FileInputStream(file),encoding);//考虑到编码格式
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt = null;
                while((lineTxt = bufferedReader.readLine()) != null){
                    System.out.println(lineTxt);
                }
                read.close();
            }else{
                System.out.println("找不到指定的文件");
            }
        } catch (Exception e) {
            System.out.println("读取文件内容出错");
            e.printStackTrace();
        }
    }
原文地址:https://www.cnblogs.com/xuegu/p/8491445.html