Java—I/O系统

字节输入流

package com.fs.test;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;

public class Test {
	
	public void m1() throws Exception {//父类异常
		
		//1.创建文件输入流对象
		InputStream in= new FileInputStream("D:/test/b.txt");
		
		//3.创建一个字节数组用来存放读取到的数据
		byte[] buf = new byte[10];
		
		//2.通过输入流对象读取数据
		in.read(buf);
		
		//4.将字节数组转为字符串显示
		String str = new String(buf);
		System.out.println(str);
		
		//5.关闭流
		in.close();
		
	}

	public void m2() throws Exception {
		
		//1.创建一个字节流输出对象
		InputStream in = new FileInputStream("d:/test/b.txt");
		
		//2.对输入流对象进行封装
		BufferedInputStream bin = new BufferedInputStream(in);
		
		//3.创建一个字节数组,用于装每一次读取到的数据
		byte[] buf = new byte[10];
		String str = "";
		
		//4.循环读取数据,知道文件中的数据被读取完
		while(bin.read(buf) != -1){//判断文件是否已被读取到末尾
			str += new String(buf);//将每一次读取到的数据添加到str的末尾
		}
		
		System.out.println(str);
		
		//先创建的后关闭,后创建的先关闭
		bin.close();
		in.close();
		
	}
	//调用者,谁调用谁抛出
	public static void main(String[] args) throws Exception {
		Test t = new Test();
		t.m1();
		t.m2();
		
	}

}

字节输出流

package com.fs.test;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;

public class Test {
	
	public void m1() throws Exception {
		//1.创建一个文件字节输出流对象
		//第一个参数表示输出的路径
		//第二个参数表示文本的追加方式:如果为true,表示在文件末尾追加内容;如果不写或者为false,表示覆盖文件原有内容
		OutputStream out = new FileOutputStream("d:/test/a.txt", true);
		
		//2.准备输出的数据
		String str = "这是要输出的数据";
		
		//3.通过输出流对象输出数据
		out.write(str.getBytes());//str.getBytes()将字符串转为字节数组
		
		//4.将数据刷出
		out.flush();
		
		//5.关闭流
		out.close();
		
	}

	public void m2() throws Exception {
		//1.创建一个输出流对象
		OutputStream out = new FileOutputStream("d:/test/a.txt", true);
		
		//2.对输出流对象进行封装
		BufferedOutputStream bout = new BufferedOutputStream(out);
		
		//3.准备要输出的数据
		String str = "abcdefg";
		byte[] buf = str.getBytes();//转为字节数组
		
		//4.每隔一秒输出一个字节
		for(int i = 0; i < buf.length;i++){
			Thread.sleep(1000);//睡眠一秒
			
			bout.write(buf[i]);//输出数据
		}
		
		//5.将数据刷出
		bout.flush();
		
		//6.关闭流
		bout.close();
		out.close();
		
		
	}
	//调用者
	public static void main(String[] args) throws Exception {
		new Test().m2();
		
	}

}

文件的复制

package com.fs.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;
import java.nio.file.Files;

public class CopyFiles {

	static String path = "c:/picture.PNG";//需要复制的文件
	static String newpath = "d:/copy.PNG";//需要复制的路径并重命名

	public static void main(String[] args) throws Exception {
		//创建两个File对象
		File source = new File(path);
		File dest = new File(newpath);

		//计算开始复制到结束的时间差,所消耗的时间
		long start = 0;
		long end = 0;

		start = System.currentTimeMillis();
		copy_FileChannel(source, dest);
		end = System.currentTimeMillis();
		System.out.println("copy_fileChannel" + (end - start));
	}

	public static void copy_FileChannel(File source, File dest) throws Exception {
		@SuppressWarnings("resource")
		FileChannel in = new FileInputStream(source).getChannel();
		@SuppressWarnings("resource")
		FileChannel out = new FileOutputStream(dest).getChannel();

		//transferFrom()方法快速复制文件
		out.transferFrom(in, 0, in.size());

		in.close();
		out.close();

	}

	public static void copy_File(File source, File dest) throws Exception {
		
		Files.copy(source.toPath(), dest.toPath());

	}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值