IO流---文件字节流,字符流,文件拷贝
字节流操作四大步骤:
1,创建源
2,选择流
3,执行操作
4,通知关闭资源
FileInPutStream类:字节输入流
从文件中读入信息:
1 package cn.ftf.io;
2
3 import java.io.FileInputStream;
4 import java.io.FileNotFoundException;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.io.OutputStream;
9
10 /**
11 * 四个步骤,分段读取
12 * 1.创建源
13 * 2,选择流
14 * 3,操作
15 * 4,释放资源
16 *
17 * @author 房廷飞
18 */
19
20
21 public class TestIO05 {
22 public static void main(String[] args) {
23 //创建源
24 File src=new File("D:\\myjavacode\\JavaPractise01\\src\\cn\\ftf\\io\\abc.txt");
25 InputStream is = null; 为了方便关闭流,都是这样写的
26 try { //try-catch处理异常,都是这样
27 is=new FileInputStream(src); //实例化字节流对象
28 byte[]flush=new byte[1024]; //做一个数组,做缓冲容器,提高效率,数组容量是一次读取的字节数量
29 int len=-1; //用于接收字节长度
30 while((len=is.read(flush))!=-1) { //.read(字节数组),读一定的长度,可查api文档
31 String str=new String(flush,0,len); //解码,由字符数组构建字符串
32 System.out.println(str);
33 }
34
35 } catch (FileNotFoundException e) {
36 // TODO Auto-generated catch block
37 e.printStackTrace();
38 } catch (IOException e) {
39 // TODO Auto-generated catch block
40 e.printStackTrace();
41 }finally { //关闭字节流,都是这样写的
42 try {
43 if(null!=is) {
44 is.close();
45 }
46 } catch (IOException e) {
47 // TODO Auto-generated catch block
48 e.printStackTrace();
49 }
50 }
51 }
52 }
字节输出流:FileOutputStream
将信息写到文件
1 package cn.ftf.io;
2
3 import java.io.File;
4 import java.io.FileNotFoundException;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7 import java.io.OutputStream;
8
9 //一切四步骤:1,创建源2,选择流3,操作4,释放资源
10
11 public class TestIO05{
12 public static void main(String[] args) {
13 //创建源
14 File file=new File("D:\\myjavacode\\JavaPractise01\\src\\cn\\ftf\\io\\test002.txt");
15 //选择流
16 OutputStream os=null;
17
18 //操作
19 try {
20 os=new FileOutputStream(file,true); //实例化输出流对象,//默认是刷新写,跟写的话加true (file,true)
21 String msg="hello 房廷飞!!!";
22 byte[] bt= msg.getBytes(); //编码,将字符串转化为字节数组,可查api文档查到
23 try {
24 os.write(bt); // 将字节数组写入文件
25 os.flush(); //写完清空缓存,养成习惯,都这样写的
26 } catch (IOException e) {
27 // TODO Auto-generated catch block
28 e.printStackTrace();
29 }
30 } catch (FileNotFoundException e) {
31 // TODO Auto-generated catch block
32 e.printStackTrace();
33
34 }
35 finally { //通知关闭资源,标准格式
36 try {
37 if(os!=null) {
38 os.close();
39 }
40 }catch (IOException e) {
41 // TODO Auto-generated catch block
42 e.printStackTrace();
43 }
44 }
45 }
46 }
文件拷贝:字节输入流,输出流合二为一
1 package cn.ftf.io;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.FileOutputStream;
7 import java.io.IOException;
8 import java.io.InputStream;
9 import java.io.OutputStream;
10
11 public class CopyFile {
12
13 public static void main(String[] args) {
14 copyFile("D:/myjavacode/JavaPractise01/src/cn/ftf/io/abc.txt","C:/Users/user/Desktop/hello.py");
15 }
16 public static void copyFile(String first,String second) {
17 //1,创建源
18 File file1=new File(first);
19 File file2=new File(second);
20 //2,选择流
21 InputStream str1=null;
22 OutputStream str2=null;
23
24 //3,执行操作
25 try {
26 str1=new FileInputStream(file1);
27 str2=new FileOutputStream(file2);
28 byte[] flush=new byte[1024];
29 try {
30 while(str1.read(flush)!=-1) {
31 str2.write(flush);
32 str2.flush();
33 }
34 } catch (IOException e) {
35 // TODO Auto-generated catch block
36 e.printStackTrace();
37 }
38 } catch (FileNotFoundException e) {
39 // TODO Auto-generated catch block
40 e.printStackTrace();
41 }
42 //4,关闭资源 //分别关闭,先打打开的后关闭
43 if(str2!=null) {
44 try {
45 str2.close();
46 } catch (IOException e) {
47 // TODO Auto-generated catch block
48 e.printStackTrace();
49 }
50 }
51 if(str1!=null) {
52 try {
53 str1.close();
54 } catch (IOException e) {
55 // TODO Auto-generated catch block
56 e.printStackTrace();
57 }
58 }
59 }
60 }
至于字符流也是大同小异,同样是标准四步骤,直接由字节流就可以改写来。
由字节流拷贝文件改写为字符流拷贝文本文件:
1 package cn.ftf.io;
2 /**
3 * 字节流和字符流大同小异
4 * 在copyfile的例子下改写成copytext,字节流改写成字符流
5 */
6 import java.io.File;
7 import java.io.FileNotFoundException;
8 import java.io.FileReader;
9 import java.io.FileWriter;
10 import java.io.IOException;
11 import java.io.Reader;
12 import java.io.Writer;
13
14 public class CopyText {
15
16 public static void main(String[] args) {
17 copyFile("D:/myjavacode/JavaPractise01/src/cn/ftf/io/abc.txt","C:/Users/user/Desktop/hello.txt");
18 }
19 public static void copyFile(String first,String second) {
20 //1,创建源
21 File file1=new File(first);
22 File file2=new File(second);
23 //2,选择流
24 Reader str1=null;
25 Writer str2=null;
26
27 //3,执行操作
28 try {
29 str1=new FileReader(file1);
30 try {
31 str2=new FileWriter(file2);
32 } catch (IOException e1) {
33 // TODO Auto-generated catch block
34 e1.printStackTrace();
35 }
36 char[] flush=new char[1024];
37 try {
38 while(str1.read(flush)!=-1) {
39 str2.write(flush);
40 str2.flush();
41 }
42 String msg="hello ftf!!!";
43 str2.append(msg);
44 } catch (IOException e) {
45 // TODO Auto-generated catch block
46 e.printStackTrace();
47 }
48 } catch (FileNotFoundException e) {
49 // TODO Auto-generated catch block
50 e.printStackTrace();
51 }
52 //4,关闭资源 //分别关闭,先打打开的后关闭
53 if(str2!=null) {
54 try {
55 str2.close();
56 } catch (IOException e) {
57 // TODO Auto-generated catch block
58 e.printStackTrace();
59 }
60 }
61 if(str1!=null) {
62 try {
63 str1.close();
64 } catch (IOException e) {
65 // TODO Auto-generated catch block
66 e.printStackTrace();
67 }
68 }
69 }
70 }