Professional Documents
Culture Documents
Chapter 8 IO
Chapter 8 IO
El Dick 1
Input/Output
2
I/O
Streams and files M. El Dick
M. El Dick M. El Dick
The File Class
5
M. El Dick 6 M. El Dick
M. El Dick M. El Dick
Using Path Names
9
UNIX :
/user/smith/home.work/java/FileDemo.java
10 How to write/read?
Windows :
D:\Work\Java\Programs\FileDemo.java
When a backslash is used in a quoted string it must be
written as two backslashes since backslash is the
escape character:
"D:\\Work\\Java\\Programs\\FileDemo.java"
M. El Dick M. El Dick
M. El Dick M. El Dick
Close the stream Stream
13 14
M. El Dick M. El Dick
Stream Buffering
15 16
M. El Dick M. El Dick
Java I/O classes The FileWriter Class
17 18
Too many
4 categories based on class suffix : Methods write(int character) write(char[] s) write(string s)
append (int character)
Byte Character flush()
M. El Dick M. El Dick
import java.io.*;
public class CharacterFileOutput {
To append at the end of an existing file:
M. El Dick M. El Dick
Example Console input
25 26
import java.io.*;
To read input from the console, associate a stream with
public class CharacterFileInput {
the standard input System.in
public static void main (String args[]){
import java.io.*;
File f = new File("File.txt");
public class ConsoleInput {
char buffer[]; public static void main (String args[]){
try { String line;
FileReader in = new FileReader(f); try {
buffer = new char [(int) f.length()]; BufferedReader in = new BufferedReader(new
in.read(buffer); InputStreamReader(System.in));
System.out.println(buffer); while ((line = in.readLine()) != null)
in.close(); System.out.println(line);
in.close();
} catch (IOException e) {
} catch (IOException e) {
e.printStackTrace();
e.printStackTrace();
}}} }
}
M. El Dick M. El Dick
}
More efficient!
No encoding or decoding required
Portability
M. El Dick M. El Dick
Binary I/O Classes The FileOutputStream Class
33 34
M. El Dick M. El Dick
import java.io.*;
public class ByteFileOutput { Methods int read() (one byte)
public static void main (String args[]){ byte[] read(byte[] buffer)
int[] p = { 1, 2, 3, 5, 7, 11, 19, 23}; long skip(long n)
try {
DataOutputStream s = new DataOutputStream(new
FileOutputStream ("primes.bin"));
Construction File f = new File(“temp.dat”);
for (int i = 0; i<p.length; i++)
FileInputStream w = new FileInputStream (f);
s.writeInt(p[i]);
s.close();
FileInputStream w = new FileInputStream
} catch (IOException e) {
(“temp.dat”);
e.printStackTrace();
}
}
M. El Dick M. El Dick
}
Reads bytes from stream and converts them into primitive type
M. El Dick values or strings as M.
appropriate
El Dick
Example Example: Student grades
41 42
import java.io.*;
Problem:
public class CharacterFileOutput {
public static void main (String args[]){ Write student names and grades to a binary file
try { Read the data back from the file
File f = new File ("primes.bin");
DataInputStream s = new DataInputStream (new FileInputStream
(f));
int size = (int) f.length()/ 4; // 4 bytes per int
for (int i = 0; i<size; i++){
int p = s.readInt();
System.out.println(p);
}
s.close();
} catch (IOException e) {
e.printStackTrace();
M. El Dick M. El Dick
}}}
43 44
M. El Dick M. El Dick
Example: Copying Files
45 46
M. El Dick M. El Dick
M. El Dick M. El Dick
Random-Access Files
49 50
Two modes
“r” read-only
“rw” read and write
M. El Dick M. El Dick
M. El Dick M. El Dick
Exercise 1: Converting text into UTF
53 54
M. El Dick M. El Dick
Exercise 2:
File textfile = new File ("f1.txt");
File binfile = new File ("f2.bin");
try{
55 BufferedReader br = new BufferedReader(new FileReader(textfile)); 56
DataOutputStream dos = new DataOutputStream (new
FileOutputStream(binfile)); Write a program that removes from a text file
String line;
int i=0; all the occurrences of a string that is given as a
while((line =br.readLine())!=null){
System.out.println(i+" "+line); parameter
dos.writeUTF(line);
i++; Hint: Use the I/O classes that are character-
}
br.close();
oriented.
dos.close();
System.out.println("The text file size is "+textfile.length());
System.out.println("The binary file size is "+binfile.length());
}
catch(IOException e){
e.printStackTrace();
}
}
M. El Dick M. El Dick
Exercise 3: Exercise 4:
57 58
M. El Dick M. El Dick