1 import java.io.BufferedInputStream; 2 import java.io.BufferedReader; 3 import java.io.FileInputStream; 4 import java.io.FileNotFoundException; 5 import java.io.IOException; 6 import java.io.InputStreamReader; 7 8 9 public class testFile { 10 11 public static void main(String[] args) { 12 FileInputStream fis = null; 13 InputStreamReader isr = null; 14 BufferedReader br = null; //用于包装InputStreamReader,提高处理性能。因为BufferedReader有缓冲的,而InputStreamReader没有。 15 try { 16 String str = ""; 17 String str1 = ""; 18 fis = new FileInputStream("d:\files.txt");// FileInputStream 19 // 从文件系统中的某个文件中获取字节 20 isr = new InputStreamReader(fis);// InputStreamReader 是字节流通向字符流的桥梁, 21 br = new BufferedReader(isr);// 从字符输入流中读取文件中的内容,封装了一个new InputStreamReader的对象 22 String[] arr; 23 while ((str = br.readLine()) != null) { 24 //str1 += "NSRDZDAH: " + str + " "; 25 arr = str.split(" "); 26 str1 += "{"NSRDZDAH":"" + arr[0] +"","FPDKZS":""+arr[1]+""},"+" "; 27 } 28 // 当读取的一行不为空时,把读到的str的值赋给str1 29 System.out.println(str1);// 打印出str1 30 } catch (FileNotFoundException e) { 31 } catch (IOException e) { 32 } finally { 33 try { 34 br.close(); 35 isr.close(); 36 fis.close(); 37 // 关闭的时候最好按照先后顺序关闭最后开的先关闭所以先关s,再关n,最后关m 38 } catch (IOException e) { 39 e.printStackTrace(); 40 } 41 } 42 } 43 44 }