1 package com.inputstream;
2
3 /*
4 File类: 用于描述一个文件或者文件夹的。
5
6 通过File对象我们可以读取文件或者文件夹的属性数据,如果我们需要读取文件的内容数据,那么我们需要使用IO流技术。
7
8 IO流(Input Output)
9
10 IO流解决问题: 解决设备与设备之间的数据传输问题。 内存--->硬盘 硬盘--->内存
11 IO流技术:
12 IO流分类:
13 如果是按照数据的流向划分:
14 输入流
15 输出流
16 如果按照处理的单位划分:
17 字节流: 字节流读取得都是文件中二进制数据,读取到二进制数据不会经过任何的处理。
18 字符流: 字符流读取的数据是以字符为单位的 。 字符流也是读取文件中的二进制数据,不过会把这些二进制数据转换成我们能 识别的字符。
19 字符流 = 字节流 + 解码
20 输入字节流:
21 --------| InputStream 所有输入字节流的基类 抽象类
22 ------------| FileInputStream 读取文件数据的输入字节流
23 使用FileInputStream读取文件数据的步骤:
24 1. 找到目标文件
25 2. 建立数据的输入通道。
26 3. 读取文件中的数据。
27 4. 关闭 资源.
28 */
29 import java.io.File;
30 import java.io.FileInputStream;
31 import java.io.FileNotFoundException;
32 import java.io.IOException;
33 import java.io.InputStream;
34 /**
35 * 字节流
36 * @author Administrator
37 *
38 */
39 //方式2 : 使用循环读取文件的数据
40 /*class Inputtest{
41 public static void inputRead1(String path){
42 File file = new File("E://aa.txt");
43 InputStream inputStream = null;
44 try {
45 inputStream = new FileInputStream(file);
46 int str =0;
47 while((str = inputStream.read())!=-1){
48 System.out.print((char)str);
49 }
50 } catch (FileNotFoundException e) {
51 e.printStackTrace();
52 }catch (IOException e){
53 e.printStackTrace();
54 }finally{
55 if(inputStream != null){
56 try {
57 inputStream.close();
58 } catch (IOException e) {
59 e.printStackTrace();
60 }
61 }
62 }
63 }
64 }
65
66 public class Demo1 {
67
68 public static void main(String[] args) {
69 Inputtest inputtest = new Inputtest();
70 inputtest.inputRead1("E://aa.txt");
71 }
72 }*/
73
74
75
76 //方式3:使用缓冲 数组 读取。 缺点: 无法读取完整一个文件的数据。
77 /*class inputTest{
78 public static void inputRead2(){
79 File file =new File("E://aa.txt");
80 InputStream inputStream = null;
81 try {
82 inputStream = new FileInputStream(file);
83 byte[] bs = new byte[1024];
84 int length = inputStream.read(bs);
85 String str = new String(bs,0,length);
86 System.out.println("内容是:");
87 System.out.println(str);
88 } catch (FileNotFoundException e) {
89 e.printStackTrace();
90 }catch (IOException e){
91 e.printStackTrace();
92 }finally{
93 if(inputStream != null){
94 try {
95 inputStream.close();
96 } catch (IOException e) {
97 e.printStackTrace();
98 }
99 }
100 }
101 }
102 }
103 public class Demo1 {
104
105 public static void main(String[] args) {
106 inputTest inputTest = new inputTest();
107 inputTest.inputRead2();
108 }
109 }*/
110
111
112
113
114 //方式4:使用缓冲数组配合循环一起读取。
115 class inputTest{
116 public static void inputRead3(){
117 //找到目标文件
118 File file = new File("E://aa.txt");
119 InputStream inputStream = null;
120 try {
121 //建立数据的输入通道
122 inputStream = new FileInputStream(file);
123 //建立缓冲数组配合循环读取文件的数据。
124 int length = 0;//保存每次读取到的字节个数。
125 byte[] bs = new byte[1024];//存储读取到的数据 缓冲数组 的长度一般是1024的倍数,因为与计算机的处理单位。 理论上缓冲数组越大,效率越高
126 while((length = inputStream.read(bs))!=-1){// read方法如果读取到了文件的末尾,那么会返回-1表示。
127 String str = new String(bs,0,length);
128 System.out.println("内容是"+"\n"+str);
129 }
130 } catch (FileNotFoundException e) {
131 e.printStackTrace();
132 } catch (IOException e){
133 e.printStackTrace();
134 }finally{
135 if(inputStream != null){
136 try {
137 inputStream.close();
138 } catch (IOException e) {
139 // TODO Auto-generated catch block
140 e.printStackTrace();
141 }
142 }
143 }
144
145 }
146 }
147
148 public class Demo1 {
149
150 public static void main(String[] args) {
151 inputTest inputTest = new inputTest();
152 inputTest.inputRead3();
153 }
154 }
155
156
157
158
159 /*public class Demo1 {
160
161 public static void main(String[] args) {
162 // TODO Auto-generated method stub
163
164 //File file = new File("E://aa.txt");
165 Inputtest inputtest = new Inputtest();
166 File[] files = Inputtest.inputRead("E://aa.txt");
167
168 }
169
170 }
171
172
173 class Inputtest{
174 public static File[] inputRead(String path){
175 File file = new File("E://aa.txt");
176 File[] files = file.listFiles();
177 FileInputStream fileInputStream = null;
178 try {
179 fileInputStream = new FileInputStream(file);
180 int str=0;
181
182 while(str != -1){
183 str = fileInputStream.read();
184 System.out.print((char)str);
185 }
186 } catch (FileNotFoundException e) {
187 e.printStackTrace();
188 }catch(IOException e){
189 e.printStackTrace();
190 }finally{
191 if(fileInputStream != null){
192 try {
193 fileInputStream.close();
194 } catch (IOException e) {
195 e.printStackTrace();
196 }
197 }
198 }
199 return files;
200 }
201 }*/