【问题标题】:how to find a character in a text file and save the word after that character to another file如何在文本文件中查找字符并将该字符后面的单词保存到另一个文件
【发布时间】:2016-08-19 17:09:48
【问题描述】:

我正在尝试识别文件每一行中字符 (=) 之后的单词并将其保存到另一个文件中。以下是我到目前为止编写的代码,但它无法正常工作。任何想法编码器?

My input.txt file is :

President=obama
Vice President=Biden
Head of State=Kerry

package Test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.StringJoiner;

public class Stringbuilder {
    public static void main(String[] args) throws FileNotFoundException,     IOException {
    {

        try (BufferedReader br = new BufferedReader(new FileReader("input.txt"))) {
            String sCurrentLine;

            while ((sCurrentLine = br.readLine()) != null) {

                String[] results = sCurrentLine.split("");

                String word = sCurrentLine;

                String Sym = "=";

                for (int i = 0; i < sCurrentLine.length(); i++) {

                    if (word.contains(Sym)) {
                        // System.out.println(results[1]);

                        for (int j = 0; j < sCurrentLine.length(); j++) {

                            String content = results[j];

                            System.out.println(content);

                            File file = new File("output.txt");

                            if (!file.exists()) {
                                file.createNewFile();
                            }

                            FileWriter fw = new FileWriter(file.getAbsoluteFile());
                            BufferedWriter bw = new BufferedWriter(fw);
                            bw.write(content);
                            bw.close();

                            System.out.println("Done");

                        }
                    }
                }
            }

        }
    }
}

}

【问题讨论】:

  • it is not working correctly,需要详细说明吗?提供输入/输出/堆栈跟踪。由于缺乏细节投票结束。
  • 请添加示例输入文件和所需的输出。
  • 我已经添加了输入文件的详细信息。
  • 克里是国务卿,不是国家元首,这点很不一样

标签: java arrays file


【解决方案1】:

你想要这样的东西吗?

public static void main(String[] args) throws IOException {
    try (BufferedWriter bw = new BufferedWriter(new FileWriter(new File("output.txt").getAbsoluteFile()))) {
        for (String sCurrentLine: Files.readAllLines(Paths.get("input.txt"))) {
            String[] splitted = sCurrentLine.split("=");
            if (splitted.length > 1) {
                for (int i = 1; i < splitted.length; i++) {
                    bw.write(splitted[i].split("\\s+")[0] + "\n");
                }
            }
        }
    }
}

【讨论】:

  • 同意 read-lines 和 split 但对于仅给出的示例 split(,2) 并将 splitted[1] 输出为一行。也无需测试和创建文件,new FileWriter 为您完成。
  • 嗨,David Pérez Cabrera,这些名字是组合在一起的。如何让它们在不同的线路上?我认为 + "\n" 会解决的。
  • @dave_thompson_085 你说得对,我曾试图保留这张原始支票,但正如你所说,这没有意义。
  • @sainid 它在不同的行上打印单词:tutorialspoint.com/…
  • @David Pérez Cabrera,当我使用 eclipse 编译并打开它不打印在不同行上的文本文件时,这很奇怪。
【解决方案2】:

我会在嵌套的 for 循环中添加一个 if 语句并更改初始条件:

                    for (int j = 1; j < results.length(); j++) {

                        String previous=results[j-1];
                        if(previous.equals(Sym)){
                          //write results[j] to file
                      }
                    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-08
    • 1970-01-01
    • 1970-01-01
    • 2013-09-25
    • 1970-01-01
    • 1970-01-01
    • 2020-06-24
    相关资源
    最近更新 更多