Skip to content

Commit c87edc8

Browse files
committed
IO
1 parent 91c413a commit c87edc8

File tree

2 files changed

+200
-5
lines changed

2 files changed

+200
-5
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package io;
2+
3+
import java.io.BufferedReader;
4+
import java.io.Console;
5+
import java.io.IOException;
6+
import java.util.Arrays;
7+
8+
/**
9+
*
10+
* @author chengfeili
11+
* Jun 14, 2017 3:18:58 PM
12+
*
13+
* The Console class is a singleton.
14+
*
15+
* This is a bug #122429 of eclipse
16+
*
17+
*/
18+
public class IteractingWithUsers {
19+
20+
public void readLineTest() throws IOException {
21+
Console console = System.console();
22+
if (console == null) {
23+
throw new RuntimeException("Console not available");
24+
} else {
25+
console.writer().print("How excited are you about your trip today? ");
26+
console.flush();
27+
String excitementAnswer = console.readLine();
28+
String name = console.readLine("Please enter your name: ");
29+
Integer age = null;
30+
console.writer().print("What is your age? ");
31+
console.flush();
32+
BufferedReader reader = new BufferedReader(console.reader());
33+
String value = reader.readLine();
34+
age = Integer.valueOf(value);
35+
console.writer().println();
36+
console.format("Your name is " + name);
37+
console.writer().println();
38+
console.format("Your age is " + age);
39+
console.printf("Your excitement level is: " + excitementAnswer);
40+
}
41+
}
42+
43+
/**
44+
* By disabling echoing, the user does not see the text they are typing.
45+
*
46+
* The readPassword() method returns an array of characters instead of a
47+
* String. String values are added to a shared memory pool for performance
48+
* reasons in Java. This means that if a password that a user typed in were
49+
* to be returned to the process as a String , it might be available in the
50+
* String pool long after the user entered it.
51+
*/
52+
public void readPasswordTest() {
53+
Console console = System.console();
54+
if (console == null) {
55+
throw new RuntimeException("Console not available");
56+
} else {
57+
char[] password = console.readPassword("Enter your password: ");
58+
console.format("Enter your password again: ");
59+
console.flush();
60+
char[] verify = console.readPassword();
61+
boolean match = Arrays.equals(password, verify);
62+
// Immediately clear passwords from memory
63+
// Arrays.fill(password, 'x');
64+
for (int i = 0; i < password.length; i++) {
65+
password[i] = 'x';
66+
}
67+
for (int i = 0; i < verify.length; i++) {
68+
verify[i] = 'x';
69+
}
70+
console.format("Your password was " + (match ? "correct" : "incorrect"));
71+
}
72+
}
73+
74+
public static void main(String[] args) {
75+
76+
}
77+
}

Java-8/src/io/StreamsContinue.java

Lines changed: 123 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
11
package io;
22

3-
import java.io.*;
3+
import java.io.BufferedInputStream;
4+
import java.io.BufferedOutputStream;
5+
import java.io.BufferedReader;
6+
import java.io.BufferedWriter;
7+
import java.io.EOFException;
8+
import java.io.File;
9+
import java.io.FileInputStream;
10+
import java.io.FileOutputStream;
11+
import java.io.FileReader;
12+
import java.io.FileWriter;
13+
import java.io.IOException;
14+
import java.io.InputStream;
15+
import java.io.ObjectInputStream;
16+
import java.io.ObjectOutputStream;
17+
import java.io.OutputStream;
18+
import java.io.Serializable;
419
import java.util.ArrayList;
520
import java.util.List;
621

@@ -16,8 +31,7 @@ public class StreamsContinue {
1631
public void fileInputOutputStream() throws IOException {
1732
File source = new File("Zoo.class");
1833
File destination = new File("ZooCopy.class");
19-
try (InputStream in = new FileInputStream(source);
20-
OutputStream out = new FileOutputStream(destination)) {
34+
try (InputStream in = new FileInputStream(source); OutputStream out = new FileOutputStream(destination)) {
2135
int b;
2236
while ((b = in.read()) != -1) {
2337
out.write(b);
@@ -59,7 +73,7 @@ public void fileReaderWiter() throws IOException {
5973
}
6074

6175
// BufferedReader
62-
public static List<String> readFile(File source) throws IOException {
76+
public List<String> readFile(File source) throws IOException {
6377
List<String> data = new ArrayList<String>();
6478
try (BufferedReader reader = new BufferedReader(new FileReader(source))) {
6579
String s;
@@ -71,15 +85,119 @@ public static List<String> readFile(File source) throws IOException {
7185
}
7286

7387
// BufferedWriter
74-
public static void writeFile(List<String> data, File destination) throws IOException {
88+
public void writeFile(List<String> data, File destination) throws IOException {
7589
try (BufferedWriter writer = new BufferedWriter(new FileWriter(destination))) {
7690
for (String s : data) {
7791
writer.write(s);
7892
}
7993
}
8094
}
8195

96+
// Deserialize
97+
public List<Animal> getAnimals(File dataFile) throws IOException, ClassNotFoundException {
98+
List<Animal> animals = new ArrayList<Animal>();
99+
try (ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(dataFile)))) {
100+
while (true) {
101+
Object object = in.readObject();
102+
if (object instanceof Animal)
103+
animals.add((Animal) object);
104+
}
105+
} catch (EOFException e) {
106+
// File end reached
107+
}
108+
return animals;
109+
}
110+
111+
// Serialize
112+
public static void createAnimalsFile(List<Animal> animals, File dataFile) throws IOException {
113+
try (ObjectOutputStream out = new ObjectOutputStream(
114+
new BufferedOutputStream(new FileOutputStream(dataFile)))) {
115+
for (Animal animal : animals)
116+
out.writeObject(animal);
117+
}
118+
}
119+
82120
public static void main(String[] args) throws IOException {
83121

84122
}
85123
}
124+
125+
/**
126+
* Any class can implement the Serializable interface since there are no
127+
* required methods to implement.
128+
*
129+
* Note that the requirement for properly marking an object as Serializable may
130+
* involve nested objects.
131+
*
132+
* Therefore, any object references contained within the Tail class must belong
133+
* to classes that are also marked as Serializable , and so on.
134+
*
135+
* Besides transient instance variables, static class members will also be
136+
* ignored during the serialization and deserialization process.
137+
*
138+
* Why Not Mark Every Class as Serializable ?
139+
*
140+
* The reason that we do not is that there are some classes that we want to
141+
* instruct the JVM not to serialize. In particular, process-heavy classes such
142+
* as the Thread class or any of the Stream classes would be diffi cult, often
143+
* impossible, to save to persistent storage, since much of their work involves
144+
* managing JVM processes or resources in real time.
145+
*
146+
*/
147+
class Animal implements Serializable {
148+
private static final long serialVersionUID = 1L;
149+
private String name;
150+
private int age;
151+
private char type;
152+
153+
public Animal(String name, int age, char type) {
154+
this.name = name;
155+
this.age = age;
156+
this.type = type;
157+
}
158+
159+
public String getName() {
160+
return name;
161+
}
162+
163+
public int getAge() {
164+
return age;
165+
}
166+
167+
public char getType() {
168+
return type;
169+
}
170+
171+
public String toString() {
172+
return "Animal [name=" + name + ", age=" + age + ", type=" + type + "]";
173+
}
174+
}
175+
176+
class Animal1 implements Serializable {
177+
private static final long serialVersionUID = 2L;
178+
private transient String name;
179+
private transient int age = 10;
180+
private static char type = 'C';
181+
{
182+
this.age = 14;
183+
}
184+
185+
public Animal1() {
186+
this.name = "Unknown";
187+
this.age = 12;
188+
this.type = 'Q';
189+
}
190+
191+
public Animal1(String name, int age, char type) {
192+
this.name = name;
193+
this.age = age;
194+
this.type = type;
195+
}
196+
// Same methods as before
197+
198+
// [Animal [name=null, age=0, type=P], Animal [name=null, age=0, type=P]]
199+
/*
200+
* As expected, you can see that the values for name and age are lost on
201+
* serialization and not set again during deserialization.
202+
*/
203+
}

0 commit comments

Comments
 (0)