Results 1 to 2 of 2

Thread: Scanner class

  1. #1
    ajaymenon.k is offline Member
    Join Date
    Nov 2007
    Posts
    2
    Rep Power
    0

    Default Scanner class

    I am very new to java 5 .As a learning thing, i was writing some sample code on
    using scanner utilities to scan a file and see the output.

    After scanning, i tried writing the output to a file like this :

    File file = new File("E:///xanadu.txt");
    try {
    scanner = new Scanner(file);
    pw = new PrintWriter(new FileWriter("E:///scanneroutput.txt"));
    while (scanner.hasNextLine()) {
    String l = scanner.nextLine();
    //System.out.println(l);
    pw.print(l);
    }

    } catch (Exception e) {

    } finally {

    System.out.println("Out File Generated.");
    if (scanner != null) {
    scanner.close();
    }
    }

    But the output is not getting written to the file.But when i use S.O.P it is getting printed on the screen. I couldnt understand why this is happening ? Can anybody please help me out ?

  2. #2
    m_srikanth is offline Member
    Join Date
    Nov 2007
    Posts
    8
    Rep Power
    0

    Default

    I tried to run the same on my system and it did produce the output file. The code I used is :
    Java Code:
    import java.io.File;
    import java.io.FileWriter;
    import java.io.PrintWriter;
    import java.util.Scanner;
    
    public class ScannerTest {
    	public static void main(String[] args) {
    		
    	File file = new File("C:///YServer.txt");
    	Scanner scanner = null;
    	PrintWriter pw = null;
    	
    	try {
    		scanner = new Scanner(file);
    		pw = new PrintWriter(new FileWriter("C:///scanneroutput.txt"));
    		while (scanner.hasNextLine()) {
    			String l = scanner.nextLine();
    		//	System.out.println(l);
    			pw.print(l);
    		}
    
    	} catch (Exception e) {
    
    	} finally {
    
    		System.out.println("Out File Generated.");
    		if (scanner != null) {
    		scanner.close();
    		}
    	}
     }//end of main method
    }

Similar Threads

  1. Using Scanner class to read int value
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 02-07-2009, 02:47 AM
  2. Getting tokens using Scanner class
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-05-2008, 09:11 AM
  3. Using Scanner class to read int
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-18-2008, 11:50 AM
  4. JDK 5.0 Scanner Class
    By Sircedric88 in forum New To Java
    Replies: 3
    Last Post: 07-27-2007, 06:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •