【问题标题】:How can i split a textfile and store 2 values in one line?如何拆分文本文件并在一行中存储 2 个值?
【发布时间】:2014-06-23 20:12:33
【问题描述】:

我有一个文本文件 -> 23/34

我想将它们存储在String One = 23anotherString = 34 中并将它们放在一个字符串中以将它们写在文本文件中,但它不起作用。 :( 每次休息。也许是因为 split 方法,但我不知道如何将它们分开。

try {
    BufferedReader in = new BufferedReader (new FileReader (textfile) );
    try {
        while( (textfile= in.readLine()) != null ) {
            String[] parts = textfileString.split("/");
            String one = parts[0];
        }
    }
}

当我打印或存储one + "/" + anotherString 时,它会在一行中进行换行,但我希望将其全部放在一行中。 :(

【问题讨论】:

  • 如何打印?你试过类似System.out.printf("%s/%s\n", parts[0], parts[1]);

标签: java text save storage java-io


【解决方案1】:
public static void main(String[] args) throws Exception {

    File file = new File("output.txt");
    if (!file.exists()) {
        file.createNewFile();
    }

    BufferedWriter bw = new BufferedWriter(new FileWriter(file.getAbsoluteFile()));
    BufferedReader br = new BufferedReader(new FileReader("input.txt"));

    String line = null;
    while ((line = br.readLine()) != null) {
        String string1 = line.split("/")[0];
        String string2 = line.split("/")[1];
        bw.write(string1 + string2 + "\n");
        bw.flush();
    }

    br.close();
    bw.close();

}

存档:

23/34

导致 output.txt 包含:

2334

您需要阅读每一行,并将其拆分为您指定的字符(“/”)。然后将 string1 分配给第一个拆分,将 string2 分配给第二个拆分。然后,您可以根据需要使用变量。要将它们输出到文件中,您只需将它们与 + 运算符一起附加即可。

【讨论】:

    【解决方案2】:

    您从未向我们展示过您是如何编写文件的,因此我们无法真正帮助您编写代码。这是一种更现代的方法,但我认为它可以满足您的需求。

    File infile = new File("input.txt");
    File outfile = new File("output.txt");
    try (BufferedReader reader = Files.newBufferedReader(infile.toPath());
         BufferedWriter writer = Files.newBufferedWriter(outfile.toPath())) {
        String line;
        while ((line = reader.readLine()) != null) {
            String parts[] = line.split("/");
            String one = parts[0];
            String two = parts[1];
            writer.write(one + "/" + two);
        }
    } catch (IOException ex) {
        System.err.println(ex.getLocalizedMessage());
    }
    

    【讨论】:

    • 然后呢?我看不出有什么问题,这不是什么大的修改。
    【解决方案3】:
        InputStream stream = this.getClass().getResourceAsStream("./test.txt");
        BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
    
        String currentLine;
    
        try {
            while ((currentLine = reader.readLine()) != null) {
                String[] parts = currentLine.split("/");
                System.out.println(parts[0] + "/" + parts[1]);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-25
      相关资源
      最近更新 更多