Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Carriage return in java

Options
  • 24-04-2008 11:19am
    #1
    Registered Users Posts: 427 ✭✭


    Hi,
    I'm writing a program which will replace certain strings in a file. The way I am doing it is I read the entire file into a string buffer, perform the required changes and then write it back to the file. Unfortunately, it all becomes one line.
                    String text = "";
    		try
                    {
    		    BufferedReader in = new BufferedReader(new FileReader(path));
    		    String line; //a line in the file
    		    while ((line = in.readLine()) != null)
    		    {
    		    	text = text + line;
    		    }
    		    in.close();
    		} catch (Exception e)
    		{
    		    e.printStackTrace();
    		}
    		StringBuffer buff = new StringBuffer(text);
    		
                    //perform required changes
    
    		try
    		{
    			PrintWriter out = new PrintWriter(path);
    			out.write(buff.toString());
    			out.close();
    		}
    		catch (Exception e)
    		{
    			e.printStackTrace();
    		}
    

    Any ideas how to get the line breaks back?
    Thanks,
    Kev


Comments

  • Moderators, Science, Health & Environment Moderators Posts: 10,079 Mod ✭✭✭✭marco_polo


    When you use readline() it returns everything up to but not including the newline or carraige return characters at the end.

    The easiest way to get back the line breaks is to alter the following line like so:
    text = text + line "\r\n";
    

    You need both the carraige return and the newline character to get a line break, I can't remember the exact reason for this but I think it is because the carraige return character on its own means to go back to the start of the current line or something like that.


  • Registered Users Posts: 427 ✭✭Kevo


    Excellent.
    Thanks a mil.


  • Subscribers Posts: 4,076 ✭✭✭IRLConor


    This:
    String lineEnding = System.getProperty("line.separator");
    .
    .
    .
    text += line + lineEnding;
    

    is a slightly better way of adding the line endings. Unfortunately line endings are not the same across operating systems. Wikipedia explains.

    Short version is:

    "\r\n" -> Windows
    "\n" -> UNIX, Linux, OSX
    "\r" -> Mac OS9 and earlier


  • Moderators, Science, Health & Environment Moderators Posts: 10,079 Mod ✭✭✭✭marco_polo


    IRLConor wrote: »
    This:
    String lineEnding = System.getProperty("line.separator");
    .
    .
    .
    text += line + lineEnding;
    

    is a slightly better way of adding the line endings. Unfortunately line endings are not the same across operating systems. Wikipedia explains.

    Short version is:

    "\r\n" -> Windows
    "\n" -> UNIX, Linux, OSX
    "\r" -> Mac OS9 and earlier

    Ah now I remember the reason for the "\r\n". :)


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    Why do it that way? It makes the code messy. Better way is to use BufferedReader readline method and PrintWriter to output the data. Uses less memory as well (once gc kicks in I guess). Unless your replacing stuff across a newline it is the better way.


  • Advertisement
  • Subscribers Posts: 4,076 ✭✭✭IRLConor


    Hobbes wrote: »
    Why do it that way? It makes the code messy. Better way is to use BufferedReader readline method and PrintWriter to output the data. Uses less memory as well (once gc kicks in I guess). Unless your replacing stuff across a newline it is the better way.

    Yeah, that would be better. Good point.


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    Just to expand on the earlier message. What I meant was to use the readline and printWriter in the same loop, rather then creating a huge string.


  • Moderators, Science, Health & Environment Moderators Posts: 10,079 Mod ✭✭✭✭marco_polo


    Hobbes wrote: »
    Just to expand on the earlier message. What I meant was to use the readline and printWriter in the same loop, rather then creating a huge string.

    Unfortunately the OP never mentions if the changes are being made can be done line by line or not. I suppose the next best way would be to append to a StringBuffer in the loop instead of using the string concat operator.


Advertisement