Java IO-字节流

*I/O流的分类:
流向:
输入流 读取数据
输出流 写入数据

数据类型:
字节流:
字节输入流 读取数据 InputStream
字节输出流 写入数据 OutputStream

字符流:
字符输入流 读取数据 Reader
字符输出流 写入数据 Writer

注意:一般我们在探讨IO流的时候,如果没有明确说明按哪种分类来说,默认情况按照数据类型来分!

这边文章主要是浅谈字节流的–
1.字节流之写入数据
FileOutputStream的构造器:

  • FileOutputStream(File file)
    创建文件输出流以写入由指定的 File对象表示的文件。
    FileOutputStream(String name)
    创建文件输出流以指定的名称写入文件。

    FileOutputStream(File file, boolean append)
    创建文件输出流以写入由指定的 File对象表示的文件。 默认是false 也就是说默认是不追加在文字的末尾的
    字节流输出操作的步骤:
    A.创建字节输出流对象
    B.写数据
    C.释放资源
    在这里插入图片描述

写入的方式有很多种

  • public void write(int b);写一个字节
  • public void write(byte[] b);写一个字节数组
  • public void write(byte[] b,int off,int len);写一个字节数组的一部分

例子:
public static void main(String[] args) throws IOException {

OutputStream os = new FileOutputStream(“fos.txt”,false); //false代表的是第二次操作时会覆盖之前的内容,true代表的是第二次操作时会在末尾追加本次添加的内容

byte[] bs = {97,88,41,36,77,127}; //里面的元素对应ASCII表

os.write(bs, 0,bs.length);//len是字节的长度 off字节数组的起始下标
os.close();
}

加入异常处理的字节输出流的操作(注意IO的异常需要去捕获)
例子:
public static void main(String[] args) {

	FileOutputStream fos = null; //全局变量
	
	try {
		fos = new FileOutputStream("E:\\不要再打了\\这样打不死人\\a.txt"); //默认false,下次操作会覆盖之前的内容
	} catch (FileNotFoundException e) {
		
		e.printStackTrace();
	}
	try {
		fos.write("你好啊啊啊a".getBytes()); //写入内容
	} catch (IOException e) {
		
		e.printStackTrace();
	}
	try {
		if(fos!=null) {  //引用类型判断是否为空,避免空指针异常
		fos.close();
		}
	} catch (IOException e) {
		
		e.printStackTrace();
	}
	
	
	//Java8的新特性,了解一下
	//JDK1.8的 这么写 就不用写fos1.close()了。会自动执行fos1.close()
	try(FileOutputStream fos1 = new FileOutputStream("fos.txt")){
		
		fos1.write("213".getBytes());
		
	} catch (FileNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}

以上是输出流的小例子,无非是创建,写入,获取!
Syso("---------------------------------------------------------------")
接下来是输入流,何为输入流,就是读取呗!

字节输入流的操作步骤:
A.创建字节输入流对象
B.调用read()方法读取操作,并把数据显示在控制台中
C.释放资源

读取方式:
A.int read():一次读取一个字节
B.int read(byte[] b):一次读取一个字节数组
C.int read(byte[] b,int off,int len):一次读取一个部分字节数组
A.
public static void main(String[] args) {

	FileInputStream fis = null;
	try {
		fis = new FileInputStream("fos.txt");
	
		int by = 0;
		//将读取 赋值 判断都在while(里)
		while((by = fis.read())!=-1){ //读取完之后返回-1;
			System.out.println((char)by);
		}
	} catch (FileNotFoundException e) {
		
		e.printStackTrace();
	} catch (IOException e) {
		
		e.printStackTrace();
	}finally {
		try {
			if(fis!=null){
				fis.close();
			}
		} catch (IOException e) {
			
			e.printStackTrace();
		}
	}
}

B C:

public static void main(String[] args) {
	
	FileInputStream fis = null;
	try {
		fis = new FileInputStream("fos.txt");
		byte[] bs = new byte[1024];//数组的长度一般都是1024或者是1024的整倍数 1KB=1024B 1M=1024KB
		//int len = fis.read(bs);//长度	
		//fis.read(bs) 读取完之后放入bs数组里
		//长度  如果读完了 将会返回为-1 
		
		//我要将文件一次性都读取完整
		int len = 0;
		while((len = fis.read(bs))!=-1){  
			System.out.print(new String(bs,0,len));
		}
	} catch (FileNotFoundException e) {
		
		e.printStackTrace();
	} catch (IOException e) {
		
		e.printStackTrace();
	}finally {
		try {
			if(fis!=null){
				fis.close();
			}
		} catch (IOException e) {
			
			e.printStackTrace();
		}
	}
	
}
  • 复制文件:

  • 若数据源 目的地是相同的,那么复制的文件必须要重命名,因为不重命名叫覆盖

  • 数据源:从哪里来
    fos.txt --读取文件数据

  • 目的地:到哪里去
    ffs.txt --写入文件数据

public class CopyFileDemo1 {

public static void main(String[] args) throws IOException {
	// TODO Auto-generated method stub
	FileInputStream fis = new FileInputStream("fos.txt");
	byte[] bs = new byte[1024];
	int len = 0;
	FileOutputStream fos = new FileOutputStream("ffs.txt");//自动创建文件
	//边读边写!!
	while((len = fis.read(bs))!=-1){
		fos.write(bs,0,len);
	}
	fos.close();
	fis.close();	
}

}

将E:\javaTest\demo/3.jpg 复制到d盘下 demo目录下(demo目录不利用win命名创建)

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
File f= new File(“E:\javaTest\demo/3.jpg”);
File f1 = new File(“D:/demo”);
if(!f1.exists()){
f1.mkdirs();
}
File f2 = new File(f1, f.getName());
FileInputStream fis = new FileInputStream(f);
FileOutputStream fos = new FileOutputStream(f2);

byte[] bs = new byte[1024];
int len = 0;
while((len=fis.read(bs))!=-1){
fos.write(bs, 0, len);
}
fos.close();
fis.close();

}
熟悉掌握对象方法就很好运用了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值