Skip to content

Commit 127b72d

Browse files
author
crazy
committed
1
1 parent 599d8e2 commit 127b72d

2 files changed

Lines changed: 25 additions & 15 deletions

File tree

JavaBase/poweroff.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
helloworld

JavaBase/src/com/it520/io01/FileInputStreamDemo.java

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,34 @@
22

33
import java.io.File;
44
import java.io.FileInputStream;
5+
import java.io.FileNotFoundException;
6+
import java.io.FileOutputStream;
7+
import java.io.IOException;
58
import java.io.InputStream;
9+
import java.io.OutputStream;
610

711
public class FileInputStreamDemo {
8-
public static void main(String[] args) throws Exception{
9-
//1.创建目标对象(表示数据保存到哪一个文件)
10-
File f1=new File("file/stream.txt");
11-
12-
//2.创建水管
13-
InputStream input=new FileInputStream(f1);//InputStream和OutputStream不能直接new
14-
//3.具体操作
15-
byte[] b1=new byte[5];//
16-
int len=-1;
17-
len=input.read(b1);
12+
public static void main(String[] args) throws IOException{
13+
//定义一个file对象
14+
File f1=new File("poweroff.txt");
15+
File f2=new File("copy.txt");
16+
System.out.println(f1.getAbsolutePath());
17+
//定义流对象
18+
InputStream fis=new FileInputStream(f1);
19+
OutputStream fos=new FileOutputStream(f2);
20+
//流操作
21+
byte[] b1=new byte[5];//数组中需要定义长度
22+
int len=0;
23+
//len=fis.read(b1);
1824
while(len!=-1){
19-
String str=new String(b1,0,len);
20-
System.out.println(str);
25+
len=fis.read(b1);
26+
//String str=new String(b1,0,len);
27+
//System.out.println(str);
28+
fos.write(b1, 0, len);
2129
}
22-
//4.关闭操作
23-
input.close();
30+
System.out.println();
31+
//关闭流操作
32+
fis.close();
33+
fos.close();
2434
}
25-
2635
}

0 commit comments

Comments
 (0)