举例有如下文件:
文件内容为:
1.InputStream
字节输入流,所有字节输入流的父类,抽象类
读取字节流方式一:
/**
* 字节输入流_read()
*
* @throws IOException
*/
private static void inputStreamTest_1() throws IOException{
File file = new File("E:"+File.separator+"输入输出流测试.txt");
InputStream inputStream = new FileInputStream(file);
int temp;
//循环字节流,一个字节一个字节的读取,无字节时返回-1
while((temp=inputStream.read())!=-1){
//有字节时,返回0到255范围内的int字节值
System.out.print(temp+"——");
//字节值转换为char
System.out.println((char)temp + "");
}
inputStream.close();
}
输出结果:
73——I
110——n
112——p
117——u
116——t
83——S
116——t
114——r
101——e
97——a
109——m
读取字节流方式二:
/**
* 字节输入流_read(byte[] b)
*
* b.length==0,则不读取任何字节并返回0
* b.length!=0,则读取的字节存储在b[0]至b[length-1]中
* 流末无字节时,则返回-1
* @throws IOException
*/
private static void inputStreamTest_2() throws IOException {
//File.separator_路径分隔符,可跨系统使用
File file = new File("E:"+File.separator+"输入输出流测试.txt");
InputStream inputStream = new FileInputStream(file);
//创建文件长度的字节数组,file.length()文件字节,inputStream.available()流字节
byte b[]=new byte[(int)file.length()];
int temp;
//循环字节流,最多读取b.length个字节、最少1个,并将其存储在缓冲区数组b中
while((temp=inputStream.read(b))!=-1){
//以整数形式返回实际读取的字节数
System.out.println(temp);
System.out.println(new String(b));
}
//关闭流
inputStream.close();
}
输出结果:
11
InputStream
读取字节流方式三:
/**
* 字节输入流_read(byte[] b, int off, int len)
*
* b为null,则抛出NullPointerException
* off为负,或len为负,或off+len>b.length,或len>b.length-off,则抛出IndexOutOfBoundsException
* len==0,则无字节可读且返回0
* 流末无字节时,则返回-1
* @throws IOException
*/
private static void inputStreamTest_3() throws IOException {
//File.separator_路径分隔符,可跨系统使用
File file = new File("E:"+File.separator+"输入输出流测试.txt");
InputStream inputStream = new FileInputStream(file);
//创建文件长度的字节数组,file.length()文件字节,inputStream.available()流字节
byte b[]=new byte[(int)file.length()];
int temp;
//最多读取len个字节、最少1个,并把他们依次存储在从b[off]开始的字节数组中
while((temp=inputStream.read(b,0,b.length))!=-1){
//以整数形式返回实际读取的字节数
System.out.println(temp);
System.out.println(new String(b));
}
//关闭流
inputStream.close();
}
输出结果:
11
InputStream
2.OutputStream
字节输出流,所有字节输出流的父类,抽象类
写出字节流方式一:
/**
* 字节输出流_write(int b)
*
* @throws IOException
*/
private static void outputStreamTest_1() throws IOException {
File file = new File("E:"+File.separator+"输入输出流测试.txt");
//true表示不覆盖原有内容写出
OutputStream outputStream = new FileOutputStream(file,true);
//写出int字节值到文件中
outputStream.write(73);
//清空缓冲区数据
outputStream.flush();
outputStream.close();
}
此时文件内容为:
InputStreamI
写出字节流方式二:
/**
* 字节输出流_write(byte b[])
*
* @throws IOException
*/
private static void outputStreamTest_2() throws IOException {
File file = new File("E:"+File.separator+"输入输出流测试.txt");
//true表示不覆盖原有内容写出
OutputStream outputStream = new FileOutputStream(file,true);
String str = "OutputStream";
byte[] b = str.getBytes();
//写出字节数组到文件中
outputStream.write(b);
//刷空输出流,并输出所有被缓存的字节
outputStream.flush();
outputStream.close();
}
此时文件内容为:
InputStreamIOutputStream
写出字节流方式三:
/**
* 字节输出流_write(byte b[], int off, int len)
*
* b为null,则抛出NullPointerException
* off 为负,或len为负,或off+len>b.length,则抛出 IndexOutOfBoundsException
* len==0,则不写出字节
* @throws IOException
*/
private static void outputStreamTest_3() throws IOException {
File file = new File("E:"+File.separator+"输入输出流测试.txt");
//true表示不覆盖原有内容写出
OutputStream outputStream = new FileOutputStream(file,true);
String str = "OutputStream";
byte[] b = str.getBytes();
//写出字节b[off]到b[off+len-1]
outputStream.write(b,0,b.length);
//刷空输出流,并输出所有被缓存的字节
outputStream.flush();
outputStream.close();
}
此时文件内容为:
InputStreamIOutputStreamOutputStream
3.InputStream与OutputStream整合运用
举例E盘有如下文件:
/**
* 输入字节流与输出字节流
*
* @throws IOException IO异常
*/
private static void InputStreamAndOutputStreamTest() throws IOException {
File inputFile = new File("E:"+File.separator+"输入流测试.jpg");
File outFile = new File("E:"+File.separator+"输出流测试.jpg");
InputStream inputStream = new FileInputStream(inputFile);
OutputStream outputStream = new FileOutputStream(outFile);
byte b[]=new byte[(int)inputFile.length()];
//读取字节数组
while(inputStream.read(b)!=-1){
//写出字节数组
outputStream.write(b);
}
outputStream.flush();
outputStream.close();
}
方法读取E盘下“输入流测试.jpg”文件,此时E盘下写出“输出流测试.jpg”文件;
注:字节输入流与字节输出流主要用于对字节和二进制文件处理,java.io提供字符输入流与字符输出流处理字符或字符串