package com.zml.json; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import org.apache.commons.io.FileUtils; import org.json.JSONException; import org.json.JSONObject; public class ReadJson { public static void readTxtFile(String filePath){ try { int i =0; String encoding="GBK"; File file=new File(filePath); 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("几次:"+i); System.out.println(lineTxt); i++; } read.close(); }else{ System.out.println("找不到指定的文件"); } } catch (Exception e) { System.out.println("读取文件内容出错"); e.printStackTrace(); } } public static void main(String argv[]){ String filePath = "D:\\software\\test.txt"; readTxtFile(filePath); } }
转载于:https://my.oschina.net/u/3229047/blog/1203895