在java中重构文本文件

时间:2013-09-24 06:36:11

标签: java text-files

我有像

这样的文本文件内容
name1,name2,name3,....nameN
value1,value2,value3,....valueN

我希望内容

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

如何使用java代码实现此目的。

3 个答案:

答案 0 :(得分:1)

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

//usage
System.out.println(names[0] + " with value " + values[0];

答案 1 :(得分:1)

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

for(int i=0;i<names.length;i++){
System.out.println(names[i] + "      " + values[i];
}

答案 2 :(得分: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();
        }