簡體   English   中英

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

[英]Text file - multiline string into one line

我正在解析txt文件並執行一些編輯任務。 我在將多行字符串更改為一行字符串時卡住了。

工作流程:1)將多行合並為一行2)提取包含一些char或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(Enabled)'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. 因為我將提取以Jrn.D開頭的字符串, 所以我需要將其加入並獲取

    Jrn.Directive“ WindowSize”,“ [A.rvt]”,“平面圖:級別1”,1912,849

我認為有必要先定義哪些行需要連接,然后提取包含有趣信息的行,例如以Jrn.D開頭的信息。

編碼Im用於查找特定st的內容

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();
        }

    }
}

我認為可以解決您的特定問題的最佳方式(對文件的侵入最少)是在Jrn.Directive元信息的末尾添加定界符(*)(如果位於可能性范圍內),例如:

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信息存儲在文件中的多行中,我真的不知道

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM