Java 如何将一个文件中的两列数据分别读到两个数组中?
数据如下:22.00013.83322.10014.44822.20014.74522.30014.88322.40014.90722.50014.83822.60015...
数据如下:
22.000 13.833
22.100 14.448
22.200 14.745
22.300 14.883
22.400 14.907
22.500 14.838
22.600 15.063 展开
22.000 13.833
22.100 14.448
22.200 14.745
22.300 14.883
22.400 14.907
22.500 14.838
22.600 15.063 展开
2个回答
展开全部
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class ReadTest {
public static void main(String[] args) {
final String file = "src.txt";
List<String> firstColList = new ArrayList<String>();
List<String> secondColList = new ArrayList<String>();
try {
BufferedReader bf = new BufferedReader(new FileReader(file));
String content = null;
while((content = bf.readLine()) != null){
String ary[] = content.trim().split("\\s+");
firstColList.add(ary[0]);
secondColList.add(ary[1]);
}
bf.close();
} catch (IOException e) {
e.printStackTrace();
}
String[] firstColAry = firstColList.toArray(new String[0]);
String[] secondColAry = secondColList.toArray(new String[0]);
System.out.println("The item in the array is: ");
for(int i = 0; i < firstColAry.length; i++){
System.out.println(firstColAry[i] + "\t" + secondColAry[i]);
}
}
}
---------------测试
The item in the array is:
22.000 13.833
22.100 14.448
22.200 14.745
22.300 14.883
22.400 14.907
22.500 14.838
22.600 15.063
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class ReadTest {
public static void main(String[] args) {
final String file = "src.txt";
List<String> firstColList = new ArrayList<String>();
List<String> secondColList = new ArrayList<String>();
try {
BufferedReader bf = new BufferedReader(new FileReader(file));
String content = null;
while((content = bf.readLine()) != null){
String ary[] = content.trim().split("\\s+");
firstColList.add(ary[0]);
secondColList.add(ary[1]);
}
bf.close();
} catch (IOException e) {
e.printStackTrace();
}
String[] firstColAry = firstColList.toArray(new String[0]);
String[] secondColAry = secondColList.toArray(new String[0]);
System.out.println("The item in the array is: ");
for(int i = 0; i < firstColAry.length; i++){
System.out.println(firstColAry[i] + "\t" + secondColAry[i]);
}
}
}
---------------测试
The item in the array is:
22.000 13.833
22.100 14.448
22.200 14.745
22.300 14.883
22.400 14.907
22.500 14.838
22.600 15.063
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询