我有一个文本文件转储,需要将其转换为分隔文件。该文件包含一系列格式如下的“记录”(因为没有更好的词):
User: abc123
Date: 7/3/12
Subject: the foo is bar
Project: 123456
Problem: foo bar in multiple lines of text
Resolution: foo un-barred in multiple lines of text
User: abc123
Date: 7/3/12
Subject: the foo is bar
Project: 234567
Problem: foo bar in multiple lines of text
Resolution: foo un-barred in multiple lines of text
...
我的最终结果是获得一个包含分隔值的平面文件。使用上面的记录,我们会看到:
abc123;7/3/12;the foo is bar;123456;foo bar in multiple lines of text;foo un-barred in multiple lines of text
abc123;7/3/12;the foo is bar;234567;foo bar in multiple lines of text;foo un-barred in multiple lines of text
代码出现在下面,然后是我遇到的问题。
import java.util.*;
import java.io.*;
import java.nio.file.*;
//
public class ParseOutlookFolderForSE
{
public static void main(String args[])
{
String user = "";
String PDLDate = "";
String name = "";
String PDLNum = "";
String problemDesc = "test";
String resolutionDesc = "test";
String delim = ";";
int recordCounter = 0;
//
try
{
Path file = Paths.get("testfile2.txt");
FileInputStream fstream = new FileInputStream("testfile2.txt");
// Get the object of DataInputStream
/* DataInputStream in = new DataInputStream(fstream); */
BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); //Buffered Reader
String inputLine = null; //String
StringBuffer theText = new StringBuffer(); //StringBuffer
// problem: output contains last record ONLY. program is cycling through the entire file, overwriting records until the end.
// add a for loop based on recordCounter
for(recordCounter=0;recordCounter<10;recordCounter++)
{
while((inputLine=br.readLine())!=null)
{
if(inputLine.toLowerCase().startsWith("from:"))
{
/* recordCounter = recordCounter++; */ // commented out when I added recordCounter++ to the for loop
user = inputLine.trim().substring(5).trim();
}
else
if(inputLine.toLowerCase().startsWith("effective date"))
{
PDLDate = inputLine.trim().substring(15).trim();
}
else
if(inputLine.toLowerCase().startsWith("to:"))
{
name = inputLine.trim().substring(3).trim();
}
else
if(inputLine.toLowerCase().startsWith("sir number"))
{
PDLNum = inputLine.trim().substring(12).trim();
}
} //close for loop
} // close while
System.out.println(recordCounter + "\n" + user + "\n" + name + "\n" + PDLNum + "\n" + PDLDate + "\n" + problemDesc + "\n" + resolutionDesc);
System.out.println(recordCounter + ";" + user + ";" + name + ";" + PDLNum + ";" + PDLDate + ";" + problemDesc + ";" + resolutionDesc);
String lineForFile = (recordCounter + ";" + user + ";" + name + ";" + PDLNum + ";" + PDLDate + ";" + problemDesc + ";" + resolutionDesc + System.getProperty("line.separator"));
System.out.println(lineForFile);
try
{
BufferedWriter out = new BufferedWriter(new FileWriter("testfileoutput.txt"));
out.write(lineForFile);
out.close();
}
catch (IOException e)
{
System.out.println("Exception ");
}
} //close try
catch (Exception e)
{
System.err.println("Error: " + e.getMessage());
}
}
}
我的最终输出只是最后一条记录。我相信正在发生的事情是程序正在读取每一行,但只有最后一个不会被下一条记录覆盖。说得通。所以我添加了一个FOR
循环,递增 1if(inputLine.toLowerCase().startsWith("user:"))
并用我的数据输出计数器变量来验证发生了什么。
我的循环在我的伪代码中的第 3 步之后开始……在我的陈述FOR
之后BufferedReader
但之前。IF
我在第 6 步中写入文件后终止它。我正在使用for(recCounter=0;recCounter<10;recCounter++)
并且在输出文件中获得十条记录时,它们都是输入文件的最后一条记录的实例,编号为 0-9。
将 for 循环留在原处,我将其修改为读取for(recCounter=0;recCounter<10;)
并在语句中放置recCounter
's 增量IF
,每次以User:
. 在这种情况下,我的输出文件中也有 10 条记录,它们是输入文件中最后一条记录的 10 条实例,所有计数器均为 0。
编辑:鉴于文件的格式,从下一条记录中确定 w=一条记录的唯一方法是在行首出现单词“User:”的后续实例。每次发生,直到它发生的下一次是构成单个记录的时间。
似乎我没有正确设置我的“recCounter”,或者我没有将设置的结果解释为“开始新记录”。
有人对如何将此文件作为多条记录读取有任何建议吗?