File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ helloworld
Original file line number Diff line number Diff line change 22
33import java .io .File ;
44import java .io .FileInputStream ;
5+ import java .io .FileNotFoundException ;
6+ import java .io .FileOutputStream ;
7+ import java .io .IOException ;
58import java .io .InputStream ;
9+ import java .io .OutputStream ;
610
711public 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}
You can’t perform that action at this time.
0 commit comments