-1

我有文本文件内容,如

name1,name2,name3,....nameN
value1,value2,value3,....valueN

我希望内容是

name1     value1
name2     value2
name3     value3
  .        .
  .        .
nameN     valueN

我怎样才能用java代码实现这一点。

4

3 回答 3

1
String[] names = namesString.split(',');
String[] values = valuesString.split(',');

//usage
System.out.println(names[0] + " with value " + values[0];
于 2013-09-24T06:42:44.283 回答
1
String[] names = namesString.split(",");
String[] values = valuesString.split(",");

for(int i=0;i<names.length;i++){
System.out.println(names[i] + "      " + values[i];
}
于 2013-09-24T06:46:06.427 回答
0
try {
            File file = new File("D:\\temp\\test.txt");
            InputStreamReader reader = new InputStreamReader(new FileInputStream(file));
            BufferedReader bufferedReader = new BufferedReader(reader);
            String namesString = bufferedReader.readLine();
            String valuesString = bufferedReader.readLine();
            String[] names = namesString.split(",");
            String[] values = valuesString.split(",");
            for (int i = 0; i < names.length; i++) {
                System.out.println(names[i] + "\t" + values[i]);
            }
            bufferedReader.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
于 2013-09-24T07:10:00.213 回答