How to delete a particular line from a file?

Java delete line from text file?

  • Hi Friends, I am writing a java program, which is writting to a text file in this form: aaa : bbb : ccc :ddd eee : ggg : ggg: hhh kkk : ppp : qqq: ccc now i want to delete middle line with eee: ggg : ggg: hhh and move the last line to move one step up. how can i do that.. Thanks

  • Answer:

    How about this public static void copyAndRemoveLine(Reader _in, Writer _out, int line) { try { BufferedReader in = new BufferedReader(_in); PrintWriter out = new PrintWriter(_out); String s = in.readLine(); for(int i=1; s!=null; s=in.readLine(), i++) { if(i != line) out.println(s); } in.close(); out.close(); } catch (Exception e) { e.printStackTrace(); } } public static void copyAndRemoveLine(String in, String out, int line) { try { copyAndRemoveLine(new FileReader(in), new FileWriter(out), line); } catch(Exception e) { e.printStackTrace(); } } public static void main(String[] args) { copyAndRemoveLine("file", "file_copy", line); }

john at Yahoo! Answers Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.