您的位置:首页 > 编程语言 > Java开发

JAVA中inputStream/OutputStream字节输入流、输出流读写文件

2017-05-15 14:36 567 查看
好记性不如赖笔头……

注意:InputStream/OutputStream是抽象类,不能被实例化,只能实例化其子类,且是字节流,不是字符流

InputStream is = new FileInputStream(“a.txt”); 等同于 InputStream is = new FileInputStream(new File(“a.txt”));

OutputStream类似与之类似,

package com.Ckinghan.outputStream;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class OutputStreamDemo {

public static void main(String[] args) {

/**
* 使用字节流写入文件
*/
outputStreadRead();

/**
*字节流的数据读取方式一:每次读取一个字节
*/
inputStreamReader();

/**
* 字节流的数据读取方式二:每次读取一定长度的字节,建议使用
*/
inputStreamReader1();

/**
* 字节流的数据读取方式三:每次读取一定长度的字节从指定的数组索引上保存数据
*/
inputStreamReader2();
}

/**
* @描述:字节流的数据读取方式三:每次读取一定长度的字节从指定的数组索引上保存数据
* @创建时间:
*/
public static void inputStreamReader2(){
//创建字节对象
InputStream inputStream = null;
try {
//实例化对象
inputStream  = new FileInputStream("JavaIOOutputStreamReader.java");

/**
* 每次读取一定长度的字节从指定的数组索引上保存数据,可以读取中文(文件中的数据为:OutputStreamp测试写入数据)
*/
//定义每次读取字节的大小与保存字节的数据
byte[] bs = new byte[1024];
//定义每次读取的长度
int len = -1;
/**
* inputStream.read(bs,1,10); 参数说明:
* bs:数组,每次最大可以保存1024个字节
* 1    :指定的索引位置,这里是从bs数组的索引1个开始保存数据
* 10:从inputStream读取的字节个数,从0开始,注意,如果读取的数据中文,而读取的字节个数为奇数,可能会现无法识别或错误的数据
*/
len = inputStream.read(bs,1,10);
//输出字符,注意:在输出时,应从指定的索引位置进行读取
System.out.println(new String(bs,1,len));

} catch (FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally {
//关闭流
if(inputStream != null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

/**
* @描述:字节流的数据读取方式二:每次读取一定长度的字节,建议使用
* @创建时间:
*/
public static void inputStreamReader1(){
//创建字节对象
InputStream inputStream = null;
try {
//实例化对象
inputStream  = new FileInputStream("JavaIOOutputStreamReader.java");

/**
* 读取一定长度的字节数据,建议使用,可以读取中文(文件中的数据为:OutputStreamp测试写入数据)
* 读取结果为:OutputStreamp测试写入数据
*/
//定义每次读取字节的大小与保存字节的数据
byte[] bs = new byte[1024];
//定义每次读取的长度
int len = -1;
//循环读取数据,如果读取的数据为-1,说明已经读取了末尾
while((len = inputStream.read(bs)) != -1){
//输出字符
System.out.println(new String(bs,0,len));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally {
//关闭流
if(inputStream != null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

/**
* @描述:字节流的数据读取方式一:每次读取一个字节
* @创建时间:
*/
public static void inputStreamReader(){
//创建对象
InputStream inputStream = null;
try {
//实例化对象
inputStream  = new FileInputStream("JavaIOOutputStreamReader.java");
/**
* 每次读取一个字节,对于英文是可以的(文件中的数据为:OutputStreamp测试写入数据),
* 但不能读取中文,因为编码我使用的是UTF-8,一个中文占两个字节的长度
* 读取的结果如下:OutputStreamp????????????
*/
//创建读取的字符,保存的对应的acsii码
int b = -1;
//循环读取字符,如果字符为-1,说明已读取到文件的末尾
while ((b = inputStream.read()) != -1) {
//将读取的acsii码转换为字符并输出
System.out.print((char)b);
}
System.out.println();
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally {
//关闭流
if(inputStream != null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

/**
* @描述:使用字节流写入数据到文件中
* @创建时间:
*/
public static void outputStreadRead(){
//创建字节流对象
OutputStream stream = null;
try {
//实例化对象
stream = new FileOutputStream("JavaIOOutputStreamReader.java");
//要写入的字符串数据
String string = "OutputStreamp测试写入数据";
//将字符串数据转换为字节数组
byte[] bytes = string.getBytes();
//将字节数组写入到文件
stream.write(bytes);
//清空缓冲区,将写入的数据保存
stream.flush();
//写入成功后的提示语
System.out.println("写入文件成功");

//抛出异常
} catch (IOException e) {
e.printStackTrace();
}finally {
//如果stream被实例化
if(stream != null){
try {
//关闭字节流
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐