Results 1 to 6 of 6
  1. #1
    Elaforestis is offline Member
    Join Date
    Oct 2012
    Posts
    11
    Rep Power
    0

    Default 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
    i want to keep only the first number (before the " - ") on each line of the file.
    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 :)

  2. #2
    ContoNick is offline Member
    Join Date
    Dec 2011
    Location
    Croatia,Zagreb
    Posts
    24
    Rep Power
    0

    Default 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);
    
    }
    Just read line and get your numbers :) ( HINT: google: java file read every line )

    I hope it will help u.

    Have fun :)

  3. #3
    Elaforestis is offline Member
    Join Date
    Oct 2012
    Posts
    11
    Rep Power
    0

    Default 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();
    	}
    I end up with a NullPointerException at int position = nextLine.indexOf(" - ");
    and an empty txt file (dump2new.txt)

  4. #4
    ContoNick is offline Member
    Join Date
    Dec 2011
    Location
    Croatia,Zagreb
    Posts
    24
    Rep Power
    0

    Default 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 :)

  5. #5
    Elaforestis is offline Member
    Join Date
    Oct 2012
    Posts
    11
    Rep Power
    0

    Default 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

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default 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

  1. Java- Writing a file and reading a file line by line
    By Nazneen Ali in forum New To Java
    Replies: 7
    Last Post: 07-20-2011, 08:56 AM
  2. Replies: 1
    Last Post: 02-13-2011, 04:09 PM
  3. Searching and replacing characters in a File
    By DBaskov in forum New To Java
    Replies: 3
    Last Post: 02-01-2011, 12:39 AM
  4. Replies: 8
    Last Post: 12-28-2010, 03:24 PM
  5. Replies: 4
    Last Post: 09-10-2009, 11:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •