質問

私は次のコードを持っていますが、それは機能していないようです。誰かがアイデアを持っている理由はありますか?基本的に、削除しようとしている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 );
    }
}
役に立ちましたか?

解決

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

すなわち

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

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

他のヒント

これは常に真実です: "name"!= "name" || "" email "!=" name "

if(! (line.trim().equals(name) || line.trim().equals(email)) ){
}

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top