0

私は次のコードを持っていますが、それは機能していないようです..誰かがなぜ何か考えを持っていますか?基本的に、削除しようとしている2つの文字列を含む行を除くすべての行を含む新しいファイルを作成していますが、コードが機能していないようです。

public void removelines(String name, String email){
try{
    //Create objects for our file and the temp file.
    File f = new File("addy");
    File temp = new File(f.getAbsolutePath()+ "temp");
    //Our readers.
    BufferedReader buf = new BufferedReader(new FileReader("addy"));
    PrintWriter print = new PrintWriter(new FileWriter(temp));

    String line = null;

    while ((line = buf.readLine())  != null) {
        //put all data not equal to that to be removed into the new file.
            if(!line.trim().equals(name) || !line.trim().equals(email)){
                print.println(line);
                print.flush();
            }
            }
        print.close();
        buf.close();
        f.delete();
        temp.renameTo(f);
    } catch (Exception e){
        JOptionPane.showMessageDialog(null, "ERROR:" + e );
    }
}
4

3 に答える 3

2

あなたの|| &&である必要があります。

すなわち

!line.trim().equals(name) && !line.trim().equals(email)

それ以外の場合、行が名前または電子メールのいずれとも等しくない場合は、書き込まれます。

于 2010-11-29T12:48:09.783 に答える
1

これは常に true です: "Name" != "Name" || " "メール" != "名前"

if(! (line.trim().equals(name) || line.trim().equals(email)) ){
}
于 2010-11-29T12:50:52.810 に答える
0

他のポスターは正しいです。あなたはおそらくこれを読むべきです。

于 2010-11-29T12:54:49.967 に答える