1 package com.outputstream;
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.util.Arrays;
10
11 /*
12 输出字节流:
13
14 --------| OutputStream 是所有输出字节流 的父类。 抽象类
15 -----------| FileOutStream 向文件输出数据的输出字节流。
16
17 FileOutputStream如何使用呢?
18 1. 找到目标文件
19 2. 建立数据的输出通道。
20 3. 把数据转换成字节数组写出。
21 4. 关闭资源
22
23 FileOutputStream要注意的细节:
24 1. 使用FileOutputStream 的时候,如果目标文件不存在,那么会自动创建目标文件对象。
25 2. 使用FileOutputStream写数据的时候,如果目标文件已经存在,那么会先清空目标文件中的数据,然后再写入数据。
26 3.使用FileOutputStream写数据的时候, 如果目标文件已经存在,需要在原来数据基础上追加数据的时候应该使用new FileOutputStream(file,true)构造函数,第二参数为true。
27 4.使用FileOutputStream的write方法写数据的时候,虽然接收的是一个int类型的数据,但是真正写出的只是一个字节的数据,只是
28 把低八位的二进制数据写出,其他二十四位数据全部丢弃。
29 00000000-000000000-00000001-11111111 511
30 11111111---> -1
31 */
32 //使用字节数组把数据写出。
33 /*class Write{
34 public static void writeTest() throws IOException{
35 //找到目标文件
36 File file = new File("D://aa.txt");
37 //创建目标文件
38 file.createNewFile();
39 //建立数据通道
40 FileOutputStream inputStream= new FileOutputStream(file,true);
41 String str = "\r\n你好,你在干嘛";//如果想存入数据进行换行的话加一个"\n\r"既可以
42 //把字符串转换为字符数组
43 inputStream.write(str.getBytes());
44 inputStream.close();
45
46 }
47 }
48
49 public class Demo1 {
50
51 public static void main(String[] args) throws IOException {
52 // TODO Auto-generated method stub
53
54 Write write = new Write();
55 write.writeTest();
56 }
57
58 }
59 */
60
61
62 /*4.使用FileOutputStream的write方法写数据的时候,虽然接收的是一个int类型的数据,但是真正写出的只是一个字节的数据,只是
63 把低八位的二进制数据写出,其他二十四位数据全部丢弃。
64 00000000-000000000-00000001-11111111 511
65 */
66
67
68 // 写入的一各方法
69 class Write{
70 public static void writeTest(){
71
72 File file = new File("D://bb.txt");
73 FileOutputStream fileOutputStream = null;
74 try {
75 fileOutputStream = new FileOutputStream(file);
76 fileOutputStream.write(511);
77 } catch (FileNotFoundException e) {
78 e.printStackTrace();
79 }catch (IOException e){
80 e.printStackTrace();
81 }finally{
82 if(fileOutputStream != null){
83 try {
84 fileOutputStream.close();
85 } catch (IOException e) {
86 e.printStackTrace();
87 }
88 }
89 }
90
91 }
92 }
93 // 读取数据的一个方法
94 class Read extends Write{
95 public static void readTest(){
96 Write write = new Write();
97 write.writeTest();
98 File file = new File("D://bb.txt");
99 InputStream inputStream = null;
100 try {
101 inputStream = new FileInputStream(file);
102 byte[] bs = new byte[4];
103 int length = inputStream.read(bs);
104 System.out.println("内容是,"+Arrays.toString(bs));
105
106 } catch (FileNotFoundException e) {
107 e.printStackTrace();
108 } catch (IOException e){
109 e.printStackTrace();
110 }finally{
111 if(inputStream != null){
112 try {
113 inputStream.close();
114 } catch (IOException e) {
115 // TODO Auto-generated catch block
116 e.printStackTrace();
117 }
118 }
119 }
120
121 }
122 }
123 public class Demo1 {
124
125 public static void main(String[] args) throws IOException {
126
127 Read read = new Read();
128 read.readTest();
129 }
130 }