import java.io.*; public class ReadFile { public static void main(String[] args) { try { File file = new File("E:\\JavaLog/logs/1.txt"); if (file.isFile() && file.exists()) { //读取的时指定GBK编码格式,若中文出现乱码请尝试utf-8,window默认编码格式为GBK BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "gbk")); String lineTxt = null; while ((lineTxt = br.readLine()) != null) { System.out.println(lineTxt); } br.close(); } else { System.out.println("文件不存在!"); } } catch (Exception e) { System.out.println("文件读取错误!"); } } }
时间: 2024-10-11 21:27:33