【发布时间】:2014-12-11 16:44:34
【问题描述】:
我的代码假设读取一个文件,其中整数由换行符分隔并反向打印出来。我的代码只有在整数在第一行时才有效,但如果整数由新行分隔,它会给出这个
正在打开文件...
文件已打开
逆序前 [5 12 3 5 4 , 0]
逆序后 [ 4 5 3 21 5, 0]
用包含这个的文件
5 12 3 5 4
0
如何让我的代码改为显示:0 4 5 3 21 5
下面是我的代码
public static void main(String[] args) throws Exception {
String line =null;
ArrayList<StringBuilder> arrayList = new ArrayList<StringBuilder>();
System.out.println("Opening file... \n");
try
{
BufferedReader in = new BufferedReader(new FileReader(new File("file.txt")));
System.out.println("File opened");
while((line = in.readLine()) != null && line.length()!= 0) {
//String[] input = line.split(" ");
arrayList.add(new StringBuilder(line));
}
in.close();
System.out.println("Before Reverse Order " + arrayList);
for(int i = 0; i < arrayList.size(); i++) {
arrayList.get(i).reverse();
}
System.out.println("After Reverse Order " + arrayList);
}
catch (IOException e)
{
e.printStackTrace();
}
}
【问题讨论】:
标签: java arraylist bufferedreader