Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.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 - Fatal编程技术网

Java-文本文件-在某些字符串之间读取

Java-文本文件-在某些字符串之间读取,java,Java,在过去的20个小时里,我试图解决以下问题,所以在我开始考虑跳出窗外之前,我想了想;-),我最好在这里寻求帮助: I have a text file with following content: ID 1 Title Men and mice Content Lenny loves kittens ID 2 Title Here is now only the Title of a Book ID 3 Content Here is now only the Content of a Book

在过去的20个小时里,我试图解决以下问题,所以在我开始考虑跳出窗外之前,我想了想;-),我最好在这里寻求帮助:

I have a text file with following content:
ID
1
Title
Men and mice
Content
Lenny loves kittens
ID
2
Title
Here is now only the Title of a Book
ID
3
Content
Here is now only the Content of a Book
您可以看到的问题是,id后面既有标题又有内容,或者id后面只有标题。 我想创建包含ID值(例如1)和相应的标题值和/或内容值的文本文件

我取得的最好成绩是三张名单。一个具有id值,一个具有标题值,一个具有内容值。但是它实际上是无用的,因为id、内容和标题之间的信息丢失了


我真的非常感谢你的支持

因此您希望用三个字段填充类的集合

class Data {
    int id;
    String title;
    String content;

    // helper method to read a file and return a list.
    public static List<Data> readAll(String filename) throws IOException {
        // List we will return.
        List<Data> ret = new ArrayList<Data>();
        // last value we added.
        Data last = null;
        // Open a file as text so we can read the lines.
        // us try-with-resource so the file is closed when we are done.
        try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
            // declare a String and use it in a loop.
            // read line and stop when we get a null
            for (String line; (line = br.readLine()) != null; ) {
                // look the heading.
                switch (line) {
                    case "ID":
                        // assume ID is always first
                        ret.add(last = new Data());
                        // read the next line and parse it as an integer
                        last.id = Integer.parseInt(br.readLine());
                        break;

                    case "Title":
                        // read the next line and save it as a title
                        last.title = br.readLine();
                        break;
                    case "Content":
                        // read the next line and save it as a content
                        last.content = br.readLine();
                        break;
                }
            }
        }
        return ret;
    }
}
类数据{
int-id;
字符串标题;
字符串内容;
//helper方法读取文件并返回列表。
公共静态列表readAll(字符串文件名)引发IOException{
//我们将返回的列表。
List ret=new ArrayList();
//我们最后增加的价值。
数据last=null;
//以文本形式打开一个文件,以便我们可以读取行。
//我们尝试使用资源,以便在完成后关闭文件。
try(BufferedReader br=new BufferedReader(new FileReader(filename))){
//声明一个字符串并在循环中使用它。
//读取行并在得到空值时停止
for(字符串行;(line=br.readLine())!=null;){
//看标题。
道岔(线路){
案例“ID”:
//假设ID总是第一个
ret.add(last=新数据());
//读取下一行并将其解析为整数
last.id=Integer.parseInt(br.readLine());
打破
案例“标题”:
//阅读下一行并将其另存为标题
last.title=br.readLine();
打破
案例“内容”:
//阅读下一行并将其另存为内容
last.content=br.readLine();
打破
}
}
}
返回ret;
}
}
注意:唯一重要的字段是ID。内容和标题是可选的


要从20小时缩短到5分钟,您需要大量练习。

因此,您需要用三个字段填充一个类集合

class Data {
    int id;
    String title;
    String content;

    // helper method to read a file and return a list.
    public static List<Data> readAll(String filename) throws IOException {
        // List we will return.
        List<Data> ret = new ArrayList<Data>();
        // last value we added.
        Data last = null;
        // Open a file as text so we can read the lines.
        // us try-with-resource so the file is closed when we are done.
        try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
            // declare a String and use it in a loop.
            // read line and stop when we get a null
            for (String line; (line = br.readLine()) != null; ) {
                // look the heading.
                switch (line) {
                    case "ID":
                        // assume ID is always first
                        ret.add(last = new Data());
                        // read the next line and parse it as an integer
                        last.id = Integer.parseInt(br.readLine());
                        break;

                    case "Title":
                        // read the next line and save it as a title
                        last.title = br.readLine();
                        break;
                    case "Content":
                        // read the next line and save it as a content
                        last.content = br.readLine();
                        break;
                }
            }
        }
        return ret;
    }
}
类数据{
int-id;
字符串标题;
字符串内容;
//helper方法读取文件并返回列表。
公共静态列表readAll(字符串文件名)引发IOException{
//我们将返回的列表。
List ret=new ArrayList();
//我们最后增加的价值。
数据last=null;
//以文本形式打开一个文件,以便我们可以读取行。
//我们尝试使用资源,以便在完成后关闭文件。
try(BufferedReader br=new BufferedReader(new FileReader(filename))){
//声明一个字符串并在循环中使用它。
//读取行并在得到空值时停止
for(字符串行;(line=br.readLine())!=null;){
//看标题。
道岔(线路){
案例“ID”:
//假设ID总是第一个
ret.add(last=新数据());
//读取下一行并将其解析为整数
last.id=Integer.parseInt(br.readLine());
打破
案例“标题”:
//阅读下一行并将其另存为标题
last.title=br.readLine();
打破
案例“内容”:
//阅读下一行并将其另存为内容
last.content=br.readLine();
打破
}
}
}
返回ret;
}
}
注意:唯一重要的字段是ID。内容和标题是可选的

要从20小时减少到5分钟,你需要大量练习。

如果你创建一个图书类,然后有一个图书实例列表,你可以在程序中保留“id、内容和标题之间的信息”

图书类别:

public class Book {

    private int id;
    private String title;
    private String content;

    //...
    //getters and setters

}
书籍清单:

private List<Book> books = new ArrayList<Book>();
private List books=new ArrayList();
如果创建一个图书类,然后有一个图书实例列表,则可以在程序中保留“id、内容和标题之间的信息”

图书类别:

public class Book {

    private int id;
    private String title;
    private String content;

    //...
    //getters and setters

}
书籍清单:

private List<Book> books = new ArrayList<Book>();
private List books=new ArrayList();

请显示您已经尝试过的代码,以及您所包含的示例输入文件的输出文件示例。请勿丢失“id、内容和标题之间的信息”。读取id、标题和内容,将它们写入输出,然后转到下一个条目。您不需要任何列表。只需保留字符串和id,并在需要时拆分和读取整个字符串的内容。请显示您已经尝试过的代码,以及输出文件的示例(给定您包含的示例输入文件)。不要丢失“id、内容和标题之间的信息”。读取id、标题和内容,将它们写入输出,然后转到下一个条目。您不需要任何列表。只需保留字符串和id,并在需要时拆分和读取整个字符串的内容。这非常有帮助。非常感谢。我忘了提到,滴度和内容可以有多行。你有没有一个想法,如何改变与此相关的代码?我看到有人可以使用模式来提取两个已定义字符串之间的行,但我不知道如何实现这一点。您能帮忙吗?@javanoob您可以创建一个列表或值,或者简单地将它们与中间的新行组合。