Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Carriage return in java

  • 24-04-2008 11:19AM
    #1
    Registered Users, Registered Users 2 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,093 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, Registered Users 2 Posts: 427 ✭✭Kevo


    Excellent.
    Thanks a mil.


  • Subscribers Posts: 4,077 ✭✭✭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,093 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, Registered Users 2 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,077 ✭✭✭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, Registered Users 2 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,093 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