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
Related Q & A:
- How to use hadoop for text file?Best solution by Stack Overflow
- How to delete row from text file?Best solution by Stack Overflow
- How to extract information from text file in Python?Best solution by Stack Overflow
- Is there a limit on the size of a new file or a text file?Best solution by Stack Overflow
- How to search for a particular string in a text file using java?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.