Im parsing txt file and perform some editing tasks. Im stuck while changing multi-line string into one line string.
Workflow: 1) join multi-lines into one line 2) extract specific lines which contain some char or startsWith
tried already some methods but without desired results.
the goal is to have this line:
Jrn.Directive "WindowSize" , "[A.rvt]", "Floor Plan: Level 1" , 1912, 849
based on
Jrn.Directive "WindowSize" _
, "[A.rvt]", "Floor Plan: Level 1" _
, 1912, 849
tried:
line.lines().collect(Collectors.joining("_"+"[\n]"));
or
line.replaceAll(" _\n" +
" ,");
Appreciated for any advice Update:
Workflow:
text contain following text (it is small portion of whole txt file) - I was not able paste it as a code please see screenshot
Jrn.Directive "WindowSize" _ , "[A.rvt]", "Floor Plan: Level 1" _ , 1912, 849 ' 0:< .Marshalling ' 0:< ...CompactCaching = 1 (Enabled) ' 0:< .ThreadPool ' 0:< ...ActivePoolSize = 51 ' 0:< ...ConfiguredPoolSize = automatic ' 0:< ...ParallelCores = 8 ' 0:< ...RequestedPoolSize = automatic ' 0:< .Tuning ' 0:< ...ElemTable = 1 (Serial except when multithreaded) ' 0:< BC: 0,0,0 Jrn.Directive "WindowSize" _ , "[A.rvt]", "Floor Plan: Level 1" _ , 1912, 84
Please see screenshot https://i.ibb.co/0cRrwcR/2019-02-03-1947.png
Because I will be extracting strings which startsWith Jrn.D etc I need to join this and get
Jrn.Directive "WindowSize" , "[A.rvt]", "Floor Plan: Level 1" , 1912, 849
I think it's necessary first to define which lines need to be joined afterwards I can extract lines which contains interesting information like for example these which starts with Jrn.D .
Code what Im using to find specific stings
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();
}
}
}
The best (least intrusive to the file) way I can think to go about your specific problem is to add a delimiter (*) at the end of the Jrn.Directive
meta-information if that's within the realm of possibility, eg:
Jrn.Directive "WindowSize" _ , "[A.rvt]", "Floor Plan: Level 1" _ , 1912, 849*
You can then use a loop to serially print each token that does not match the delimiter and break the loop when it does.
Something like this
//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);
}
As to why your Jrn.Directive information is stored in multiple lines in the file, I really don't know
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.