13.07.2015 Views

I/O Fundamentals

I/O Fundamentals

I/O Fundamentals

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Java Input and Output/** A sample of creating an indexed random access file */public class CreateAddressBook {public static void main(String[] args) {try {// create the index filePrintWriter index = new PrintWriter(new FileWriter("phone.idx"));// create the phone data fileRandomAccessFile data =new RandomAccessFile("phone.dat","rw");// write a few sample recordswriteIt(data, index, "Ernie","123 Sesame Street, Apt E","MuppetVille", "PBS", 12345,5, "123-4567");writeIt(data, index, "Cookie Monster","123 Sesame Street, Apt C","MuppetVille", "PBS", 12345,3, "COO-KIES");writeIt(data, index, "Fred Rogers","111 Trolley Lane","Neighborhood", "MakeBelive", 22222,78, "BMY-NABR");}// close the index and the data fileindex.close();data.close();}catch(Exception e) {e.printStackTrace();}/** Write our information to a data file and create* an index entry*/public static void writeIt(RandomAccessFile data,PrintWriter index,String name,String street,String city,String state,int zip,int age,String phone)throws IOException {AddressData d = new AddressData();d.setName(name);d.setStreet(street);Java Input and Output -46© 1996-2003 jGuru.com. All Rights Reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!