通过java字节流复制文件,逐个字节读取写入

时间:2022-06-18 16:03:26
package cwj.bbb;

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

public class StreamTest
{
public static void main(String[] args) throws IOException
{
/*把路径下的文件/home/cwjy1202/hadoop/javaTest/input01.txt
* 复制一份/home/cwjy1202/hadoop/javaTest/input011.txt
* */
File file = new File("/home/cwjy1202/hadoop/javaTest/input01.txt");
InputStream fis = new FileInputStream(file);
OutputStream fos = new FileOutputStream("/home/cwjy1202/hadoop/javaTest/input011.txt");

//逐个读取
int len = fis.read();
while(-1 != len)
{
//逐个写入
fos.write(len);
len = fis.read();
}

//把缓冲区数据强行输出
fos.flush();
fos.close();
fis.close();
}
}