代码如下:

package com.work.javaio;

import java.io.*;

public class ReadTxt {
    public static void readTxtFile(String filePath){
        try{
            String encoding = "UTF-8";
            File file = new File(filePath);
            if(file.isFile() && file.exists()){
                FileInputStream fileInputStream = new FileInputStream(file);
                InputStreamReader read = new InputStreamReader(fileInputStream,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 (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        String filePath = "D:\\temp\\hello.txt";
        readTxtFile(filePath);
    }
}

 

相关文章:

  • 2022-02-10
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-22
  • 2021-06-26
  • 2021-08-21
相关资源
相似解决方案