-1

I am trying to read a file content and delete some specified String but i am still not succeeded. I have tried this link too

but it is not working, and I tried with ignorecase too and my whole file was deleted in that case. :(

my own code is like

BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), charset));
        PrintWriter writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(tempfile), charset));  BufferedWriter bw = new BufferedWriter(writer);

        while((currentLine = reader.readLine()) != null)
        {
            //System.out.println(currentLine);

            if (currentLine.equals(del))
            currentLine = currentLine.replace(del, "");
            writer.write(currentLine);

        }
      tempfile.renameTo(file);
Community
  • 1
  • 1
lost
  • 165
  • 1
  • 4
  • 10
  • What string is it? Does it contain regex characters? – Zavior Sep 09 '13 at 07:40
  • 1
    what's not working? is it an error? exception? output bad? – No Idea For Name Sep 09 '13 at 07:41
  • What does "not working" mean? Do you get an exception? Doesn't your if statement get executed? I see several problems in your code. The biggest one is, that you do not seem to close your Streams. – Matthias Sep 09 '13 at 07:42
  • no no i did close both bufferedreader and writer stream as well, didnt posted here but. and I havent got any exception, error etc but it is not going to my if portion of code, because the file is just the same nothing is being deleted, although i have given a string named del to delete. – lost Sep 09 '13 at 07:47

5 Answers5

0

You logic will Delete the string only if full line matches, If you want to match a part of string in each line , try using matches (or) substring method available in string class

upog
  • 4,965
  • 8
  • 42
  • 81
  • okay so should i tokenize the line first or like split it on some delimiter...let me try it – lost Sep 09 '13 at 07:49
0

Are you getting any exception or simply the new file is having all the content same as original file?

You should close the write stream, so that its content get flushed to physical file and then can try to check the file.

Gyanendra Dwivedi
  • 5,511
  • 2
  • 27
  • 53
  • the original contents are same. not getting any exception . writer.close(); reader.close(); i closed using above. – lost Sep 09 '13 at 08:04
0

currentLine.equals(del) you mean currentLine.matches(del)

currentLine.equals(del) matches one entire line, while currentLine.matches(del) matches a substring of currentLine.

lulyon
  • 6,707
  • 7
  • 32
  • 49
0

You can do this way

    BufferedReader br = new BufferedReader(new FileReader("D:\\test.txt"));
    List<String> lines=new ArrayList<>();
    String currentLine = new String();
    while ((currentLine = br.readLine()) != null) {
        if (currentLine.contains("hi")) {
            lines.add(currentLine.replace("hi", ""));
        }else {
            lines.add(currentLine);
        }
    }
    BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\test.txt"));
    for (String i:lines){
       bw.write(i);
       bw.newLine();
    }
    bw.close();

Here is my input.

   hi how are you?
   I am fine.
   again hi hi.

Out put

how are you?
I am fine.
again  .
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
0

You can keep most of your original code and avoid storing the entire file in a List (which might be bad if your file is large). Here's a stand-alone you can play with:

import java.io.*;

public class StringRepl {
    public void doReplace(String oldFileName, String newFileName) {
        File oldFile = new File(oldFileName);
        File tempFile = new File(newFileName);

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(oldFile), "utf-8"));

            PrintWriter writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(tempFile), "utf-8"));
            BufferedWriter bw = new BufferedWriter(writer);
            String currentLine;
            String del = "hi";
            while((currentLine = reader.readLine()) != null)
            {
                //System.out.println(currentLine);

                if (currentLine.contains(del))
                    currentLine = currentLine.replace(del, "");
                bw.write(currentLine);
                bw.newLine();

            }
            bw.close();
            reader.close();
            writer.close();
            //tempFile.renameTo(oldFile);
        } catch (FileNotFoundException fnfe) {
            System.out.print("No file found: " + oldFileName);
        } catch (UnsupportedEncodingException uee) {
            System.out.print("'utf-8' not supported");
        } catch (IOException ioe) {
            System.out.print("Error using file!");
            ioe.printStackTrace();
        }
    }

    public static void main(String[] args) {
        StringRepl sr = new StringRepl();
        sr.doReplace("o.txt","n.txt");
    }
}

Compile and run:

javac StringRepl.java
java StringRepl

o.txt:

hi how are you?
I am fine.
again hi hi.

n.txt:

 how are you?
I am fine.
again  .
wwwslinger
  • 936
  • 8
  • 14