Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中从平面文件读取多条记录_Java_Io_Inputstream_String Parsing - Fatal编程技术网

在Java中从平面文件读取多条记录

在Java中从平面文件读取多条记录,java,io,inputstream,string-parsing,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:

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

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

...
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
我的最终结果是得到一个包含分隔值的平面文件。利用上述记录,我们可以看到:

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

    }
import java.util.*;
导入java.io.*;
导入java.nio.file.*;
//
公共类ParseOutlookFolderForSE
{
公共静态void main(字符串参数[])
{
字符串user=“”;
字符串PDLDate=“”;
字符串名称=”;
字符串PDLNum=“”;
字符串problemDesc=“测试”;
字符串解析sc=“测试”;
字符串delim=“;”;
int记录计数器=0;
//
尝试
{
Path file=Path.get(“testfile2.txt”);
FileInputStream fstream=新的FileInputStream(“testfile2.txt”);
//获取DataInputStream的对象
/*DataInputStream in=新的DataInputStream(fstream)*/
BufferedReader br=new BufferedReader(new InputStreamReader(fstream));//缓冲读取器
字符串inputLine=null;//字符串
StringBuffer theText=new StringBuffer();//StringBuffer
//问题:输出仅包含最后一条记录。程序正在整个文件中循环,覆盖记录直到结束。
//基于recordCounter添加for循环

对于(recordCounter=0;recordCounter从您的描述来看,情况似乎如下:您实际上并没有在完成输出字符串时写入它们,而是在结束时完成所有写入操作。听起来您并没有将输出字符串保存在循环之外,因此每次找到一条记录时,您都在覆盖先前计算的输出字符串

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


如果不发布您的代码,我不确定我能帮您做得更多。

从您的描述来看,情况如下:您实际上并不是在完成输出字符串时编写输出字符串,而是在最后完成所有的编写。听起来您并不是在将输出字符串保存到循环之外,因此当您找到一条记录时,您将覆盖以前计算的输出字符串

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


如果不发布您的代码,我不确定我是否能进一步帮助您。

好的,那么您的伪代码应该是这样的:

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

...
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
当你完成一个集合并可以启动下一个集合时,可能有一个诀窍。如果一个集合应该被从你的例子中出现的一个空行终止,那么你可以检查空白行。否则,你怎么知道一个集合总是从“用户”开始?


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

好的,因此您的伪代码应该是这样的:

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

...
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
当你完成一个集合并可以启动下一个集合时,可能有一个诀窍。如果一个集合应该被从你的例子中出现的一个空行终止,那么你可以检查空白行。否则,你怎么知道一个集合总是从“用户”开始?


另外,别忘了写最后一条记录。你不想在缓冲区/表中留下未写的东西。

我也怀疑这是正在发生的事情,但我不知道在哪里发生。代码已经发布了……所以如果你有任何进一步的输入,我很高兴了解你得到了什么!我也怀疑这是正在发生的事情,但我我不知道该在哪里实现。虽然代码已经发布了…所以如果您有任何进一步的输入,我很高兴了解您得到了什么!我想我缺少的是“if end of set”(如果集合结束)部分;我会稍微弄乱它。是的,集合总是以“user:”开头这是文件中唯一可靠的标记。我已经澄清了添加该注释的问题。我认为“if end of set”部分是我所缺少的;我将对此进行一些处理。是的,集合总是以“user:”开头,这是文件中唯一可靠的标记。我已经澄清了添加该注释的问题。您有两行标记“}//close for loop'但只有1 for loop!如果不删除其中一个,代码也不会为我编译。请尝试删除第一个,并告诉我们这是否解决了问题。也不确定为什么所有的append()都可以工作。Java String类没有append()方法。您应该改为使用StringBuilder。正如@ColinD提到的,您还有一个额外的}这是没有意义的。在这里有//close for循环和//close for while注释。@ColinD代码已更新到正确的版本…打开太多窗口的问题!代码仍然无法按预期工作--仅限最后一条记录。@Chris911 append()是基于我今天早些时候遇到的这个问题。我的建议不好吗?@dwwilson66 Chris911指出您在字符串上调用append。您发布的问题建议在StringBuilder上使用它。您有两行标记为“}”//close for loop'但只有1 for loop!如果不删除其中一个,代码也不会为我编译。请尝试删除第一个,并告诉我们这是否解决了问题。也不确定为什么所有的append()都可以工作。Java String类没有append()方法。您应该改为使用StringBuilder。正如@ColinD提到的,您还有一个没有意义的额外的}。其中有//close for循环和//close for while注释。@ColinD代码更新为正确的版本…打开的窗口太多的问题!代码仍然无法按预期工作