文件操作(把源文件拷贝到目标文件路径下)
2012-06-30 14:16
239 查看
/** * 功能描述:拷贝一个目录或者文件到指定路径下,即把源文件拷贝到目标文件路径下 * * @param source * 源文件 * @param target * 目标文件路径 * @return void */ public static void copy(File source, File target) { File tarpath = new File(target, source.getName()); if (source.isDirectory()) { tarpath.mkdir(); File[] dir = source.listFiles(); for (int i = 0; i < dir.length; i++) { copy(dir[i], tarpath); } } else { try { InputStream is = new FileInputStream(source); // 用于读取文件的原始字节流 OutputStream os = new FileOutputStream(tarpath); // 用于写入文件的原始字节的流 byte[] buf = new byte[1024];// 存储读取数据的缓冲区大小 int len = 0; while ((len = is.read(buf)) != -1) { os.write(buf, 0, len); } is.close(); os.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
相关文章推荐
- 完成文件复制操作,在程序运行后提示输入源文件路径,然后再输入目标文件路径。
- 完成文件复制操作,在程序运行后提示输入源文件路径,然后再输入目标文件路径。
- 完成文件复制操作,在程序运行后提示输入源文件路径,然后再输入目标文件路径。
- perl 把源目录下的文件拷贝到目标目录(带路径)
- 完成文件复制操作,在程序运行后提示输入源文件路径,然后再输入目标文件路径。
- 完成文件复制操作,在程序运行后提示输入源文件路径,然后再输入目标文件路径。
- 完成文件复制操作,在程序运行后提示输入源文件路径,然后再输入目标文件路径。
- 完成文件复制操作,在程序运行后提示输入源文件路径,然后再输入目标文件路径。
- 完成文件复制操作,在程序运行后提示输入源文件路径,然后再输入目标文件路径。
- [随笔]Linux下,如何将一个文件及其所在的递归路径一起拷贝到目标目录?
- 完成文件复制操作,在程序运行后提示输入源文件路径,然后再输入目标文件路径。
- VS20xx IDE开发应用时_拷贝VS环境的库文件DLL到目标设备上运行的操作步骤
- 完成文件复制操作,在程序运行后提示输入源文件路径,然后再输入目标文件路径。
- 完成文件复制操作,在程序运行后提示输入源文件路径,然后再输入目标文件路径。
- C# 文件操作 全收录 追加、拷贝、删除、移动文件、创建目录、递归删除文件夹及文件....
- 沙盒路径下Plist文件操作
- 求c#中上传视频文件(目标文件夹的相对路径为./MV)的源代码(即想把视频文件上传到MV文件夹中)
- SSIS 文件系统任务无法使用变量配置目标路径
- C# 远程创建共享目录及文件拷贝操作
- Python os模块--路径、文件、系统命令等操作