java移动代码_java文件移动代码

最近辞职在家,开始复习以前学过的内容,好准备找更好的工作。在网上下了些视频解压后都是一个个文件夹下一个文件。看完一个又要去另一个文件夹里面找,找得心烦。所以写了个文件移动的代码,把所有视频文件移动到同一指定的文件夹下。代码如下:

package com;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

public class MoveFile {

/**

* @param args

* @throws IOException

*/

public static void main(String[] args) throws IOException {

String fileName = "F:\\視頻教程";

File filepath = new File(fileName);

File[] files = filepath.listFiles();

for (File file : files) {

if (file.isDirectory()) {

File[] subFiles = new File(file.getPath()).listFiles();

for (File subfile : subFiles) {

if (subfile.isFile()) {

// if (subfile.getName().lastIndexOf(".avi") != -1) {

copy(subfile.getPath(), fileName + File.separator

+ subfile.getName());

subfile.delete();

// }

}

}

}

}

System.out.println("移动成功");

}

public static void copy(String from, String to) throws IOException {

int BUFF_SIZE = 100000;

byte[] buffer = new byte[BUFF_SIZE];

InputStream in = null;

OutputStream out = null;

try {

in = new FileInputStream(from);

out = new FileOutputStream(to);

while (true) {

synchronized (buffer) {

int amountRead = in.read(buffer);

if (amountRead == -1) {

break;

}

out.write(buffer);

}

}

} finally {

if (in != null) {

in.close();

}

if (out != null) {

out.close();

}

}

}

}

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2011-07-13 22:29

浏览 1275

评论

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值