简体   繁体   English

如何从文本文件中删除字符串?

[英]How to delete String from text file?

I'm working on creating simple address book using text file but my code is throwing too much errors in deleting a String method.It shows IO Exceptions at mostly places and when the IO Exceptions are resolved then compiling error cannot find symbol occurs at 5 places in some identifiers.我正在使用文本文件创建简单的通讯簿,但我的代码在删除字符串方法时抛出了太多错误。它在大多数地方显示 IO 异常,当 IO 异常得到解决时,编译错误在 5 个地方找不到符号在一些标识符中。 Here is my code:这是我的代码:

    public void DeletePerson(){    
      try {
         File file = new File("AddressBook.txt");
         File temp = File.createTempFile("file", ".txt", file.getParentFile()); 
         BufferedReader reader = new BufferedReader(new InputStreamReader(new 
           FileInputStream(file), Charset));

         PrintWriter writer = new PrintWriter(new OutputStreamWriter(new 
           FileOutputStream(temp), Charset));
         //More code ...
      } finally {
        if (writer != null) {
            System.out.println("Closing PrintWriter");
            writer.close(); 
        } else { 
            System.out.println("PrintWriter not open");
        }
        file.delete();
        temp.renameTo(file);
      }
    }

Output: Output:

    C:\java\AddressBook>javac AddressBook.java
    
    AddressBook.java:50: error: cannot find symbol
    
        if (writer != null) {
            ^
      symbol:   variable writer
    
      location: class AddressBook
    
    AddressBook.java:52: error: cannot find symbol
    
            writer.close();
            ^
      symbol:   variable writer
    
      location: class AddressBook
    
    AddressBook.java:57: error: cannot find symbol
    
    file.delete();
    ^
      symbol:   variable file
    
      location: class AddressBook
    
    AddressBook.java:58: error: cannot find symbol
    
    temp.renameTo(file);
                  ^
      symbol:   variable file
    
      location: class AddressBook
    
    AddressBook.java:58: error: cannot find symbol
    
    temp.renameTo(file);
    ^
      symbol:   variable temp
    
      location: class AddressBook

I'm creating address-book and finding problem in deleting person's name method.我正在创建通讯录并在删除人名方法中发现问题。 Firstly i have to take input from user ie name of the person then i have to check the text-file(read the file) and find the matched word and then delete it from addressbook.首先,我必须从用户那里获取输入,即人名,然后我必须检查文本文件(读取文件)并找到匹配的单词,然后将其从地址簿中删除。 I have also made other methods to delete the name but they didn't work thoroughly.我还采用了其他方法来删除名称,但它们并没有彻底奏效。 Kindly check the code and resolve the problem please.请检查代码并解决问题。

Your File and PrintWriter Object inside the try block您的FilePrintWriter Object 在try块内
You can try to use it.你可以尝试使用它。

File file = new File("AddressBook.txt");
PrintWriter writer = new PrintWriter(new FileWriter(file,Charset.forName("UTF-8")));//use "throws IOException" in your method.
try{
//some java code
}finally{
    if (writer != null) { 
        System.out.println("Closing PrintWriter");
        writer.close(); 
    } else { 
        System.out.println("PrintWriter not open");
    } 
    file.delete();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM