File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes first JavaRanch question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "first JavaRanch question" Watch "first JavaRanch question" New topic
Author

first JavaRanch question

Jesse Crockett
Ranch Hand

Joined: Feb 03, 2005
Posts: 129
Hi, I have a question that should be simple. I'm working on a small program for an introductory class. The program takes a string and prints to a file with Scanner and PrintWriter. I only need to print the contents of the file in the command screen before closing the program. The textbook says it can be done with FileReader and FileWriter. Here is my guess:



But it doesn't work. How do I print the contents of the file?
Maximilian Xavier Stocker
Ranch Hand

Joined: Sep 20, 2005
Posts: 381
You need to get each line of the file from your scanner as a String.

Look at the Javadoc API for Scanner for a method that would do this.

Then take that String and print it.
Jesse Crockett
Ranch Hand

Joined: Feb 03, 2005
Posts: 129
The Scanner method to take a line as a string is nextLine();



I get an error "No Line Found"

Does it matter that the file name is order.out?
Jesse Crockett
Ranch Hand

Joined: Feb 03, 2005
Posts: 129
I found the problem. It was all to do with the place of the line to close the file:



I had to close the file before using FileReader.
Jesse Crockett
Ranch Hand

Joined: Feb 03, 2005
Posts: 129
Another question: why does the font size vary between code snippets? I'm copying from the same file, but the font size is too small in some of the posts...
Maximilian Xavier Stocker
Ranch Hand

Joined: Sep 20, 2005
Posts: 381
Originally posted by Jesse Crockett:
Another question: why does the font size vary between code snippets? I'm copying from the same file, but the font size is too small in some of the posts...


I am not sure what you mean. You mean you see different font sizes in posts here? I am not sure how that is even possible and it sounds like a buggy browser to me.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: first JavaRanch question