Results 1 to 2 of 2

Thread: Write to File

  1. #1
    Join Date
    Jan 2010
    Posts
    80
    Rep Power
    0

    Default Write to File

    Hi

    I have a little problem

    In this code, the programme writes to a file. but the problem is in the red line.

    "\n" doesn't work. I need each record of student in one line.

    Java Code:
    import java.io.*;
    import java.util.*;
    
    public class StudentsRecords {
    	public static void main(String[] args){
    		
    		Scanner scan = new Scanner(System.in);
    		
    		try{
    			
    		FileWriter fstream = new FileWriter("C://Users//dell//Desktop//G E N E R A L//J A V A//Records.txt");
            BufferedWriter out = new BufferedWriter(fstream);
            
            int size=0;
    		System.out.print("Enter number of students: ");
    		size = scan.nextInt();
    		
    		String name="";
    		int mark=0;
    		for(int i=0; i<size; i++){
    			System.out.print("Enter Name: ");
    			name = scan.next();
    			System.out.print("Enter mark: ");
    			mark = scan.nextInt();
    			[COLOR="Red"]out.write((i+1)+" "+ name + " "+ mark + "\n");[/COLOR]
    			System.out.println();
    		}
    		out.close();
    		
    		}catch(Exception e){
    			System.err.println("Error: " + e.getMessage());
    		}
    	}
    }

  2. #2
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    10

    Default

    you can try escaping the "\n" to "\\n", or you can use newLine() method
    Java Code:
    out.newLine();

Similar Threads

  1. Replies: 6
    Last Post: 12-01-2010, 05:31 AM
  2. write from cmd to a file
    By idbirohit in forum New To Java
    Replies: 11
    Last Post: 11-26-2010, 04:23 AM
  3. Replies: 8
    Last Post: 05-12-2010, 07:19 PM
  4. Replies: 5
    Last Post: 02-05-2009, 10:28 AM
  5. [SOLVED] File Write in File
    By Eku in forum New To Java
    Replies: 4
    Last Post: 10-27-2008, 04:01 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
  •