Skip to content

Commit 7fd4d1e

Browse files
committed
提交初始源码
0 parents  commit 7fd4d1e

17 files changed

Lines changed: 1109 additions & 0 deletions

JavaIO.iml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5">
4+
<output url="file://$MODULE_DIR$/target/classes" />
5+
<output-test url="file://$MODULE_DIR$/target/test-classes" />
6+
<content url="file://$MODULE_DIR$">
7+
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
8+
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
9+
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
10+
<excludeFolder url="file://$MODULE_DIR$/target" />
11+
</content>
12+
<orderEntry type="inheritedJdk" />
13+
<orderEntry type="sourceFolder" forTests="false" />
14+
<orderEntry type="library" name="Maven: junit:junit:4.12" level="project" />
15+
<orderEntry type="library" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
16+
</component>
17+
</module>

pom.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>website.dengta</groupId>
8+
<artifactId>java-io</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
<dependencies>
11+
<dependency>
12+
<groupId>junit</groupId>
13+
<artifactId>junit</artifactId>
14+
<version>RELEASE</version>
15+
</dependency>
16+
</dependencies>
17+
18+
19+
</project>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package website.dengta.javaio;
2+
3+
import java.io.*;
4+
5+
/**
6+
* Created by Administrator on 2017/9/28.
7+
* <p>
8+
* 被Serializable接口声明的类的对象的属性都将被序列化,但是如果想自定义序列化的内容的时候,就需要实现Externalizable接口。
9+
* <p>
10+
* 当一个类要使用Externalizable这个接口的时候,这个类中必须要有一个无参的构造函数,如果没有的话,在构造的时候会产生异常,这是因为在反序列话的时候会默认调用无参的构造函数。
11+
*/
12+
public class ExternalizableDemo {
13+
public static void main(String[] args) throws Exception {
14+
ser(); // 序列化
15+
dser(); // 反序列话
16+
}
17+
18+
public static void ser() throws Exception{
19+
File file = new File("d:" + File.separator + "hello.txt");
20+
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(
21+
file));
22+
out.writeObject(new PersonE("rollen", 20));
23+
out.close();
24+
}
25+
26+
public static void dser() throws Exception{
27+
File file = new File("d:" + File.separator + "hello.txt");
28+
ObjectInputStream input = new ObjectInputStream(new FileInputStream(
29+
file));
30+
Object obj = input.readObject();
31+
input.close();
32+
System.out.println(obj);
33+
}
34+
35+
36+
/**
37+
* 当我们使用Serializable接口实现序列化操作的时候,如果一个对象的某一个属性不想被序列化保存下来,那么我们可以使用transient关键字进行说明:
38+
*/
39+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package website.dengta.javaio;
2+
3+
import java.io.*;
4+
5+
/**
6+
* Created by Administrator on 2017/9/28.
7+
*/
8+
public class ObjectInputStreamDemo {
9+
10+
public static void main(String[] args) throws IOException, ClassNotFoundException {
11+
File file = new File("d:" + File.separator + "hello.txt");
12+
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
13+
Object obj = ois.readObject();
14+
ois.close();
15+
System.out.println(obj);
16+
17+
}
18+
19+
//对象序列化的反序列化
20+
/**
21+
* 打印代码如下:Person{name='dengta', age=1}
22+
*/
23+
}
24+
25+
26+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package website.dengta.javaio;
2+
3+
import java.io.*;
4+
5+
/**
6+
* Created by Administrator on 2017/9/28.
7+
*/
8+
public class ObjectOutputStreamDemo {
9+
10+
public static void main(String[] args) throws IOException{
11+
File file = new File("d:"+File.separator+"hello.txt");
12+
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
13+
oos.writeObject(new Person("dengta",1));
14+
oos.close();
15+
}
16+
17+
/**
18+
* 二进制文件不能查看,可使用ObjectInputStream类来查看
19+
*/
20+
21+
22+
}
23+
24+
25+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package website.dengta.javaio;
2+
3+
import java.io.Serializable;
4+
5+
/**
6+
* Created by Administrator on 2017/9/28.
7+
*/
8+
public class Person implements Serializable{
9+
10+
private transient String name ;
11+
private int age;
12+
public Person() {
13+
}
14+
15+
public Person(String name, int age) {
16+
this.name = name;
17+
this.age = age;
18+
}
19+
20+
@Override
21+
public String toString() {
22+
return "Person{" +
23+
"name='" + name + '\'' +
24+
", age=" + age +
25+
'}';
26+
}
27+
28+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package website.dengta.javaio;
2+
3+
import java.io.Externalizable;
4+
import java.io.IOException;
5+
import java.io.ObjectInput;
6+
import java.io.ObjectOutput;
7+
8+
/**
9+
* Created by Administrator on 2017/9/28.
10+
*/
11+
public class PersonE implements Externalizable{
12+
13+
private String name ;
14+
private int age;
15+
16+
public PersonE() {
17+
}
18+
19+
public PersonE(String name, int age) {
20+
this.name = name;
21+
this.age = age;
22+
}
23+
24+
@Override
25+
public String toString() {
26+
return "PersonE{" +
27+
"name='" + name + '\'' +
28+
", age=" + age +
29+
'}';
30+
}
31+
32+
// 复写这个方法,根据需要可以保存的属性或者具体内容,在序列化的时候使用
33+
public void writeExternal(ObjectOutput out) throws IOException{
34+
out.writeObject(this.name);
35+
out.writeInt(age);
36+
}
37+
38+
// 复写这个方法,根据需要读取内容 反序列话的时候需要
39+
public void readExternal(ObjectInput in) throws IOException,
40+
ClassNotFoundException{
41+
this.name = (String) in.readObject();
42+
this.age = in.readInt();
43+
}
44+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package website.dengta.javaio;
2+
3+
import java.io.*;
4+
5+
/**
6+
* Created by Administrator on 2017/9/28.
7+
*/
8+
public class SerObjectDemo {
9+
public static void main(String[] args) throws Exception{
10+
Student[] stu = { new Student("hello", 20), new Student("world", 30),
11+
new Student("rollen", 40) };
12+
ser(stu);
13+
Object[] obj = dser();
14+
for(int i = 0; i < obj.length; ++i){
15+
Student s = (Student) obj[i];
16+
System.out.println(s);
17+
}
18+
}
19+
20+
// 序列化
21+
public static void ser(Object[] obj) throws Exception{
22+
File file = new File("d:" + File.separator + "hello.txt");
23+
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(
24+
file));
25+
out.writeObject(obj);
26+
out.close();
27+
}
28+
29+
// 反序列化
30+
public static Object[] dser() throws Exception{
31+
File file = new File("d:" + File.separator + "hello.txt");
32+
ObjectInputStream input = new ObjectInputStream(new FileInputStream(
33+
file));
34+
Object[] obj = (Object[]) input.readObject();
35+
input.close();
36+
return obj;
37+
}
38+
}
39+
40+
41+
class Student implements Serializable{
42+
public Student(){
43+
44+
}
45+
46+
public Student(String name, int age){
47+
this.name = name;
48+
this.age = age;
49+
}
50+
51+
@Override
52+
public String toString(){
53+
return "姓名: " + name + " 年龄:" + age;
54+
}
55+
56+
private String name;
57+
private int age;
58+
}
59+

0 commit comments

Comments
 (0)