Java: 读取文件直到找到一个新日期

huangapple go评论66阅读模式
英文:

Java: Read file as long as a new date is found

问题

public class versuch1 {
    public static void main(String[] args) {
        ArrayList<String> liste = new ArrayList<String>();
        String lastLine = "";
        String str_all = "";
        String currLine = "";
        try {
            FileReader fstream = new FileReader("test.txt");
            BufferedReader br = new BufferedReader(fstream);
            while ((currLine = br.readLine()) != null) {
                Pattern p = Pattern.compile("[0-3]?[0-9].[0-3]?[0-9].(?:[0-9]{2})?[0-9]{2} [0-2]?[0-9]:[0-6]?[0-9]:[0-5]");
                Matcher m = p.matcher(currLine);
                if (m.find() == true) {
                    lastLine = currLine;
                    liste.add(lastLine);

                } else if (m.find() == false) {
                    str_all = currLine + " " + lastLine;
                    liste.set((liste.indexOf(currLine)), str_all);

                }

            }
            br.close();

        } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());

        }

        System.out.print(liste.get(0) + " " + liste.get(1);
     }
}
英文:

I want to read the file and add each entry to an arraylist on a date. But the date should also be included.

File Example:

15.09.2002 Hello, this is the first entry.

\t this line, I also need in the first entry.
\t this line, I also need in the first entry.
\t this line, I also need in the first entry.

17.10.2020 And this ist the next entry

I tried this. But the Reader reads only the first Line

public class versuch1 {
public static void main(String[] args) {
	ArrayList&lt;String&gt; liste = new ArrayList&lt;String&gt;();
	String lastLine = &quot;&quot;;
	String str_all = &quot;&quot;;
	String currLine = &quot;&quot;;
	try {
		FileReader fstream = new FileReader(&quot;test.txt&quot;);
		BufferedReader br = new BufferedReader(fstream);
		while ((currLine = br.readLine()) != null) {
			Pattern p = Pattern
					.compile(&quot;[0-3]?[0-9].[0-3]?[0-9].(?:[0-9]{2})?[0-9]{2} [0-2]?[0-9]:[0-6]?[0-9]:[0-5]&quot;);
			Matcher m = p.matcher(currLine);
			if (m.find() == true) {
				lastLine = currLine;
				liste.add(lastLine);

			} else if (m.find() == false) {
				str_all = currLine + &quot; &quot; + lastLine;
				liste.set((liste.indexOf(currLine)), str_all);

			}

		}
		br.close();

	} catch (Exception e) {
		System.err.println(&quot;Error: &quot; + e.getMessage());

	}

	System.out.print(liste.get(0) + &quot; &quot;+liste.get(1);
 }
}

答案1

得分: 1

我已解决了我的问题 Java: 读取文件直到找到一个新日期

public class versuch1 {
    public static void main(String[] args) {
        ArrayList<String> liste = new ArrayList<String>();
        String lastLine = "";
        String currLine = "";
        String str_all = "";
        try {
            FileReader fstream = new FileReader("test.txt");
            BufferedReader br = new BufferedReader(fstream);
            currLine = br.readLine();
            while (currLine != null) {
                Pattern p = Pattern
                        .compile("[0-3]?[0-9].[0-3]?[0-9].(?:[0-9]{2})?[0-9]{2} [0-2]?[0-9]:[0-6]?[0-9]:[0-5]");
                Matcher m = p.matcher(currLine);
                if (m.find() == true) {
                    liste.add(currLine);
                    lastLine = currLine;

                } else if (m.find() == false) {
                    liste.set((liste.size() - 1), (str_all));
                    lastLine = str_all;

                }
                currLine = br.readLine();
                str_all = lastLine + currLine;
            }

        } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());

        }

        System.out.print(liste.get(1) + " ");
    }
}

如果您需要更多帮助,请告诉我。

英文:

I have solved my problem Java: 读取文件直到找到一个新日期

public class versuch1 {
public static void main(String[] args) {
	ArrayList&lt;String&gt; liste = new ArrayList&lt;String&gt;();
	String lastLine = &quot;&quot;;
	String currLine = &quot;&quot;;
	String str_all = &quot;&quot;;
	try {
		FileReader fstream = new FileReader(&quot;test.txt&quot;);
		BufferedReader br = new BufferedReader(fstream);
		currLine = br.readLine();
		while (currLine != null) {
			Pattern p = Pattern
					.compile(&quot;[0-3]?[0-9].[0-3]?[0-9].(?:[0-9]{2})?[0-9]{2} [0-2]?[0-9]:[0-6]?[0-9]:[0-5]&quot;);
			Matcher m = p.matcher(currLine);
			if (m.find() == true) {
				liste.add(currLine);
				lastLine = currLine;

			} else if (m.find() == false) {
				liste.set((liste.size() - 1), (str_all));
				lastLine = str_all;

			}
			currLine = br.readLine();
			str_all = lastLine + currLine;
		}

	} catch (Exception e) {
		System.err.println(&quot;Error: &quot; + e.getMessage());

	}

	System.out.print(liste.get(1) + &quot; &quot;);
}

}

答案2

得分: 0

阅读这些行时,保持一个“当前条目”。

如果所读行以日期开头,则属于一个新条目。在这种情况下,将当前条目添加到条目列表中,并创建一个新的当前条目,其中包含所读行。

如果行不以日期开头,则将其添加到当前条目。

为了使此过程起作用,在循环之前需要将第一行读入当前条目。在循环结束后,需要将当前条目添加到条目列表中。这只有在至少有一行且第一行以日期开头时才起作用。因此要特殊处理没有行的特殊情况(使用if-else语句)。如果第一行不以日期开头,则报告错误。

愉快编码!

英文:

While reading the lines, keep a "current entry".

If the line read begins with a date, then it belongs to a new entry. In this case add the current entry to the list of entries and create a new current entry consisting of the read line.

If the line did not begin with a date, just add it to the current entry.

For this to work, you need to read the first line into the current entry before the loop. And after the loop you need to add the current entry to the list of entries. This in turn only works if there is at least one line and the first line begins with a date. So handle the special case of no lines specially (use if-else). And report an error if the first line does not begin with a date.

Happy coding.

huangapple
  • 本文由 发表于 2020年8月22日 16:21:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63534065.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定