java I/O,读取text文档,储存到array.
Filed in: 中文, java笔记
Add comments
做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 } }
程序如下
- import java.io.*;
- class ReadWorderNum{
- public static void main(String []args){
- String a[][]=new String[30][6]; // this is the reuqire of my assignment, change it into your require
- int i=0; //index of a[i][]
- try{
- FileReader fr = new FileReader(new File("aaa.txt"));
- BufferedReader br = new BufferedReader(fr);
- String s = br.readLine();
- while(s!=null&&i<=99){
- String b[]=s.split(" "); //this is what i have to remember, confused by it for a long time,lol
- for(int j=0; j<b.length; j++){
- a[i][j]=b[j];
- }
- i++;
- s = br.readLine();
- }
- br.close();
- }catch(IOException e){
- System.out.println(e);
- }
- //print out for test
- for(int ii=0; ii<i; ii++){
- for(int m=0; m<4;m++){
- System.out.println(a[ii][m]);
- }
- }
- }
- }
Tags: java text I/O
Recent Comments