Java输入流输出流读取文件

处理图片、音乐等一般使用字节流;处理汉字可以使用字符流。

java中提供了专用于输入输出功能的包Java.io,其中包括:
     InputStream,OutputStream,Reader,Writer
     InputStream 和OutputStream,两个是为字节流设计的,主要用来处理字节或二进制对象,
     Reader和 Writer.两个是为字符流(一个字符占两个字节)设计的,主要用来处理字符或字符串

    //复制文件
	public static void copyFile() throws Exception{
		File file= new File("E:"+File.separator+"aa.txt");
		//复制文件aa.txt,名字为aa-副本.txt
		//获取file的名字
		String name = file.getName();
		//根据最后一个.截取
		String houzhui = name.substring(name.lastIndexOf("."));
		//名称
		String name2= name.substring(0,name.indexOf("."))+"-副本";
		//新名称
		String newName = name2+houzhui;
		//创建文件副本
		File file2 =new File("E:"+File.separator+File.separator+newName);
		//读取file内容给file2 通过字节流
		InputStream iput = new FileInputStream(file);
		OutputStream oput = new FileOutputStream(file2);
		//进行数据读取
		byte data[] = new byte[1024];
		int temp = 0;
		if((temp = iput.read(data))!= -1 ){
			System.out.println(temp);
			System.out.println(data);
			//读取内容
			oput.write(data,0,temp);
		}
		iput.close();
		oput.close();
	}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值