我已经读取了一个文件到一个数组中,但是想知道如何解析该数组中的某些值。Java Array解析
我的代码:
...
try{
...
String strLine;
String delims= "[ ]+";
//parsing it
ArrayList parsedit = new ArrayList();
while((strLine=br.readLine())!=null){
System.out.println("Being read:");
System.out.println(strLine);
parsedit.add(strLine.split(delims));
}
System.out.println("Length:" + parsedit.size());
in.close();
...
,我读的文件是这样的:
a b c d e f g
1 2 3 4 5 6 7
1 4 5 6 3 5 7
1 4 6 7 3 2 5
这使得像这样的输出:
How many files will be input? 1
Hey, please write the full path of the input file number1!
/home/User/Da/HA/file.doc
Being read:
a b c d e f g
Being read:
1 2 3 4 5 6 7
...
Length:4
我想解析出这些数据,并且只剩下第一个和第五个值,以便它可以像这样读取:
a e
1 5
有没有人有关于如何去做的建议? 编辑: 继一些我已经改变了我的代码的建议:
public static void main(String[] args) {
System.out.print("How many files will be input? ");
Scanner readIn=new Scanner(System.in);
int input=readIn.nextInt();
int i=1;
while(i<=input){
System.out.println("Hey, please write the full path of the input file number" + i + "! ");
Scanner fIn=new Scanner(System.in);
String fileinput=fIn.nextLine();
try{
FileInputStream fstream=new FileInputStream(fileinput);
DataInputStream in=new DataInputStream(fstream);
BufferedReader br=new BufferedReader(new InputStreamReader(in));
String strLine;
String delims= "[ ]+";
ArrayList parsedit = new ArrayList();
while((strLine=br.readLine())!=null){
System.out.println("Being read:");
System.out.println(strLine);
parsedit.add(strLine.split(delims));
}
String[] splits=strLine.split(delims);
System.out.println("Length:" + splits.length);
in.close();
}
catch(Exception e){
System.err.println("Error: " + e.getMessage());
}
i++;
}
}
}
这不给回任何错误,但它似乎并没有被工作都是一样的。我错过了一些愚蠢的东西吗?输出是这样的:
How many files will be input? 1
Hey, please write the full path of the input file number1!
/home/User/Da/HA/file.doc
Being read:
a b c d e f g
Being read:
1 2 3 4 5 6 7
...
但尽管我有一条线,告诉我数组的长度,它永远不会被打印出来,这告诉我,我刚刚结束了打破超过我固定的。你知道我可能会遗漏/忘记什么吗?
除了'BufferedReader.readLine()',你可以尝试使用'BufferedReader.read()'逐个字符地读取字符以找到你要找的东西。 – Shaded
请不要使用DataInputStream来读取文本http://vanillajava.blogspot.co.uk/2012/08/java-memes-which-refuse-to-die.html它比有用的更混乱。 –
@PeterLawrey你会推荐什么? – Stephopolis