| 8 | |
| 9 | public class TxtFile { |
| 10 | public static List<String> readTxtFile(String filePath) { |
| 11 | List<String> urList=new ArrayList<String>(); |
| 12 | try { |
| 13 | String encoding = "GBK"; |
| 14 | File file = new File(filePath); |
| 15 | String filename = file.getPath(); |
| 16 | if (file.isFile() && file.exists() && filename.endsWith(".txt")) { //判断文件是否存在,是否为TXT文件 |
| 17 | InputStreamReader read = new InputStreamReader( |
| 18 | new FileInputStream(file), encoding);//考虑到编码格式 |
| 19 | BufferedReader bufferedReader = new BufferedReader(read); |
| 20 | String lineTxt = null; |
| 21 | while ((lineTxt = bufferedReader.readLine()) != null) { |
| 22 | // System.out.println(lineTxt); |
| 23 | urList.add(lineTxt); |
| 24 | } |
| 25 | read.close(); |
| 26 | } else { |
| 27 | System.out.println("找不到指定的TXT文件"); |
| 28 | return null; |
| 29 | } |
| 30 | } catch (Exception e) { |
| 31 | System.out.println("读取文件内容出错"); |
| 32 | e.printStackTrace(); |
| 33 | return null; |
| 34 | } |
| 35 | return urList; |
| 36 | } |
| 37 | } |