使用Java从平面文件中读取多个记录

时间:2012-07-03 17:37:32

标签: java io inputstream string-parsing

我有一个文本文件转储,我需要转换为分隔文件。该文件包含一系列“记录”(缺少更好的单词)格式如下:

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循环,递增1 if(inputLine.toLowerCase().startsWith("user:"))并输出计数器变量和我的数据以验证发生了什么。

我的FOR循环在我的伪代码中的第3步之后开始......在BufferedReader之后但在我的IF语句之前。我在步骤6中写入文件后终止它。我正在使用for(recCounter=0;recCounter<10;recCounter++),当我在输出文件中得到10条记录时,它们都是输入文件的LAST记录的实例,编号为0-9 。

将for循环保留在同一个地方,我将其修改为阅读for(recCounter=0;recCounter<10;)并将recCounter的增量放在IF语句中,每次行以{{开头时递增1}}。在这种情况下,我的输出文件中还有10条记录,它们是输入文件中最后一条记录的10个实例,所有计数器都是0。

编辑:鉴于文件的格式如何,确定w =下一个记录的唯一方法是在行的开头单词“User:”的后续实例。每次发生时,直到它发生的NEXT时间构成单个记录。

好像我没有恰当地设置我的“recCounter”,或者我没有将所设置的结果解释为“开始新记录”。

任何人都有关于如何将此文件作为多个记录阅读的建议吗?

2 个答案:

答案 0 :(得分:3)

好的,所以你的伪代码应该是这样的:

declare variables
open file
while not eof
  read input
  if end of set
    format output
    write output
    clear variables
  figure out which variable
  store in correct variable
end-while

可能有一个技巧可以确定你何时完成一套并可以开始下一套。如果一个集合应该由示例中显示的空行终止,那么您可以检查空白行。否则,你怎么知道的?套装总是以“用户”开头吗?

另外,不要忘记写下最后一条记录。你不想在缓冲区/表格中留下未写入的内容。

答案 1 :(得分:1)

从你的描述中听起来就像是以下情况:你实际上并没有在完​​成它们时编写输出字符串,而是在最后完成所有的写入。这听起来并不像是将输出字符串保存在循环之外,因此每次找到记录时,都会覆盖先前计算的输出字符串。

在找到每条记录并创建其输出字符串后,您应该测试您实际上是在写入文件。

如果不发布您的代码,我不确定我是否可以为您提供更多帮助。