Results 1 to 2 of 2
Thread: Scanner class
- 11-16-2007, 12:18 PM #1
Member
- Join Date
- Nov 2007
- Posts
- 2
- Rep Power
- 0
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 ?
- 11-26-2007, 07:01 AM #2
Member
- Join Date
- Nov 2007
- Posts
- 8
- Rep Power
- 0
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
-
Using Scanner class to read int value
By Java Tip in forum Java TipReplies: 1Last Post: 02-07-2009, 02:47 AM -
Getting tokens using Scanner class
By Java Tip in forum Java TipReplies: 0Last Post: 02-05-2008, 09:11 AM -
Using Scanner class to read int
By Java Tip in forum Java TipReplies: 0Last Post: 01-18-2008, 11:50 AM -
JDK 5.0 Scanner Class
By Sircedric88 in forum New To JavaReplies: 3Last Post: 07-27-2007, 06:55 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks