FileInputStream 读取文件数据的输入字节流

  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 }*/
时间: 2024-08-09 14:38:24

FileInputStream 读取文件数据的输入字节流的相关文章

R语言读取文件数据

R语言读取文件数据 ??1.read.table()函数 2.其他函数的缺省 read.csv(file, header = TRUE, sep = ",", quote="\"", dec=".",fill = TRUE, ...)read.csv2(file, header = TRUE, sep = ";", quote="\"", dec=",",fill =

使用Java FileInputStream读取文件内容到字节数组中

package date0802; import java.io.FileInputStream; import java.io.IOException; public class InputStream { @SuppressWarnings("resource") public static void main(String[] args) throws IOException { FileInputStream fileInputStream = new FileInputStr

Python读取文件数据

1题目要求: 文本文件有这些数据,需要的只有其中的5个属性,如下颜色标记 像以下的数据达到75万组: 1product/productId: B0000UIXZ4 2product/title: Timex Link USB Watch 3product/price: unknown 4review/userId: A14MVG2I9PS6NZ 5review/profileName: B. Kuiper "Wah" 6review/helpfulness: 0/0 7review/s

FileInputStream读取文件

1:read() : 从输入流中读取数据的下一个字节,返回0到255范围内的int字节值.如果因为已经到达流末尾而没有可用的字节,则返回-1.在输入数据可用.检测到流末尾或者抛出异常前,此方法一直阻塞. InputStream.read()这个方法是从流里每次只读取读取一个字节,效率会非常低. 更好的方法是用InputStream.read(byte[] b)或者InputStream.read(byte[] b,int off,int len)方法,一次读取多个字节. 2:read(byte[

解决FileInputStream 读取文件中文乱码问题(转)

当Java中使用 FileInputStream 读取txt等文档时,中文会产生乱码,解决方法如下: try { fis = new FileInputStream(file); InputStreamReader reader = new InputStreamReader(fis,"GBK"); //最后的"GBK"根据文件属性而定,如果不行,改成"UTF-8"试试 BufferedReader br = new BufferedReader

Java IO流 之 FileInputStream 读取文件

http://www.verejava.com/?id=1699461971466 package com.io; import java.io.*; public class TestInputStream { public static void main(String[] args) { InputStream is=null; try { //建立了跟文件 english.txt 的连接 is=new FileInputStream(new File("res/english.txt&q

C语言之文件操作07——读取文件数据并计算均值方差标准差

//文件 /* =============================================================== 题目:从文本文件"high.txt"中取出运动员的身高数据,并计算平均值,方差和标准差! =============================================================== */ #include<stdio.h> #include <math.h> #define hh pr

BufferedInputStream 缓冲输入字节流 -------上

1 package com.BufferedInputStreamUse; 2 3 import java.io.BufferedInputStream; 4 import java.io.File; 5 import java.io.FileInputStream; 6 import java.io.FileNotFoundException; 7 import java.io.IOException; 8 9 /* 10 我们清楚读取文件数据使用缓冲数组读取效率更高,sun也知道使用缓冲数组

HDFS读文件过程分析:读取文件的Block数据

转自http://shiyanjun.cn/archives/962.html 我们可以从java.io.InputStream类中看到,抽象出一个read方法,用来读取已经打开的InputStream实例中的字节,每次调用read方法,会读取一个字节数据,该方法抽象定义,如下所示:public abstract int read() throws IOException;Hadoop的DFSClient.DFSInputStream类实现了该抽象逻辑,如果我们清楚了如何从HDFS中读取一个文件