Macbook 使用感受 新建了一个bbs,用来跟monash it的哥们们交流

java I/O,读取text文档,储存到array.

做assignment的时候要用到的一个方法,想了半天,找了半天,没太合适的资料,也不知道怎么下手。
正好想到zz他们在我走之后就开始学java了,现在应该小牛了吧,msn过去问了下,10分钟就搞定了…惭愧啊,没的混了…….想当难……
不多说了。

Request
从text文档读入,然后存入array,比如
test.text内容是
aaaaaa bbbb 123123 abababab
cccccc ddd 43434 cdesfs
读入,然后保存到array a[][] = { { aaaaaa, bbbb, 123123, ababab} { cccccc, ddd, 43434, cdesfs } }

程序如下

  1. import java.io.*;
  2. class ReadWorderNum{
  3. public static void main(String []args){
  4. String a[][]=new String[30][6]; // this is the reuqire of my assignment, change it into your require
  5. int i=0; //index of a[i][]
  6.         
  7. try{
  8. FileReader fr = new FileReader(new File("aaa.txt"));
  9. BufferedReader br = new BufferedReader(fr);
  10. String s = br.readLine();
  11. while(s!=null&&i<=99){
  12. String b[]=s.split(" "); //this is what i have to remember, confused by it for a long time,lol
  13. for(int j=0; j<b.length; j++){
  14. a[i][j]=b[j];
  15. }
  16. i++;
  17. s = br.readLine();
  18. }
  19. br.close();
  20. }catch(IOException e){
  21. System.out.println(e);
  22. }
  23. //print out for test
  24. for(int ii=0; ii<i; ii++){
  25. for(int m=0; m<4;m++){
  26. System.out.println(a[ii][m]);
  27. }
  28. }
  29. }
  30. }

Leave a Reply