Java Application Hosting Web Hosting, website hosting, web site hosting , web page hosting Apache, PHP, MySQL, PERL, servlets Java, JSP  Java Application Hosting Web Hosting website hosting, web site hosting, web page hosting Apache, PHP, MySQL, PERL, servlets Java, JSP,Python Java Application Hosting Web Hosting website hosting, web site hosting, web page hosting Apache, PHP, MySQL, PERL, servlets Java, JSP,Python Java Application Hosting Web Hosting website hosting, web site hosting, web page hosting Apache, PHP, MySQL, PERL, servlets Java, JSP,Python Java Application Hosting Web Hosting website hosting, web site hosting, web page hosting, Apache, PHP, MySQL, PERL, servlets Java, JSP,Python
Java Application Hosting Web Hosting, website hosting, web site hosting, web page hosting, Apache, PHP, MySQL, PERL, servlets Java, JSP, Python Java Application Hosting Web Hosting, website hosting, web site hosting, web page hosting, Apache, PHP, MySQL, PERL, servlets Java, Python,JSP
Java Application Hosting Web Hosting Sign-Up Java Application Hosting Fund Raising, Fundraising, web hosting, website hosting, web site hosting  Java Application Hosting Resellers web hosting, website hosting, web site hosting Java Application Hosting EZ Site Control Panel for web hosting,website hosting, web site hosting
Java Application Hosting Web Hosting, website hosting, web site hosting , web page hosting Apache, PHP, MySQL, PERL, servlets Java, Python,JSP,  Fundraising
Java Application Hosting Fund Raising, Fundraising, web hosting, website hosting, web site hosting
WWW.

Call Us Toll-Free
(877) 256-0328

Outside USA
1 - (201) 505-0430

Java Application Hosting Welcome Java Application Hosting Web Hosting Plans Overview , Fund Raising, Fundraising, web hosting, website hosting, web site hosting Java Application Hosting Fund Raising, Fundraising, web hosting Java Application Hosting Resellers, web Hosting Java Application Hosting Web Design, web Hosting Java Application Hosting Extra Services,  web Hosting Java Application Hosting Traffic Booster, web hosting Java Application Hosting Traffic Booster, web hosting Java Application Hosting Technical Support,  web Hosting Java Application Hosting webmaster tips,  web Hosting Java Application Hosting 30 Day Money Back, web hosting Java Application Hosting Legal Notices for Web Hosting Java Application Hosting Glossary Computer Terms for web Hosting Java Application Hosting Contact Information - web hosting

Site Map
Java Application Hosting Web Hosting, website hosting, web site hosting , web page hosting Apache, PHP, MySQL, PERL, servlets Java, Python, JSP Java Application Hosting Java Application Hosting Java Application Hosting Java Application Hosting Java Application Hosting Answers to Questions and Exercises: Basic I/O (The Java™ Tutorials > Essential Classes > Basic I/O)
Trail: Essential Classes
Lesson: Basic I/O
Home Page > Essential Classes > Basic I/O
Answers to Questions and Exercises: Basic I/O

Questions

Question 1. What class would you use to read a few pieces of data that are at known positions near the end of a large file?
Answer 1. RandomAccessFile.

Question 2.In a format call, what's the best way to indicate a new line?
Answer 2. Use the %n conversion — the \n escape is not platform independent!

Question 3. How would you append data to the end of a file? Show the constructor for the class you would use and explain your answer.
Answer 3. Here's a quick answer: Use the FileWriter and BufferedWriter classes to append data to the end of the text file. Here is the FileWriter constructor, you pass in true to write to the file in append mode:

FileWriter writer = new FileWriter (String filename, boolean append);
An alternate answer is to use RandomAccessFile and skip to the end of the file and start writing:

...
RandomAccessFile file = new RandomAccessFile(datafile, "rw");
file.skipBytes((int)file.length()); //skip to the end of the file
file.writeBytes("Add this text to the end of datafile"); //write at the end of the file
file.close();
...


Exercises

Exercise 1. Implement a pair of classes, one Reader and one Writer, that count the number of times a particular character, such as e, is read or written. The character can be specified when the stream is created. Write a program to test your classes. You can use xanadu.txt as the input file.
Answer 1. See the following three files: CounterDemo.java, CountReader.java, and CountWriter.java.

Exercise 2. The file datafile begins with a single long that tells you the offset of a single int piece of data within the same file. Using the RandomAccessFile class, write a program that gets the int piece of data. What is the int data?
Answer 2. 123. See FileReader.java for the solution. If you're interested in seeing how the file was written, see FileWriter.java.


Previous page: Questions and Exercises: Basic I/O
 
 
 

Add to My Yahoo!

XML icon

Add to Google

 

 

 

 

 

 

 

 

 

 

 

JSP Servlets Tomcat mysql Java JSP Servlets Tomcat mysql Java JSP Servlets Tomcat mysql Java JSP Servlets Tomcat mysql Java JSP at JSP.aldenWEBhosting.com Servlets at servlets.aldenWEBhosting.com Tomcat at Tomcat.aldenWEBhosting.com mysql at mysql.aldenWEBhosting.com Java at Java.aldenWEBhosting.com Web Hosts Portal Web Links Web Links Web Hosting JSP Solutions Web Links JSP Solutions Web Hosting Servlets Solutions Web Links Servlets Solutions Web Hosting Web Links Web Links . . .
.
.
. .
.
. .
. . . . . . . . . . . jsp hosting servlets hosting web hosting web sites designed cheap web hosting web site hosting myspace web hosting