Results 1 to 6 of 6
Thread: Replacing each line of a file
- 01-21-2013, 10:59 PM #1
Member
- Join Date
- Oct 2012
- Posts
- 11
- Rep Power
- 0
Replacing each line of a file
Hello,
i have this txt file with a layout like this
Java Code:50 - 3 52 - 8 53 - 35 54 - 14 56 - 23 58 - 45 60 - 28 62 - 89 64 - 57 66 - 466 68 - 167 70 - 1271 72 - 782
How can i do this using bufferedreader and bufferedwriter? I was thinking about getting the indexOf the " - " and then using the substring method to remove the rest of the line but i mess up somehow...
Thanks for your time :)
- 01-21-2013, 11:14 PM #2
Member
- Join Date
- Dec 2011
- Location
- Croatia,Zagreb
- Posts
- 24
- Rep Power
- 0
Re: Replacing each line of a file
Hello.
Your idea is tottaly okey.
Here is simple example:
Java Code:public static void main(String [] args) { // read every line String input = "50 - 3"; //find position of " -" {space}- int position = input.indexOf(" -"); //use substring String firstNumber = input.substring(0, position); System.out.println(firstNumber); }
I hope it will help u.
Have fun :)
- 01-21-2013, 11:29 PM #3
Member
- Join Date
- Oct 2012
- Posts
- 11
- Rep Power
- 0
Re: Replacing each line of a file
this is my code currently:
Java Code:public static void originalDumpReplacer() throws IOException { BufferedReader reader = new BufferedReader(new FileReader("dump2.txt")); int lines = 0; while (reader.readLine() != null) { lines++; } System.out.println("Item count to be dumped: " + lines); System.out.println("Now replacing the original Dump..."); BufferedWriter writer = new BufferedWriter( new FileWriter("dump2new.txt", true)); for (int i = 0; i < lines; i++) { String nextLine = reader.readLine(); int position = nextLine.indexOf(" - "); String firstNumber = nextLine.substring(0, position); writer.write(firstNumber); writer.newLine(); writer.flush(); } writer.close(); reader.close(); }
and an empty txt file (dump2new.txt)
- 01-21-2013, 11:36 PM #4
Member
- Join Date
- Dec 2011
- Location
- Croatia,Zagreb
- Posts
- 24
- Rep Power
- 0
Re: Replacing each line of a file
Hello.
Fast look on your code:
in while loop your reader reached till EOF.
from link: bla bla returns null if the end of the stream has been reached
Think about it :)
Best solution, place breakpoint line before int position = nextline.... and debug it :)
- 01-21-2013, 11:43 PM #5
Member
- Join Date
- Oct 2012
- Posts
- 11
- Rep Power
- 0
Re: Replacing each line of a file
Oh thanks, i understand more about how bufferedreader and bufferedwriter work now!
Added a second reader and everything went like a charm.
Thanks alot for your time
- 01-22-2013, 10:51 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Re: Replacing each line of a file
You only actually need the one reader, and a single loop.
Simply read a line, process it (ie get the part of the string you want) and write that out.
Java Code:String line; while ((line = reader.readLine()) != null) { write substring line. }
Please do not ask for code as refusal often offends.
** This space for rent **
Similar Threads
-
Java- Writing a file and reading a file line by line
By Nazneen Ali in forum New To JavaReplies: 7Last Post: 07-20-2011, 08:56 AM -
How to read file line by line with fixed number of characters
By trkece in forum New To JavaReplies: 1Last Post: 02-13-2011, 04:09 PM -
Searching and replacing characters in a File
By DBaskov in forum New To JavaReplies: 3Last Post: 02-01-2011, 12:39 AM -
Reverse search a Text file from last line to top line
By Jman85 in forum New To JavaReplies: 8Last Post: 12-28-2010, 03:24 PM -
Need to read an .ini and .abook file line by line (both files contain texts)
By ollyworks in forum Java AppletsReplies: 4Last Post: 09-10-2009, 11:18 AM
Bookmarks