java-使用io流实现txt和图片的复制

 在Java中,IO流可以实现对文件的读取和写入,也因此,我们可以借助IO流来对txt文件进行复制。(类似的,图片等文件亦可)

在IO流中,存在着字节流(byte为传输单位)和字符流(char为传输单位)。

其中,字节流可以实现对图片等多媒体文件进行复制;而字符流则可以很好地对文本性质的文件进行操作。

一、文本文件的复制

这里笔者采用的是字节流进行文本的复制,效率低于字符流,但适用范围更广。

步骤:

1、(可选)创建File对象,指向需要操作的文件

2、在文件操作IO流FileInputStream和FileOutStream创建的对象中指向两个文件

3、设置缓冲区,读取文本中的内容

4、遍历缓冲区,读取所有数据后存入输出流对象

public static void main(String[] args) {

	File fileIn = new File("src/com/wust/Class06/Stream/In.txt");
	File fileOut = new File("src/com/wust/Class06/Stream/Out.txt");
	InputStream inputStream = null;
	OutputStream outputStream = null;

	try {
		inputStream = new FileInputStream(fileIn);
		outputStream = new FileOutputStream(fileOut);

		byte[] buffer = new byte[1024];
		int length = 0;
		while( (length = inputStream.read(buffer)) != -1){
			System.out.println(new String(buffer,0,length));
			outputStream.write(buffer);
		}

	} catch (FileNotFoundException e) {
		e.printStackTrace();
	}catch(IOException e){
		e.printStackTrace();
	}finally {
		try {
			inputStream.close();
			outputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	System.out.println("End");
	
}

如上,虽然以这种方法可以实现buffer的整体复制,但此处调用的write方法未输入长度,即默认全部复制buffer内容。

此时,倘若我们的buffer大小设置过大,而我们未全部填满的时候,剩余空间会被默认填满“0”,以这种write方法进行copy的时候,我们会把大量的0也一起copy过来,这里,我们添加一个遍历,获取准确的buffer有效数据长度后再进行复制。

            int Count = 0;
            for( int i = 0; i < buffer.length; i++){
                if( buffer[i] != 0)
                    Count++;
            }
            System.out.println(Count);
            outputStream.write(buffer,0,Count);

 二、图片的复制

与字符的复制不同,图片等文件是以byte的形式进行存储的,如用字符流进行读取和存储,都无法收获理想的效果,虽然我们可以通过字节-字符之间的转换,实现字符流的R&W,但这样未免南辕北辙,因小失大了。

所以在这里,笔者以一次简单的字节流复制的源码为示例。

步骤:

1、创建FileInputStream和FileOutputStream对象,以分别实现图片等文件的读取和存入操作。

        (这里,也可以如上文的txt复制,创建File对象,来予以IO流指向,便于后续进行修改)

2、创建缓冲区,以存储读取的数据(byte[] buffer)

3、使用while循环遍历缓冲区中的数据,遍历结束后存入FileOutputStream对象中。

4、最终,我们对流进行关闭,即可保证文件的复制成功。

public static void main(String[] args) {
	FileInputStream fileInputStream = null;
	FileOutputStream fileOutputStream = null;
	try {
		fileInputStream = 
            new FileInputStream("src/com/Class06/readerOrWriter/1.jpg");
		fileOutputStream = 
            new FileOutputStream("src/com/Class06/readerOrWriter/2.jpg");

		int length = 0;
		byte[] buffer = new byte[1024];

		while( (length = fileInputStream.read(buffer)) != -1){
			fileOutputStream.write(buffer);
		}

	} catch (FileNotFoundException e) {
		e.printStackTrace();
	}catch(IOException e){
		e.printStackTrace();
	}finally {
		try {
			fileInputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			fileOutputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我想脱离小码农

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值