I/O流,文件copy

字节方式copy

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.junit.Test;

public class ByteStream {
	
	@Test
	public void byteCopy(){
		File file=null;
		File file2= null;
		InputStream iStream=null;
		OutputStream oStream=null;
		
		try {
			file = new File("F:\\tupian.jpg");
			file2 = new File("E:\\英雄时刻\\123.jpg");
			iStream = new FileInputStream(file);
			oStream = new FileOutputStream(file2);
			
			byte[] b = new byte[512];
			int len= 0;
			while((len=iStream.read(b))!=-1){
				oStream.write(b);
				
			}
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				oStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				iStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}
}

buffered缓冲区方式

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.junit.Test;

public class BufStream {
	
	@Test
	public void bufCopy(){
		File file=null;
		File file2 = null;
		InputStream iStream=null;
		OutputStream oStream = null;
		BufferedInputStream bufiStream=null;
		BufferedOutputStream bufoStream=null;
		try {
			file=new File("e:\\算法与分析\\楷书编程与编码规范.rar");
			file2=new File("f:\\编码规范.rar");
			
			iStream=new FileInputStream(file);
			oStream=new FileOutputStream(file2);
			
			bufiStream=new BufferedInputStream(iStream);
			bufoStream=new BufferedOutputStream(oStream);
			
			byte[] buf=new byte[512];
			int len=0;
			while((len=bufiStream.read(buf))!=-1){
				bufoStream.write(buf);
				bufoStream.flush();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				bufoStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				bufiStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

char字符文本的copy

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import org.junit.Test;

public class CharStream {
	@Test
	public void charCopy(){//实现两个文件的copy
		File file = null;//源文件
		File file2 = null;//目标文件
		FileReader fReader = null;
		FileWriter fWriter = null;
		try{
		file =	new File("F:\\hello");
		file2 =	new File("F:\\temp.txt");
		
		fReader = new FileReader(file);
		fWriter = new FileWriter(file2);
		
		char[] c = new char[50];
		int len= 0;
		while((len=fReader.read(c))!=-1){
			//把读到的内容写到目标文件中
			fWriter.write(c, 0, len);
			fWriter.flush();
			
		}
		
		}catch(Exception e){
			e.printStackTrace();
		}finally {
			try {
				fWriter.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				fReader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一个基于Linux系统的C语言程序,实现无缓存I/O操作,将文件/ect/profile的内容复制到当前目录下的file文件中,并修改文件file的权限。 代码如下: ```c #include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <unistd.h> #include <sys/stat.h> #define BUFFER_SIZE 4096 int main() { int fd_in, fd_out; // 输入输出文件描述符 char buffer[BUFFER_SIZE]; // 缓存数组 int num_read, num_written; // 读写字节数 // 打开/ect/profile文件 if ((fd_in = open("/etc/profile", O_RDONLY)) == -1) { perror("open /etc/profile error"); exit(EXIT_FAILURE); } // 创建file文件,如果已存在则清空 if ((fd_out = open("file", O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) == -1) { perror("open file error"); exit(EXIT_FAILURE); } // 读取/ect/profile文件内容并写入file文件 while ((num_read = read(fd_in, buffer, BUFFER_SIZE)) > 0) { if ((num_written = write(fd_out, buffer, num_read)) != num_read) { perror("write error"); exit(EXIT_FAILURE); } } // 关闭文件 close(fd_in); close(fd_out); // 修改file文件权限 if (chmod("file", S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) == -1) { perror("chmod error"); exit(EXIT_FAILURE); } printf("Copy /etc/profile to file successfully!\n"); return 0; } ``` 编译命令: ``` gcc -o copy_profile copy_profile.c ``` 执行命令: ``` ./copy_profile ``` 注意:在执行命令前请确保您有足够的权限在当前目录下创建和修改文件

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值