从文件中读取并将某些部分写入另一个文件

read from file and write some parts in another file

我必须从文本文件中读取并格式化输入。我是 java 阅读文件的新手,我不知道如何处理我阅读的部分内容 这是初始文件:http://pastebin.com/D0paWtAd

而且我必须在另一个文件中写入以下输出: 平均,乔,44,31,18,12,9,10

我设法从文件中取出所有内容并将其打印到输出。我需要帮助来获取我需要的输出并将其打印到屏幕上。感谢任何帮助。

这是我写到现在的:

public class FileParsing {
public static String

read(String filename) throws IOException {
BufferedReader in = new BufferedReader(new FileReader("C:\Users\Bogdi\Desktop\example.txt"));

String s;
StringBuilder sb = new StringBuilder();
while((s = in.readLine())!= null)   sb.append(s + "\n");

in.close();

return sb.toString();
}

很高兴知道您正在使用 "StringBuilder" 组件而不是连接您的 String 值,真棒:)。

除了有关 Java.IO API 处理文件的知识之外,您还需要一些逻辑才能获得预期的结果。在这里,我提出了一种可以帮助你的方法,虽然不是完美的,但可以指出你如何面对这个问题。

//Reference to your file
    String myFilePath = "c:/dev/myFile.txt";
    File myFile = new File(myFilePath);

    //Create a buffered reader, which is a good start
    BufferedReader breader = new BufferedReader(new FileReader(myFile));
    //Define this variable called line that will evaluate each line of our file
    String line = null;
    //I will use a StringBuilder to append the information I need
    StringBuilder appender = new StringBuilder();

    while ((line = breader.readLine()) != null) {
        //First, I will obtain the characters after "equals" sign
        String afterEquals = line.substring(line.indexOf("=") + 1, line.length());

        //Then, if it contains digits...
        if (afterEquals.matches(".*\d+.*")) {
            //I will just get the digits from the line
            afterEquals = afterEquals.replaceAll("\D+","");
        }

        //Finally, append the contents
        appender.append(afterEquals);
        appender.append(",");//This is the comma you want to include
    }

    //I will delete the last comma
    appender.deleteCharAt(appender.length() - 1);

    //Close the reader...
    breader.close();

    //Then create a process to write the content
    BufferedWriter myWriter = new BufferedWriter(new FileWriter(new File("myResultFile.txt")));
    //Write the full contents I get from my appender :)
    myWriter.write(appender.toString());
    //Close the writer
    myWriter.close();
}

希望对您有所帮助。编码愉快!

如果您的目标是在另一个文件中执行指定的输出,您不需要先在 StringBuilder 中获取文件内容再进行处理,您可以将处理后的数据直接附加到 StringBuilder 中,然后就可以将结果写入文件。这是一个适用于给定文件的示例,但如果将来密钥更改,您可能必须修改它:

以下方法将正确处理文件中的数据

public static String read(String filename) throws IOException {
    BufferedReader in = new BufferedReader(new FileReader(filename));

    String s;
    StringBuilder sb = new StringBuilder();
    while((s = in.readLine())!= null) {
        String[] split1 = s.split("=");

        if (split1[0].equals("name")) {

            StringTokenizer tokenizer = new StringTokenizer(split1[1]);
            sb.append(tokenizer.nextToken());
            sb.append(",");

            sb.append(tokenizer.nextToken());
            sb.append(",");


        } else if (split1[0].equals("index")) {

            sb.append(split1[1] + ",");

        } else if (split1[0].equals("FBid")) {
            sb.append(split1[1]);

        } else {

            StringTokenizer tokenizer = new StringTokenizer(split1[1]);
            String wasted = tokenizer.nextToken();

            sb.append(tokenizer.nextToken() + ",");

        }

    }

    in.close();

    return sb.toString();
}

下一个方法会将任何字符串读取到文件

public static void writeStringToFile(String string, String  filePath) throws IOException {

    BufferedWriter writer = new BufferedWriter(
            new FileWriter(
                    new File(filePath)
                    )
            );

    writer.write(string);

    writer.newLine();
    writer.flush();
    writer.close();

}

这是一个简单的测试(File1.txt 包含您在粘贴箱中共享的文件中的数据,我将它们写入另一个文件)

public static void main(String[] args) throws Exception {

    String datas = read("C:\Tests\File1.txt");
    System.out.println(datas);

    writeStringToFile(datas, "C:\Tests\FileOuput.txt" );


}

它将产生您所期望的准确输出

[编辑] @idk,显然你在执行我的示例时遇到异常,而它对我来说工作正常。那只能意味着数据级别存在错误。这是我使用的数据样本(我相信我完全复制了您共享的数据)

结果如下: