
|
|
6. File Input and Output
- 9:00-10:00am -- File Input/Output in OS/2 C/C++ and Java
- 10:00-10:30am -- Using VisualAge for Java Effectively
- 10:30am-12:30pm -- LAB -- Converting a complete application
- 12:30-1:30pm -- Lunch
- 1:30-4:00pm -- LAB -- Converting a complete application (continued)
- 4:00-4:45pm -- Threads
- 4:45-5:00pm -- Wrapup
- Two main ways of doing File I/O in OS/2 C:
- Standard "C" file I/O APIs
- OS/2 file I/O APIs
- Maintains extended attributes for files
- Specifying fail action for open requests
- Return special values if file existed or was created
- DosOpen
- DosRead
- DosWrite
- DosResetBuffer
- DosDevIOCtl
- DosSetFilePtr
- DosSetFileInfo
- DosDupHandle
- DosClose
- DosOpen
- Open a file with the specified mode.
- Can also specify things like
- what to do if the open fails
- whether or not a child process can inherit access
- DosClose
- Close the file when it is no longer used.
- DosRead
- Read a portion of a file into memory from the position of the file pointer.
- DosWrite
- Write a block of memory to a file at the position of the file pointer.
- DosResetBuffer
- Force a flush of the file's buffers to the disk.
- DosDevIOCtl
- Perform a control operation on a file or on a device which acts as a file.
- DosSetFileInfo
- Set Extended Attributes for a file.
- DosSetFilePtr
- Seek a particular position in the file as specified in a parameter.
- DosDupHandle
- Duplicate a handle for a file so it can be used by another process or thread.
- File objects.
- Write bytes, primitives, and text to streams.
- Read bytes, primitives, and text from streams.
- Stream filters combined to form sophisticated streams.
- Random access I/O.
- Reading from URLs.
- "Least common denominator"
- Some platform-specifics not available
- May need to use native methods or Runtime.exec()
- System defines
static PrintStream out;
static PrintStream err;
- Method println used to print text to standard out and standard err.
- System also has:
static InputStream in;
- Method read reads a byte (-1 upon EOF).
- Use File objects to learn info about a file.
- File objects are not the actual streams.
- Can:
- tell you if a file exists.
- tell you if it's read/write protected.
- tell you if it's a directory.
- tell you if it's an absolute path specification.
- tell you when it was last modified.
- make directory.
- rename the file.
- list directory.
- delete the file.
- Example, find length of file:
File f = new File("info");
if ( f.exists() ) {
long len = f.length();
}
- List directory:
File f = new File("examples");
String[] dir = f.list();
- Java provides a byte stream to which filters may be attached.
- Streams are created from File objects or strings.
- Filters are combined to create more sophisticated streams.
- Creating subclasses (compile-time elements) is less flexible.
- Would have to create all interesting permutations.
- Similar to "pipes" in a shell:
c:\> echo 34 | FilterOne | ... | LastFilter
- Used to write char or bytes in binary.
byte[] someBytes = {'a', 'b', 'c'};
FileOutputStream f;
try {
f = new FileOutputStream("junk");
f.write(someBytes);
f.close();
}
catch (IOException io) {
...
}
- Used to write items of primitive type.
- Filter attached to another stream.
FileOutputStream f =
new FileOutputStream("junk");
DataOutputStream df =
new DataOutputStream(f);
df.writeInt(34);
df.close(); // flushes and closes f
- Used to print text.
FileOutputStream f =
new FileOutputStream("junk");
PrintStream p = new PrintStream(f);
p.println(34);
p.print("a string w/o newline");
p.println(3.14159);
p.close();
- Used to buffer an output stream.
FileOutputStream f =
new FileOutputStream("junk");
BufferedOutputStream bf;
bf = new BufferedOutputStream(f);
bf.write(34); // treat like FileOutputStream
- Java provides a byte stream to which filters may be attached.
- Streams are created from File objects or strings.
- Read bytes from a stream.
byte[] buffer = new byte[1024];
FileInputStream f;
try {
f = new FileInputStream("junk");
int b = f.read();
f.read(buffer); // read <= 1024 bytes
f.close();
}
catch (IOException e) {
...
}
- Read items of primitive types, portably stored in binary (Big-Endian).
FileInputStream f =
new FileInputStream("junk");
DataInputStream df;
df = new DataInputStream(f);
int i = df.readInt();
String s = df.readLine();
df.close();
- Read bytes from a string rather than file.
String junk = "begin\n34\nend\n";
StringBufferInputStream s;
s = new StringBufferInputStream(junk);
DataInputStream df = new DataInputStream(s);
String b = df.readLine(); // b is "begin"
String n = df.readLine(); // n is "34"
String e = df.readLine(); // e is "end"
- Use RandomAccessFile like combined DataOutputStream and
DataInputStream.
- Created from File objects or strings.
- Mode constructor argument as in C/C++ fopen.
RandomAccessFile f;
f = new RandomAccessFile("junk", "rw");
f.writeInt(34);
f.seek(0);
int i = f.readInt(); // i is 34
- Can read from remote system via URL:
InputStream in;
byte[] buffer = new byte[100];
URL url =
new URL("http://www.MageLang.com/");
in = url.openStream();
DataInputStream df = new DataInputStream(in);
System.out.println(df.readLine());
df.close();
- Use openConnection to write, interact.
- Can communicate with remote file systems using a client/server model.
- Server listens for connection requests from clients.
- Clients know how to connect to the server via a port number ("published phone number").
- Upon connection, server processes the request coming across the "channel" from the client.
- Can remain open or shut down after each transaction.
- Socket objects are read/write channels between hosts. The following elements make a
Socket connection unique:
- IP address of server.
- IP address of client.
- Port number for server.
- Port number for client.
- Transfer protocol.
- Create ServerSocket attached to port number
ServerSocket s = new ServerSocket(aPort,15);
- Wait for connections from clients requesting connections to that port.
Socket c = s.accept();
- Get input, output streams associated w/socket.
out = new PrintStream(c.getOutputStream());
in = new DataInputStream(c.getInputStream());
- Create Socket object attached to machine, port.
Socket c = new Socket(host, aPort);
- When the constructor returns, you have a connection.
Get input, output streams associated with socket.
in = new DataInputStream(c.getInputStream());
out = new PrintStream(c.getOutputStream());
- Can read/write to socket, thus, communicating with the server or client.
- Server talks to client:
out.println("Watson! Come here...");
String data = in.readLine();
- Client talks to server:
String data = in.readLine();
out.println("I heard you over socket!");
- Must spawn thread for each connected client.
- Pass input/output streams to client handler.
- Client handler implements protocol between client and server.
|