文本文件 - 多行字符串成一行

问题描述 投票:0回答:1

林解析txt文件,并执行一些编辑任务。林而改变多行字符串转换成一个线串卡住。

工作流:1)加入多线成一个第2行)中提取其中包含一些炭或startsWith特定行

已经尝试了一些方法,但没有预期的效果。

我们的目标是有这样一行:

Jrn.Directive "WindowSize"  , "[A.rvt]", "Floor Plan: Level 1" , 1912, 849

基于

 Jrn.Directive "WindowSize"  _
         , "[A.rvt]", "Floor Plan: Level 1" _
         , 1912, 849

尝试:

line.lines().collect(Collectors.joining("_"+"[\n]"));

要么

line.replaceAll("  _\n" +
                        "         ,");

感谢您的任何意见更新:

工作流程:

  1. 文本包含以下文本(它是整个txt文件的一小部分) - 我不能贴吧作为代码,请见截图 Jrn.Directive “WindowSize” _ “[A.rvt]”, “楼层平面:1级” _,1912年,849 '0:<.Marshalling' 0:<... CompactCaching = 1(有效)“0: <.ThreadPool '0:<... ActivePoolSize = 51' 0:<... ConfiguredPoolSize =自动 '0:<... ParallelCores = 8' 0:<... RequestedPoolSize =自动 '0:<.Tuning' 0:<... ElemTable = 1(除了串行时多线程)'0:<BC:0,0,0 Jrn.Directive “WindowSize” _ “[A.rvt]”, “楼层平面:1级” _ ,1912年,84

请看截图https://i.ibb.co/0cRrwcR/2019-02-03-1947.png

  1. 因为我会提取其中startsWith Jrn.D等我需要加入这一和得到的字符串 Jrn.Directive “WindowSize”, “[A.rvt]”, “平面图:1级”,1912年,849

我认为这是首先需要确定哪些线路需要连接之后,我可以提取其中包含例如像有趣的信息,这些线路与Jrn.D开始。

代码我使用的是找到特定字符串什么

import java.io.*;
import java.util.stream.Collectors;
public class ReadFromFile {
    public static void main(String [] args) {
        // The name of the file to open.
        String fileName = "test.txt";

        // This will reference one line at a time
        String line = null;

        try {
            // FileReader reads text files in the default encoding.
            FileReader fileReader =
                    new FileReader(fileName);

            // Always wrap FileReader in BufferedReader.
            BufferedReader bufferedReader =
                    new BufferedReader(fileReader);

            while((line = bufferedReader.readLine()) != null) {

            // Im defining which lines are important for me but firstly I 
            //need have them in one line especially when looking for Jrn
                if (line.startsWith("Jrn")|| 
                line.contains("started recording journal file")|| 
                line.contains("' Build:")|| line.contains("Dim Jrn"))
                System.out.println(line);
            }
            // Always close files.
            bufferedReader.close();
        }
        catch(FileNotFoundException ex) {
            System.out.println(
                    "Unable to open file '" +
                            fileName + "'");
        }
        catch(IOException ex) {
            System.out.println(
                    "Error reading file '"
                            + fileName + "'");
            // Or we could just do this:
            // ex.printStackTrace();
        }

    }
}
java string text-files bufferedreader
1个回答
0
投票

最好的(侵入性最小的文件)的方式我能想到去了解你的具体问题是在的Jrn.Directive元信息的末尾添加一个分隔符(*)如果该可能性,e.g领域内是:

Jrn.Directive "WindowSize" _ , "[A.rvt]", "Floor Plan: Level 1" _ , 1912, 849*

然后,您可以使用一个循环来连续打印每个令牌不匹配的分隔符,并打破循环当它。

像这样的事情

    //File object instantiation
    File file = new File("test.txt");

    //Iterator which loops over every line in the file
    Iterator<String> iterator = Files.readAllLines(file.toPath()).iterator();

    //The end delimiter for you Jrn.Directive information
    String delimiter = "*";

    while(iterator.hasNext()) {
            //String to store current line
            String line = iterator.next();
            //Execute if line starts with Jrn.Directive
            if (line.startsWith("Jrn")) {
                //JrnLoop to serialize Jrn.Directive information
                JrnLoop: while(true) {
                    //Splitting and processing each character in the current line
                    for(String token: line.split("")) {
                        //Escape and break the JrnLoop if the current character matches end delimiter
                        if (token.matches(delimiter)) {
                            System.out.println();
                            break JrnLoop;
                        }
                        //Otherwise print the current character
                        System.out.print(token);
                    }
                    //Go to the next line of the Jrn.Directive information
                    line = iterator.next();
                }
            }
            //If the line does not start with Jrn.Directive
            else {
                System.out.println(line);

        }

至于为什么你Jrn.Directive信息存储在文件中的多条线路,我真的不知道

© www.soinside.com 2019 - 2024. All rights reserved.