循环读取文本中每行的数据
java代码:
package com.zltest.bd; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStreamReader; public class FileRead { public static void main(String[] args) throws Exception { File file = new File("D:\\aa.txt"); InputStreamReader read = new InputStreamReader(new FileInputStream(file),"gbk"); BufferedReader reader = null; String temp = null; int line = 1; try { reader = new BufferedReader(read); while ((temp = reader.readLine()) != null) { System.out.println("line" + line + ":" + temp); line++; } } catch (Exception e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (Exception e) { e.printStackTrace(); } } } } }
文本的内容:
控制台输出的内容: