Writing to files in java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • abhishekphukan
    New Member
    • Jul 2014
    • 34

    Writing to files in java

    In this program I have everything running perfectly but in the addRecord() function I need to add more information. But I am not able to print it on a new line.
    Everything is displaying in the same line.

    Can someone point me out on that.

    Code:
    import java.io.*;
    import java.lang.*;
    import java.util.*;
    public class createfile {
    	public static void main(String args[]){
         
    		createfile obj= new createfile();
    		obj.openfile();
    		obj.addRecords();
    		obj.closefile();
    		System.out.println("You have created a file");
    	
    	}
    	private Formatter x;
    	public void openfile(){
    		
    		try{
    			x = new Formatter("C:\\test\\chinese.txt");
    		}
    		catch(Exception e){
    			System.out.println("You have an error");
    		}
    	}
    	
    	public void addRecords(){
    		x.format("%s %s %s","21","alex","software programmer");
    		x.format("%s %s %s", "22","greg", "architecture");
    	}
    	public void closefile(){
    		x.close();
    	}
    }

    Thank You..
    Last edited by Frinavale; Jan 12 '15, 06:02 PM. Reason: Moved the question above the posted code to give the code context. Please use [code] and [/code] tags when posting code or formatted data.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    To print a "new line" in java you need to use the escape code: \n.

    Check out this documentation on Characters but especially the section on Escape Sequences.

    -Frinny

    Comment

    Working...