首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Java 资料拷贝

2012-10-08 
Java 文件拷贝?public static void saveFiles(String origPath, String destPath) {??File file new Fil

Java 文件拷贝

?public static void saveFiles(String origPath, String destPath) {
??File file = new File(origPath);
??System.out.println("开始复制文件");
?
??InputStream in = null;
??BufferedOutputStream fos=null;
??try {
???fos= new BufferedOutputStream(new FileOutputStream(destPath));
???if (!file.exists() || file.isDirectory())
????throw new FileNotFoundException();
???in = new BufferedInputStream(new FileInputStream(file));
???byte[] temp = new byte[1024];
???int size = 0;
???while ((size = in.read(temp)) != -1) {
????fos.write(temp, 0, size);
???}?
??} catch (FileNotFoundException e) {
???e.printStackTrace();
??} catch (UnsupportedEncodingException e) {
???e.printStackTrace();
??} catch (IOException e) {
???e.printStackTrace();
??}finally{
???try {
????fos.flush();
????fos.close();
???} catch (IOException e) {
????e.printStackTrace();
???}
???try {
????in.close();
???} catch (IOException e) {
????e.printStackTrace();
???}
??}
??System.out.println("复制文件完成");
?}

热点排行