2019-7-17 [JavaSE] 文件字节流 练习题2个

1. txt写入

任务一:
文件字节流:

一个字符串 “helloword”,使用文件字节流,把其中的word写入文件word.txt中。
难点:write(参数,参数,参数)

public class homework1 {
	public static void main(String[] args) throws IOException {
		FileOutputStream fout = new FileOutputStream("e:/data/word.txt");
		String name = "helloword";
		fout.write(name.getBytes());
		fout.close();
	}
}

2. map3文件的复制

任务二:
文件字节流:
读取一个mp3文件,把此文件写入到new.mp3文件中,考虑到文件效率问题。

难点:使用缓冲流

public class homework2 {
	public static void main(String[] args) throws IOException {
		FileInputStream fin = new FileInputStream("e:/data/a.mp3");
		BufferedInputStream bfin = new BufferedInputStream(fin);
		FileOutputStream fout = new FileOutputStream("e:/data/anew.mp3");
		BufferedOutputStream bfout = new BufferedOutputStream(fout);
		int temp;
		while ((temp = bfin.read()) != -1) {
			bfout.write(temp);
		}
		bfout.flush();
		bfin.close();
		bfout.close();
	}
}

利用try- catch块提高程序的效率

public class homework3 {

	public static void main(String[] args) {
		File f = new File("e:/data/a.mp3");
		try {
			FileInputStream fin = new FileInputStream(f);
			BufferedInputStream bfin = new BufferedInputStream(fin);
			FileOutputStream fout = new FileOutputStream("e:/data/anew.mp3");
			BufferedOutputStream bfout = new BufferedOutputStream(fout);
			
			byte [] b = new byte[(int)f.length()];
			fin.read(b);
			fout.write(b);
		} catch (Exception e) {
			e.getMessage();
		}		
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值