package com.yuxinyicheng.java;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.junit.Test;
/*
*要求:将hello.txt文件中的内容复制到hello1.txt中
*注意:写入的时候一定要fos.write(b,0,len);只有这样才可以呢
* */
public class TestInputOutputStream {
@Test
public void test(){
copy("hello.txt","hello1.txt");
}
public void copy(String src,String dest){
//创建File对象
File file1=new File(src);
File file2=new File(dest);
//创建节点流
FileInputStream fis=null;
FileOutputStream fos=null;
try {
fis=new FileInputStream(file1);
fos=new FileOutputStream(file2);
//创建一个字节用来读取文件
byte[] b=new byte[5];//b.length会根据不同的传输内容,大小不同的
int len;
while((len=fis.read(b))!=-1){
//将读入的内容写入到file2中
fos.write(b, 0, len);//写入的时候,只能够使用这个
}
} catch (Exception e) {
e.printStackTrace();
}
finally{
if(fis!=null){
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(fos!=null){
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.junit.Test;
/*
*要求:将hello.txt文件中的内容复制到hello1.txt中
*注意:写入的时候一定要fos.write(b,0,len);只有这样才可以呢
* */
public class TestInputOutputStream {
@Test
public void test(){
copy("hello.txt","hello1.txt");
}
public void copy(String src,String dest){
//创建File对象
File file1=new File(src);
File file2=new File(dest);
//创建节点流
FileInputStream fis=null;
FileOutputStream fos=null;
try {
fis=new FileInputStream(file1);
fos=new FileOutputStream(file2);
//创建一个字节用来读取文件
byte[] b=new byte[5];//b.length会根据不同的传输内容,大小不同的
int len;
while((len=fis.read(b))!=-1){
//将读入的内容写入到file2中
fos.write(b, 0, len);//写入的时候,只能够使用这个
}
} catch (Exception e) {
e.printStackTrace();
}
finally{
if(fis!=null){
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(fos!=null){
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}