我将解析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" +
" ,");
感谢任何建议 更新:
工作流程:
文本包含以下文本(这是整个txt文件的一小部分)-我无法将其粘贴为代码,请参见屏幕截图
Jrn.Directive“ WindowSize” _
,“ [[A.rvt]””,“平面图:1级” _
,1912,849
'0:<。编组
'0:<... CompactCaching = 1(启用)
'0:<.ThreadPool
'0:<... ActivePoolSize = 51
'0:<... ConfiguredPoolSize =自动
'0:<... ParallelCores = 8
'0:<... RequestedPoolSize =自动
'0:<。
'0:<... ElemTable = 1(多线程时除外)
'0:
请查看屏幕快照https://i.ibb.co/0cRrwcR/2019-02-03-1947.png
因为我要提取以 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();
}
}
}
答案 0 :(得分:0)
我认为要解决您的特定问题的最佳方式(对文件的侵入最少)是在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信息存储在文件中的多行中,我真的不知道