JavaIO流分为输入流、输出流.也可以分为:字节流、字符流
FileInputStream 实现了在文件上的读取数据操作
IO流分类:
1如果是按照数据的流向划分:
(1) 输入流
(2) 输出流
2 如果按照处理的单位划分:
(1) 字节流: 字节流读取得都是文件中二进制数据,读取到二进制数据不会经过任何的处理。
(2) 字符流: 字符流读取的数据是以字符为单位的 。字符流也是读取文件中的二进制数据,不过会把这些二进制数据转换成我们能识别的字符。
字符流 = 字节流 + 解码
输入字节流:
——–| InputStream 所有输入字节流的基类 抽象类
————| FileInputStream 读取文件数据的输入字节流
出处:I/O流之进步认识、InputStream以及FileInputStream
FileInputStream API
1.常有构造函数:
FileInputStream(File file) ;//通过File对象来创建文件输入字节流
FileInputStream(String name);//通过文件系统中的路径名 name 来建立文件输入流对象
2. 常用函数:
int read()
从此输入流中读取一个数据字节。如果已到达文件末尾,则返回-1.
read( byte[] buf);//读取数据填充到字节数组buf 返回:读入缓冲区的字节总数,
如果因为已经到达文件末尾而没有更多的数据,则返回-1.
read( byte buf, int start, int size);//读取数据到字节数组buf
返回:
读入缓冲区的字节总数,如果因为已经到达文件末尾而没有更多的数据,则返回 -1。
void close(); //文件使用结束后要关闭文件。
例:int b = in.read(); //读取一个字节无符号填充到int低八位.-1是EOF
3. 实例
例1:读取文本文件中的每一个字节,并打印到控制台,每10个字节打印一行
import java.io.FileInputStream;
import java.io.IOException;
public class TestFileInputStream
{
public static void printfFile(String fileName) throws IOException
{
FileInputStream in=new FileInputStream(fileName);
int b;
int i=1;
while((b=in.read())!=-1)
{
if(b<=0xf)//如果是一位,就在前面补零
{
System.out.print("0");
}
System.out.print(Integer.toHexString(b)+" ");
if(i++%10==0)
{
System.out.println();
}
}
in.close();//关闭文件
}
public static void main(String[] args) throws IOException
{
printfFile("test.txt");
}
}
test.txt中的内容:(utf-8编码)
abcd中国小明
运行结果:
61 62 63 64 e4 b8 ad e5 9b bd
e5 b0 8f e6 98 8e
分析:utf-8编码中英文占一个字节,中文占3个字节。运行结果中的61 62 63 64,是abcd的十六进制utf-8编码,剩下的12个字节分别表示 中国小明 。
e4 b8 ad//中
e5 9b bd//国
e5 b0 8f//小
e6 98 8e//名
例2:读取一个图片中的每一个字节打印到控制台,每25行打印一次。这个例子和上面的例子差不多只需要稍微修改即可:
图片:
import java.io.FileInputStream;
import java.io.IOException;
public class TestFileInputStreamJPG
{
public static void main(String[] args) throws IOException
{
FileInputStream jpg=new FileInputStream("太极.jpg");
int b;
int i=1;
while((b=jpg.read())!=-1)
{
if(b<=0xf)//如果读到的是一位,在前面补零
{
System.out.print("0");
}
System.out.print(Integer.toHexString(b)+" ");//以十六进制方式打印
if(i++%25==0)
{
System.out.println();
}
}
jpg.close();
}
}
运行结果:
fd d7 ba df 9b f9 27 cf 90 a1 df 94 c3 39 b7 f2 94 b9 2a 4d b1 96 a0 c2 e6 33 17 c5 e2 b1 f8 9f f9 1f bf 75 ee ae 9f b1
f3 90 6f fc 95 4d 3e d6 c7 e5 32 87 6f 56 fd 81 cc 65 eb af 94 c8 70 2f 6e 05 ff 00 d7 b7 bf 75 ee 87 ee ab ae da d4 34
......//太多了,省略
d7 bd fb af 75 ef 7e eb dd 7b df ba f7 5e f7 ee bd d7 bd fb af 75 ef 7e eb dd 7b df ba f7 5e f7 ee bd d7 bd fb af 75 ef
7e eb dd 7b df ba f7 5e f7 ee bd d7 bd fb af 75 ef 7e eb dd 7b df ba f7 5e f7 ee bd d7 bd fb af 75 ef 7e eb dd 7f ff d9
可以看到读取图片和读取文本文件的方式差不多。
例3:读取test.txt的内容,使用read(byte[] buf);方法
代码:
public static void printfFile2(String fileName) throws IOException
{
FileInputStream in=new FileInputStream(fileName);
byte[] buf=new byte[10];
while(true)//每次读取10个字节到数组buf中
{
int flag=in.read(buf);//读取字节填满字节数组buf,并返回读取到的字节数
if(flag==-1)//如果返回的是-1表示已经读取到文件末尾,已经读取完毕
break;
//打印这十个字节
for (int i=0;i<buf.length;i++)
{
System.out.print(Integer.toHexString(buf[i]&0xff)+" ");
}
System.out.println();
}
in.close();//关闭文件
}
测试:
public static void main(String[] args) throws IOException
{
printfFile2("test.txt");
}
运行结果:
61 62 63 64 e4 b8 ad e5 9b bd
e5 b0 8f e6 98 8e ad e5 9b bd
分析:
二进制代码串中某一位与上1该位不变,如果该位与上0则该位被屏蔽,上面的代码中的:
System.out.print(Integer.toHexString(buf[i]&0xff)+" ");
Integer.toHexString(int i),方法的参数默认是int类型的,传入的buf[i]会自动类型转换成int类型,int类型占32位,这样打印的就是32位的16进制字符串,而我们读取到的字节只占8位,为了只显示读取到的这八位,我们可以把int类型的高24位屏蔽掉,剩下的低八位保留即可。具体的做法就是低八位与上1111 1111也就是十六进制的ff,即0xff.这样低八位保留下来,打印的结果就都是两位的了:
如果是:
System.out.print(Integer.toHexString(buf[i])+" ");
的话,打印的结果是:
例4:
int read(byte[] buf,int off,int len)
从buf数组中的偏移位置(下标)off,开始读取len个字节到buf数组中。
返回:
读入缓冲区的字节总数,如果因为已经到达文件末尾而没有更多的数据,则返回 -1。
实例:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class TestFileInputStreamRead
{
public static void printJPG(String filePath)
{
FileInputStream in=null;
try
{
in=new FileInputStream(filePath);
byte[] buf=new byte[1024];
int size=0;
//从buf字节数组的起始位置,开始读取字节到数组中,最多读满,并返回真正读取到的长度
while(size>-1)
{
size=in.read(buf, 0, buf.length);
for(int i=0;i<size;i++)
{
if(i%30==0&&i!=0)
System.out.println();
if((buf[i]&0xff)<=0xf)
System.out.print("0");
System.out.print(Integer.toHexString(buf[i]&0xff)+" ");
}
System.out.println("\n-----------size="+size+"--------------------");
}
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
finally {
if(in!=null)
{
try
{
in.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
}
public static void main(String[] args)
{
printJPG("太极.jpg");
// printJPG("test.txt");
}
}
运行结果:
0f b7 a5 cf 64 b6 f6 17 11 43 f7 94 75 bf f2 f6 dc 1f f2 e2 f7 ee bd d6 e5 1d 6f 95 cb e5
.....//略
8c 06 52 a7 f8 28 fb 2a df bd ff 00 72 b8 ff 00 7e eb dd 69 3f f2 d3 e5 0e 63 ac b0 35 38
fd bb b8 31
-----------size=1024--------------------
......//略
-----------size=1024--------------------
85 0e 47 33 98 86 0c 9d 35 56 5a b0 57 d7 95 ac 20 16 e2 e4 0b b5 87 fb 13 6f 7e eb dd 39
......//略
7e eb dd 7b df ba f7 5e f7 ee bd d7 bd fb af 75 ef 7e eb dd 7b df ba f7 5e f7 ee bd d7 bd
fb af 75 ef 7e eb dd 7f ff d9
-----------size=1000--------------------
-----------size=-1--------------------
可以把读取到的字节数组再转换成字符串,就可以正常的打印到控制台了。
实例:读取test.txt(utf-8编码),然后转换成字符串打印到控制台。
(1) 要求进行异常处理
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class TestFileInputStreamToString
{
public static String printTest(String filePath)
{
FileInputStream inputStream=null;
String string=null;
try
{
inputStream=new FileInputStream(filePath);
// 返回这个输入流中可以被读的剩下的bytes字节的估计值;
int size= inputStream.available();
byte[] buf=new byte[size];
inputStream.read(buf);//把文件中的字节读入到字节数组中
// 根据获取到的Byte数组转换成一个字符串,然后输出
string=new String(buf,"utf-8");
} catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
finally //关闭文件
{
if(inputStream!=null)//如果打开了文件,就关闭文件
{
try
{
inputStream.close();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return string;
}
public static void main(String[] args)
{
System.out.println(printTest("test.txt"));
}
}
运行结果:
abcd中国小明