您的位置:首页 > 编程语言 > Java开发

Java 如何将一个文件中的两列数据分别读到两个数组中?

2017-07-13 15:28 441 查看
数据如下:
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.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.83322.100	14.44822.200	14.74522.300	14.88322.400	14.90722.500	14.83822.600	15.063
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: