从txt文件读取/删除时遇到的Java问题

时间:2020-09-09 18:03:46

标签: java

我正在尝试使用以下代码使用Java创建删除功能。

This是我的原始txt文件,我希望删除该文件中的一行,以便得到类似this的文字。但是,使用我的代码,我得到:1, 1234, javago

有人可以帮我解决这个问题吗?如何获得所需的输出?

public class delete {
    
    private static Scanner x;   
    public static void main(String[] args) {
        
        String filepath = "easy.txt";
        String removeLine = "3333";     
        removeRecord(filepath,removeLine);      
    }
    
    public static void removeRecord(String filepath, String removeLine) {
        
        String tempFile = "temp.txt";
        File oldFile = new File(filepath);
        File newFile = new File(tempFile);
        String no = ""; String ISBN = ""; String title = "";
        
        try {
            FileWriter fw = new FileWriter(tempFile,true);
            BufferedWriter bw = new BufferedWriter(fw);
            PrintWriter pw = new PrintWriter(bw);
            x = new Scanner(new File(filepath));
            
            while (x.hasNext()) {
                no = x.next();
                ISBN = x.next();
                title = x.next();
                
                if (!ISBN.equals(removeLine)) {
                    pw.println(no + "," + ISBN + ","   + title );
                }
                
                x.close();
                pw.flush();
                pw.close();
                oldFile.delete();
                File dump = new File(filepath);
                newFile.renameTo(dump);             
            }
        }
        catch (Exception e) {
            System.out.println("Error !!!");
        }           
    }
}

3 个答案:

答案 0 :(得分:0)

首先,您要关闭while范围内的扫描仪和其他写入器。可能是您将右括号放在错误的行中。

使用.equals比较字符串没有任何问题。您还可以使用Objects.equals,因为这将使您在值上优于null

您应按以下步骤更改代码:

public static void RemoveRecord(String inputFilePath, String outputFilePath, String removeLine) {
    File oldFile = new File(inputFilePath);
    File newFile = new File(outputFilePath);
    
    String no = ""; String ISBN = ""; String title = "";
    
    try
    {
        FileWriter fw = new FileWriter(tempFile, true);
        BufferedWriter bw = new BufferedWriter(fw);
        PrintWriter pw = new PrintWriter(bw);
        
        x = new Scanner(new File(filepath));
        
        while (x.hasNext())
        {
            no = x.next();
            ISBN = x.next();
            title = x.next();
            
            // Using Objects.equals is better here
            if (!Objects.equals(ISBN , removeLine + ','))
            {
                pw.println(no + "," + ISBN + ","   + title );
            }
        }
        
        // This code should be placed outside the while loop
        x.close();
        pw.flush();
        pw.close();
        oldFile.delete();

        File dump = new File(filepath);
        newFile.renameTo(dump);
    }
    catch (Exception e)
    {
        System.out.println("Error !!!");
    }           
}

请尝试一下,让我知道代码是否有问题。

答案 1 :(得分:0)

有一些错误。

  1. 请阅读评论。

    no = x.next(); // no = "1,"
    ISBN = x.next(); // ISBN = "1234,"
    title = x.next(); // title = javago
    
    if (!ISBN.equals(removeLine))
    {
        // No need to add comma here, it is already in a string.
        // That's why you have double comma like "1,," "
        // Change "," to " " (space) instead.
        pw.println(no + "," + ISBN + ","   + title );
    }
    
  2. 由于ISBN包含逗号,因此显然不等于"3333"(不带逗号)。

  3. 您正在关闭循环中的所有对象(扫描仪等)。请把它们放在while循环之外。

答案 2 :(得分:0)

经过测试,此代码可以正常工作!

public class Demo {

    public static void main(String[] args) {
        String filepath = "easy.txt";
        String removeLine = "3333,";
        removeRecord(filepath, removeLine);
    }

    public static void removeRecord(String filepath, String removeLine) {

        String tempFile = "temp.txt";
        File oldFile = new File(filepath);
        File newFile = new File(tempFile);
        String no = "";
        String ISBN = "";
        String title = "";

        try {
            FileWriter fw = new FileWriter(tempFile, true);
            BufferedWriter bw = new BufferedWriter(fw);
            PrintWriter pw = new PrintWriter(bw);
            Scanner x = new Scanner(new File(filepath));

            while (x.hasNext()) {
                no = x.next();
                ISBN = x.next();
                title = x.next();

                if (!ISBN.equals(removeLine)) {
                    pw.println(no + " " + ISBN + " " + title);
                }
            }

            x.close();
            pw.flush();
            pw.close();
            oldFile.delete();
            File dump = new File(filepath);
            newFile.renameTo(dump);
        } catch (Exception e) {
            System.out.println("Error !!!");
        }
    }
}

文件输入

1, 12312, java
2, 33213, hello
3, 3333, delete
4, 123, last

输出写入文件

1, 12312, java
2, 33213, hello
4, 123, last