Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-16-2007, 02:18 PM
Member
 
Join Date: Nov 2007
Posts: 2
ajaymenon.k is on a distinguished road
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 ?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-26-2007, 09:01 AM
Member
 
Join Date: Nov 2007
Posts: 8
m_srikanth is on a distinguished road
I tried to run the same on my system and it did produce the output file. The code I used is :
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 }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting tokens using Scanner class Java Tip Java Tips 0 02-05-2008 11:11 AM
Using Scanner class to read int Java Tip Java Tips 0 01-18-2008 01:50 PM
Using Scanner class to read int value Java Tip Java Tips 0 12-03-2007 11:34 AM
JDK 5.0 Scanner Class Sircedric88 New To Java 3 07-27-2007 08:55 PM


All times are GMT +3. The time now is 11:19 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org