自己写的剪切文件代码
来源:互联网 发布:华软市政软件 编辑:程序博客网 时间:2024/04/29 13:02
package com.itcast;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;/* * 剪切一个非空文件夹 */public class Prac1 {public static void main(String[] args) throws IOException {File srcFile = new File("F:\\aa");File destFile = new File("E:\\aa");pasteFile(srcFile,destFile);}private static void pasteFile(File srcFile, File destFile) throws IOException {// TODO Auto-generated method stubFile[] files = srcFile.listFiles();for (File file : files) {if (file.isFile()) {copyFile(file,destFile);deleteFile(file);}else{//递归,遍历文件夹pasteFile(new File(srcFile,file.getName()), new File(destFile,file.getName()));}srcFile.delete();}}private static void deleteFile(File srcFile) {// TODO Auto-generated method stubsrcFile.delete();}//复制文件private static void copyFile(File srcFile, File destFile) throws IOException{//判断文件夹是否存在if (!destFile.exists()) {new File(destFile.getAbsolutePath()).mkdir();}//使用字节流读取文件BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(srcFile));File newFile = new File(destFile,srcFile.getName());//写入文件BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(newFile));//定义一个缓冲数组byte[] buf = new byte[1024];int len = 0;while ((len = bufferedInputStream.read(buf))!=-1) {bufferedOutputStream.write(buf, 0, len);}//关闭资源。bufferedOutputStream.close();bufferedInputStream.close();}}
代码的所有IO异常都抛出去了,不是很好哈~
0 0
- 自己写的剪切文件代码
- 自己写的代码文件
- C#实现文件剪切的代码
- 自己写的代码
- 文件夹的复制,剪切,删除,及文件追加写实例
- 在python代码中导入自己写的.py文件
- 自己写的java复制任意文件代码
- 文件的简单操作 : 读、写(覆盖写、追加写)、复制、剪切、新增、删除
- 自己写的frameset代码
- 自己写的分页代码
- 自己写的代码:时钟
- 自己写的代码,备忘
- 自己写的代码2
- 自己写的代码生成器
- 汉诺塔代码自己写的
- 剪切文件的函数
- 自己写的文件程序
- MFC下对文件及文件夹的操作(复制、剪切、删除、创建文件夹,写文件)
- 黑马程序员-OC前言-学习笔记
- Android游戏框架的搭建
- poj 1020 Anniversary Cake dfs的灵活结构
- 虚拟工作室tfs版添加用户
- URAL1353---Milliard Vasya's Function(简单数位dp)
- 自己写的剪切文件代码
- Remove Linked List Elements
- 代理模式
- Orcale 常用命令集合 (更新中)
- [LeetCode]Repeated DNA Sequences
- 序列化和反序列化xml文档
- Share Your Music - HTML5 Music Web App
- Android Studio
- 黑马程序员-面向对象和面向过程-学习笔记