Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Write to New lines in a file

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Write to New lines in a file

    Hello Gurus,
    I'm experimenting (learning) how to write text data to a file. I could not write text in new lines. I thought of append "\n" to each line but that does not looks good because there is every possibility of forgetting to append this. Hence i'm looking for a method similar to println method of System class. Following is the code snippet that i have written and that should explain my pain.
    Looking forward to have constructive comments.

    package LearningExamples;
    import java.io.File;
    import java.io.FileWriter;
    import java.io.BufferedWriter;
    import java.io.IOException;
     
    public class Write2File {
     
    	public Write2File(){
    		System.out.println("Write2File Constructor was called");
    	}
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		try{
    			File file= new File("/home/ganesh/MyFile.txt");
    			FileWriter fw = new FileWriter(file);
    			BufferedWriter bw = new BufferedWriter(fw);
    			bw.write("Testing file writing!");
    			bw.write("next line");
    			bw.close();
    		}
    		catch(IOException ioe){
    			System.out.println("IOException was caught!");
    			ioe.printStackTrace();
    		}
     
    	}
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Write to New lines in a file

    Can you explain what the problem with the posted code is?

    append "\n" to each line but that does not looks good
    Please explain what "does not look good" means. It sounds like you are talking about how programs are normally written so that they can handle all the possible combinations of data.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Write to New lines in a file

    The code is a compiled code without any errors.
    I would append "\n" like below, but my point is that programer may forget to append this and that may lead to unwanted issues.
    Hence, i'm looking for any available method like BufferReader.writeln(String str), so that the chances of using this function is more and then relatively less chances to avoid the problem that i'm mentioning.
    bw.write("Testing file writing!\n");
    bw.write("next line\n");

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Write to New lines in a file

    There are methods like println() that add the newline characters at the end of String when it is output.
    Use that method.

    programer may forget to append
    That's often called a program bug. It happens all the time in many different contexts.

    The BufferedWriter class has a newLine() method.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Write to New lines in a file

    I agree with your comment, such progrmming bugs can't be prevented and we are always trying to deplete the volume of such bugs by providing better API's.
    In our C++ project, i did override cout with a Log class. Consumers of this object are able to happily redirect the error messages to log file, each message in a new line. I think that such thing can be surely done in Java and i thought that it's a well known use case it's already available in java's API.
    Using "\n" is easier than Calling BufferedWriter.newLine() in 2 ways
    1. Usability
    2. No method calling Overhead.
    However, thanks a ton for letting me know the existance of newLine() method. I'm a C++ programmer and just started learning Java. Hence every piece of information about java is an interesting object for me.
    I accidentally but fortunately found this forum and i hope that i will learn many things about java by following this forum. I wish that i can also help other programmers one day.

    This thread can be marked closed.

Similar Threads

  1. Reading lines from a txt file
    By neliJav in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 6th, 2013, 01:54 PM
  2. reading a certain amount of lines from a file?
    By welikedogs in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 3rd, 2010, 01:28 PM
  3. Reading a lines from text file
    By kiran in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: September 26th, 2010, 10:54 PM
  4. [SOLVED] reading only certain lines from a .txt file
    By straw in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: March 7th, 2010, 07:49 PM
  5. Reading from a file, multiple lines
    By MysticDeath in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: October 15th, 2009, 02:40 AM