-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample190.java
More file actions
54 lines (51 loc) · 2.74 KB
/
Example190.java
File metadata and controls
54 lines (51 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Example 190 from page 151
//
import java.io.*;
class Example190 {
public static void main(String[] args)
throws IOException, ClassNotFoundException {
// Write numbers, words etc on file "f.txt" in human-readable form:
PrintWriter pwr = new PrintWriter(new FileWriter("f.txt"));
pwr.print(4711); pwr.print(' '); pwr.print("cool"); pwr.close();
// Read numbers and words from human-readable text file "f.txt":
StreamTokenizer stok = new StreamTokenizer(new FileReader("f.txt"));
int tok = stok.nextToken();
while (tok != StreamTokenizer.TT_EOF)
{ System.out.println(stok.sval); tok = stok.nextToken(); }
// Write primitive values to a binary file "p.dat":
DataOutputStream dos = new DataOutputStream(new FileOutputStream("p.dat"));
dos.writeInt(4711); dos.writeChar(' '); dos.writeUTF("cool"); dos.close();
// Read primitive values from binary file "p.dat":
DataInputStream dis = new DataInputStream(new FileInputStream("p.dat"));
System.out.println(dis.readInt()+"|"+dis.readChar()+"|"+ dis.readUTF());
// Write an object or array to binary file "o.dat":
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("o.dat"));
oos.writeObject(new int[] { 2, 3, 5, 7, 11 }); oos.close();
// Read objects or arrays from binary file "o.dat":
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("o.dat"));
int[] ia = (int[])(ois.readObject());
System.out.println(ia[0]+","+ia[1]+","+ia[2]+","+ia[3]+","+ia[4]);
// Read and write parts of file "raf.dat" in arbitrary order:
RandomAccessFile raf = new RandomAccessFile("raf.dat", "rw");
raf.writeDouble(3.1415); raf.writeInt(42);
raf.seek(0); System.out.println(raf.readDouble() + " " + raf.readInt());
// Read from a String s as if it were a text file:
Reader r = new StringReader("abc");
System.out.println("abc: " + (char)r.read() + (char)r.read() + (char)r.read());
// Write to a StringBuilder as if it were a text file:
Writer sw = new StringWriter();
sw.write('d'); sw.write('e'); sw.write('f');
System.out.println(sw.toString());
// Write characters to standard output and standard error:
System.out.println("std output"); System.err.println("std error");
// Read characters from standard input (the keyboard):
System.out.print("Type some characters and press Enter: ");
BufferedReader bisr = new BufferedReader(new InputStreamReader(System.in));
String response = bisr.readLine();
System.out.println("You typed: '" + response + "'");
// Read a byte from standard input (the keyboard):
System.out.print("Type one character and press Enter: ");
byte b = (byte)System.in.read();
System.out.println("First byte of your input is: " + b);
}
}