Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.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在重新编辑文件后从txt中删除一行_Java_File - Fatal编程技术网

Java在重新编辑文件后从txt中删除一行

Java在重新编辑文件后从txt中删除一行,java,file,Java,File,我有这个档案 1007 book1 5 3 1004 book2 4 1 1003 book3 3 0 1002 book4 2 1 我试图从文件中删除图书编号1004,但在此之前,我让用户输入他想要删除的图书编号。 首先检查该书是否存在于文件中,如果该书存在,我将删除它如果不显示“该书不存在” 我正在尝试打印所有文件,但不打印我已删除的图书。您将需要

我有这个档案

1007    book1           5      3
1004    book2           4      1
1003    book3           3      0
1002    book4           2      1
我试图从文件中删除图书编号1004,但在此之前,我让用户输入他想要删除的图书编号。 首先检查该书是否存在于文件中,如果该书存在,我将删除它如果不显示“该书不存在”


我正在尝试打印所有文件,但不打印我已删除的图书。

您将需要一个图书类,如下所示:

public class Book {
    private int series;
    private String name;
    private int intA;
    private int intB;

    public Book(int series,String name, int intA, int intB) {
        this.series = series;
        this.name = name;
        this.intA = intA;
        this.intB = intB;
    }
    ....
    .... (add other methods as needed, you will definitely need a 
           toString() method, and getIntA(), getIntB(), getSeriesNum(),
           getName() etc.)
}
当您使用扫描仪读取文件时,请将其读入Book类型的arraylist。当用户输入数字时,使用for循环查找与该数字匹配的图书,并从arraylist中删除该图书对象

此外,尽量将数据保存在内存中,不要太频繁地写入文件。与更改内存中的数据相比,将文件写入磁盘效率非常低。用户完成所有操作后,可以使用printwriter将数据写入文件

要将文件读入此对象数组,可以执行以下操作:

public static ArrayList<Book> readbooklist() {

    ArrayList<Book> booklist = new ArrayList<Book>();
    File file = new File("path/filename.fileextension");

    try {
        Scanner scnr = new Scanner(file);

        while (scnr.hasNextLine()) {
            String entry = scnr.nextLine();
            String [] parts = entry.split("\t"); // Depends on how data was delimited
            int series = Integer.parseInt(parts[0]);
            int intA = Integer.parseInt(parts[2]);
            int intB = Integer.parseInt(parts[3]);
            Book single = new Book(series, parts[1], intA, intB);
            booklist.add(single);
        }

        scnr.close();

    } catch (FileNotFoundException e) {
        System.out.println("File Not Found!");
    }

    return booklist;
}
publicstaticarraylistreadbooklist(){
ArrayList booklist=新建ArrayList();
File File=新文件(“path/filename.fileextension”);
试一试{
扫描仪scnr=新扫描仪(文件);
while(scnr.hasNextLine()){
String entry=scnr.nextLine();
String[]parts=entry.split(“\t”);//取决于数据的分隔方式
int series=Integer.parseInt(部分[0]);
int intA=Integer.parseInt(部分[2]);
int intB=Integer.parseInt(部分[3]);
单本=新书(系列,部分[1],intA,intB);
图书目录。添加(单个);
}
scnr.close();
}catch(filenotfounde异常){
System.out.println(“未找到文件!”);
}
归还书目;
}

请记住在开始上课时导入适当的依赖项。希望它有帮助

我建议使用映射将每一行存储为值,Id仅存储为键一次。这样,您就不必在每次要删除或添加条目时重新打开文件并读取它,只需将其删除并添加到地图中即可。完成后,您可以使用地图中存储的值覆盖旧文件,或者只创建一个新的临时文件来保存数据,删除旧文件,然后用旧文件名重命名临时文件。将所有要保存的行存储在字符串中,然后关闭扫描仪。创建一个printwriter,将字符串打印到文件并关闭它

示例代码:

File file = new File("yourTextFile.txt");
Scanner in = new Scanner(file);
String saveThisToFile = "";
while (in.hasNext()) {
    String temp = in.nextLine();
    if (condition to be true if you whant to keep this line) {
        saveThisToFile += temp + "\n";
    }
}
in.close();

PrintWriter printWriter = new PrintWriter(file);
printWriter.print(saveThisToFile);
printWriter.close();

如何将文件读入对象数组?我编辑了我的回答。见上文。我建议不要将它们读入数组,除非您知道预期的图书对象数。当您不知道要处理多少本书时,ArrayList是一种更好的方法
File file = new File("yourTextFile.txt");
Scanner in = new Scanner(file);
String saveThisToFile = "";
while (in.hasNext()) {
    String temp = in.nextLine();
    if (condition to be true if you whant to keep this line) {
        saveThisToFile += temp + "\n";
    }
}
in.close();

PrintWriter printWriter = new PrintWriter(file);
printWriter.print(saveThisToFile);
printWriter.close();