{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Intro to Java Web-Notes_Part98

Intro to Java Web-Notes_Part98 - bac<2...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: bac<2++; Sys:em.out.println("BR — lst: " + backl + " 2nd: " + back2); br.close(); bac<l = 0; bac<2 = 0; DataInputStream dis : new DataInputStream(new FilelnputStream("dos.txt")); bac<l : dis.readInt(); bac<l””; dis.readlnt(); bac<2 = dis.readlnt(); bac<2 ; Sys:em.out.println("DIS — lst: " + backl + " 2nd: " + back2); dis.close(); File f = new File("dos.txt"); long len = f.length(); RandomAccessFi’e ras : new RandomAccessFile("dos.txt", "r"); for (’ong i : ’en — 4; i >: 0; i —: 4) { ras.seek(i); int back = :as.readlnt(); back++; System.out.println("Backwards: " + back); } } The above example determines the length of the file Via a File object. The length can also be determined Via a RandomAccessFile object, but I wanted to show how to create and use a File object. File objects are not used to access or modify the contents of a file -- File objects report information about the file itself, such as its length, its path, whether or not it actually exists, is it readable or writable. You can also delete the file Via a File object (as well as create new files). Reading and Writing Objects It is also possible to read and write objects to a file. The following example creates three Employee objects and writes them to a binary file named people. dat: import java.io.*; import java.util.*; public class WriteObjs { public static void maid(String args) throws Exception { Employee tom : new Employee("cruise", 111000, 46); Employee bill : new Employee("clinton", 99000, 59); Employee shawn : new Employee("colvin", 85000, 32) I ObjectOutputStream oos = new ObjectOutputSt:eam(new FileOutputStream("people.dat")); oos.writeObject(tom); oos.writeObject(bill); oos.writeObject(shawn); oos.close(); } class Employee implements Serializable { private String name; D priva-e priva-e - salary : 500000; - age 7 30; D public a p Oyee(String Hamel int salary' int age) { this.na e = name; :his.sa’ary : salary; :his.age : age; } public String toString() { return name + ", " + salary + ", " + age; } The people. dat file is 136 bytes long. It contains the three Employee objects, written to the file one after another (preceded by a description of the Employee class). Here is a crude interpretation of people. dat’s binary data as characters. This code pulls the three Employee objects from the peopledat file and into memory: impor’ impor’ java.io.*; java.u:il.*; public class KeadObjs { public sta:ic void main(String args) throws Exception { Employee e1, e2, e3; ObjectlnputStream ois = new ObjectlnputStream(new FilelnputStream("people.dat")); el : (Employee) ois.readObject(); e? = (?mp'oyee) ois.readObject(); e3 = (?mp'oyee) ois.readObject(); ois.close(); Sys-em.ou-.prin-’n(el); Sys-em.ou-.prin-’n(e7); System.out.print1n(e3); The above code created three Employee references, used those to reference three Employee objects read from the people. a’at file, then printed out each read object's String to the console: cruise, 111000, 46 Clinton, 99000, 59 colvin, 85000, 32 Unfortunately, writing and reading the objects individually requires the programmer to know (or figure out) exactly how many objects are in the file and then read that exact number back into memory later on. It is much easier to put the objects into some kind of collection object (in this case an ArrayList), then just write the array to disk with one write statement. The array will be written to disk along with all of the objects that it references: import java.io.*; import java.util.*; public class WriteManyObjs { public static void main(String args) throws Exception { Company ibm : new Company(); ...
View Full Document

{[ snackBarMessage ]}