1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > java把一个文件的内容复制到另外一个文件

java把一个文件的内容复制到另外一个文件

时间:2018-07-23 19:04:24

相关推荐

java把一个文件的内容复制到另外一个文件

/**

* java把一个文件的内容复制到另外一个文件

*/

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

public class FileInputOutputStreamTest {

public static void main(String[] args) {

File af = new File("a.txt");

File bf = new File("b.txt");

FileInputStream is = null;

FileOutputStream os = null;

if(!bf.exists()){

try {

bf.createNewFile();

} catch (IOException e) {

e.printStackTrace();

}

}

try {

is = new FileInputStream(af);

os = new FileOutputStream(bf);

byte b[] = new byte[1024];

int len;

try {

len = is.read(b);

while (len != -1) {

os.write(b, 0, len);

len = is.read(b);

}

} catch (IOException e) {

e.printStackTrace();

}

} catch (FileNotFoundException e) {

e.printStackTrace();

}finally{

try {

if(is != null) is.close();

if(os != null) os.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。