Skip to content

Commit dcef6c4

Browse files
author
xiachaochao
committed
123
1 parent 43f60f6 commit dcef6c4

File tree

4 files changed

+77
-12
lines changed

4 files changed

+77
-12
lines changed

JavaBase/src/com/xinan/io/StreamCopyDemo.java

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,23 @@
88
import java.io.InputStream;
99
import java.io.OutputStream;
1010

11-
1211
public class StreamCopyDemo {
13-
public static void main(String[] args) throws IOException{
14-
File srcFile = new File("file/stream.txt");//创建源和目标
15-
File destFile=new File("file/stream_copy.txt");
16-
InputStream in=new FileInputStream(srcFile);//创建输入的管道(输入流和输出对象)
17-
OutputStream out=new FileOutputStream(destFile);
18-
byte[] buffer=new byte[10];//创建容量为10的缓冲区(存储已经读取的字节数据)
19-
int len=-1;//表示已经读取了多少个字节数,
20-
while((len=in.read(buffer))!=-1){
21-
out.write(buffer,0,len);
12+
public static void main(String[] args) throws IOException {
13+
File srcFile = new File("01a.wmv");// 创建源和目标
14+
File destFile = new File("copy.wmv");
15+
try {
16+
InputStream in = new FileInputStream(srcFile);// 创建输入的管道(输入流和输出对象)
17+
OutputStream out = new FileOutputStream(destFile);
18+
byte[] buffer = new byte[10000];// 创建容量为10的缓冲区(存储已经读取的字节数据)
19+
int len = -1;// 表示已经读取了多少个字节数,
20+
while ((len = in.read(buffer)) != -1) {
21+
out.write(buffer, 0, len);
22+
}
23+
in.close();// 关闭管道
24+
out.close();
25+
System.out.println("copy ok");
26+
} catch (IOException e) {
27+
e.printStackTrace();
2228
}
23-
in.close();//关闭管道
24-
out.close();
2529
}
2630
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.xinan.io01;
2+
3+
import java.io.Serializable;
4+
5+
public class Person implements Serializable {
6+
private static final long serialVersionUID = 1L;
7+
private int personId;
8+
private String name;
9+
private String pass;
10+
11+
public Person() { // 无参构造器
12+
}
13+
14+
public Person(int personId, String name, String pass) { // 有参构造器
15+
this.personId = personId;
16+
this.name = name;
17+
this.pass = pass;
18+
}
19+
public String toString(){
20+
return "personId"+personId+"\tname"+name+"\tpass"+pass;
21+
}
22+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.xinan.io01;
2+
3+
import java.io.ObjectInputStream;
4+
5+
public class ReadObject {
6+
public static void main(String[] args){
7+
try{
8+
9+
ObjectInputStream ois=new ObjectInputStream(fis);
10+
}
11+
catch(){
12+
13+
}
14+
}
15+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.xinan.io01;
2+
3+
import java.io.FileOutputStream;
4+
import java.io.IOException;
5+
import java.io.ObjectOutputStream;
6+
7+
public class WriteObject {
8+
public static void main(String[] args){
9+
try{
10+
11+
FileOutputStream fos=new FileOutputStream("hello");
12+
//System.out.println();
13+
ObjectOutputStream oos=new ObjectOutputStream(fos);
14+
oos.writeObject(new Person(1,"刘能","1234"));
15+
oos.writeObject(new Person(2,"赵四","2345"));
16+
oos.close();//关闭流操作
17+
System.out.println("copy ok了,大傻子");
18+
}
19+
catch(IOException e){
20+
e.printStackTrace();
21+
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)