我在写一个对目录操作的代码现在遇到问题了,希望大家帮助。
删除目录代码,无法删除目录内的文件。
public void deleteDirectory(String relativePath) throws Exception {
File parent = new File(root, relativePath);
if (!parent.exists()) {
throw new Exception("parentFile not existed");
}
boolean success = parent.delete();
if (!success) {
// Deletion failed
throw new Exception("Deletion failed");
}
System.out.println("Delete deleteDirectory" + "\t" + parent
+ " Successful. ");
}
删除文件的代码,仅删除制定文件
public void deleteFile(String relativePath, String fileName)
throws Exception {
File parent = new File(root, relativePath);
if (!parent.exists()) {
throw new Exception("parentFile not existed");
}
File file = new File(parent, fileName);
if (!file.exists()) {
throw new Exception("file not existed");
}
boolean success = file.delete();
if (!success) {
// Deletion failed
throw new Exception("Deletion failed");
} else {
System.out.println(file +"\t"+"Delete File Successful.");
}
}
对目录的移动,和COPY代码我不知道该如何写,我查到要用遍历,但是网上的例子都是File定义的文件,我需要的是String,希望大家指导 谢谢
public void moveDirectory(File fromRelativePath, File toRelativePath)
throws Exception {
}
public void moveDirectory(String fromRelativePath, String toRelativePath)
throws Exception {
}
public void copyDirectory(String fromRelativePath, String toRelativePath)
throws Exception {
}
删除目录代码,无法删除目录内的文件。
public void deleteDirectory(String relativePath) throws Exception {
File parent = new File(root, relativePath);
if (!parent.exists()) {
throw new Exception("parentFile not existed");
}
boolean success = parent.delete();
if (!success) {
// Deletion failed
throw new Exception("Deletion failed");
}
System.out.println("Delete deleteDirectory" + "\t" + parent
+ " Successful. ");
}
删除文件的代码,仅删除制定文件
public void deleteFile(String relativePath, String fileName)
throws Exception {
File parent = new File(root, relativePath);
if (!parent.exists()) {
throw new Exception("parentFile not existed");
}
File file = new File(parent, fileName);
if (!file.exists()) {
throw new Exception("file not existed");
}
boolean success = file.delete();
if (!success) {
// Deletion failed
throw new Exception("Deletion failed");
} else {
System.out.println(file +"\t"+"Delete File Successful.");
}
}
对目录的移动,和COPY代码我不知道该如何写,我查到要用遍历,但是网上的例子都是File定义的文件,我需要的是String,希望大家指导 谢谢
public void moveDirectory(File fromRelativePath, File toRelativePath)
throws Exception {
}
public void moveDirectory(String fromRelativePath, String toRelativePath)
throws Exception {
}
public void copyDirectory(String fromRelativePath, String toRelativePath)
throws Exception {
}
class DeleteDir { private static File m_root; //记录要删除的路径
private static ArrayList m_dirs; // /**
* 删除文件夹。
* @param dir
* @return
*/
public static boolean deleteDirs(java.io.File dir) {
try {
m_root = dir;
m_dirs = new ArrayList(); if (!m_root.isDirectory()) {
return false;
} else {
m_dirs.add(m_root); //获取指定路径下的所有文件已经文件夹(递归调用)
visitAll(m_root);
//删除list中的所有文件(倒叙循环删除)
rootDelete();
}
} catch (Exception ex) {
ex.printStackTrace();
return false;
} return true;
} /**
* 删除文件或文件夹。
* 使用倒叙删除,先删除文件,然后删除空文件夹。
* @throws java.lang.Exception
*/
private static void rootDelete() throws Exception {
if (m_dirs != null) {
//使用倒叙循环删除(先删除文件,再删除文件夹)
for (int i = m_dirs.size() - 1; i >= 0; i--) {
File f = (File) m_dirs.remove(i); //获取之后删除list中的数据
//删除数据
if (!f.delete()) {
System.out.println("文件路径:" + f.toString() + " 不存在");
}
}
} else {
throw new Exception("error in rootDelete");
}
} /**
* 通过递归调用获取所有文件和文件夹,存储在m_dirs中。
* @param tempRoot 文件路径
* @throws java.lang.Exception
*/
private static void visitAll(File tempRoot) throws Exception {
//获取指定路径下的所有文件
File[] dirs = tempRoot.listFiles(); if (dirs != null) {
//将文件数组转换成List对象
List dirslist = Arrays.asList(dirs); //将dirslist完全添加到m_dirs中
m_dirs.addAll(dirslist); //递归调用
for (int i = 0; i < dirslist.size(); i++) {
visitAll((File) dirslist.get(i));
}
}
}
}
package org.gzyz.csj.java.tool.moveFile;import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;/**
* 快速复制文件和目录<br>
* 功能:<br>
* 利用nio来快速复制文件<br>
* 利用nio快速复制目录<br>
* 所有的方法都包装了RuntimeException异常<br>
* 如果要对异常进行处理可以捕获该异常<br>
* 2006-6-9 <br>
* 18:00:40 <br>
* @author csj <br>
*/
public class MoveFile {
/**
* MoveFile 构造函数 <br>
* 2006-6-9 <br>
* 19:20:05 <br>
* @author csj <br>
*/
public MoveFile(){
//空
}
/**
* 返回 (Java 虚拟机中的空闲内存量)*(2/3) <br>
* @return <br>
* 2006-6-9 <br>
* 18:40:15 <br>
* @author csj <br>
*/
private long freeMemory(){
return (Runtime.getRuntime().freeMemory()) * (2/3);
}
/**
* 功能:<br>
* 利用nio来快速复制文件<br>
* @param srcFile 要移动的文件<br>
* @param destFile 移动后的文件<br>
* 2006-6-9 <br>
* 19:14:21 <br>
* @author csj <br>
*/
public void copyFile(String srcFile, String destFile){
this.copyFile(new File(srcFile),new File(destFile));
}
/**
* 功能:<br>
* 利用nio来快速复制文件<br>
* @param srcFile 要移动的文件<br>
* @param destFile 移动后的文件<br>
* 2006-6-9 <br>
* 19:14:29 <br>
* @author csj <br>
*/
public void copyFile(File srcFile, File destFile){
if(srcFile.length() < this.freeMemory()){
this.copyBigFile(srcFile,destFile);
}else{
FileChannel srcFcin = null;
FileChannel destFcout = null;
try {
srcFcin = new FileInputStream(srcFile).getChannel();
destFcout = new FileOutputStream(destFile).getChannel();
srcFcin.transferTo(0, srcFcin.size(), destFcout);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}finally{
//关闭所有IO对象
try {
if(srcFcin!=null){
srcFcin.close();
srcFcin=null;
}
if(destFcout!=null){
destFcout.close();
destFcout=null;
}
} catch (IOException e) {
throw new RuntimeException(e);
}
srcFile=null;
destFile=null;
}
}
}
/**
* 处理大型文件,可使用缓冲流来解决上一个方法出现的问题<br>
* 功能:<br>
* 利用nio来快速复制文件<br>
* @param srcFile 要移动的文件<br>
* @param destFile 移动后的文件<br>
* 2006-6-9 <br>
* 18:20:20 <br>
* @author csj <br>
*/
private void copyBigFile(String srcFile, String destFile){
this.copyBigFile(new File(srcFile),new File(destFile));
}
/**
* 处理大型文件,可使用缓冲流来解决上一个方法出现的问题<br>
* 功能:<br>
* 利用nio来快速复制文件<br>
* @param srcFile 要移动的文件<br>
* @param destFile 移动后的文件<br>
* 2006-6-9 <br>
* 18:20:20 <br>
* @author csj <br>
*/
private void copyBigFile(File srcFile, File destFile){
FileChannel srcFcin = null;
FileChannel destFcout = null;
ByteBuffer buffer=null;
try {
srcFcin=new FileInputStream(srcFile).getChannel();
destFcout=new FileOutputStream(destFile).getChannel();
/*每次读取数据的缓存大小*/
buffer=ByteBuffer.allocate((new Long(this.freeMemory() * (2/3))).intValue());
while(srcFcin.read(buffer) != -1){
buffer.flip();
destFcout.write(buffer);
buffer.clear();
}
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}finally{
//关闭所有IO对象
try {
if(srcFcin!=null){
srcFcin.close();
srcFcin=null;
}
if(destFcout!=null){
destFcout.close();
destFcout=null;
}
if(buffer!=null){
buffer.clear();
buffer=null;
}
} catch (IOException e) {
throw new RuntimeException(e);
}
srcFile=null;
destFile=null;
}
}
/**
* 功能:<br>
* 利用nio来快速剪切文件<br>
* @param srcFile 要移动的文件<br>
* @param destFile 移动后的文件<br>
* 2006-6-9 <br>
* 18:18:51 <br>
* @author csj <br>
*/
public void cutFile(String srcFile, String destFile){
this.cutFile(new File(srcFile),new File(destFile));
}
/**
* 功能:<br>
* 利用nio来快速剪切文件<br>
* @param srcFile 要移动的文件<br>
* @param destFile 移动后的文件<br>
* 2006-6-9 <br>
* 18:18:51 <br>
* @author csj <br>
*/
public void cutFile(File srcFile, File destFile){
this.copyFile(srcFile,destFile);
srcFile.delete();
}
/**
* 功能:<br>
* 利用nio快速复制目录<br>
* @param srcDirectory 要移动的目录<br>
* @param destDirectory 移动后的目录<br>
* 2006-6-9 <br>
* 18:15:59 <br>
* @author csj <br>
*/
public void copyDirectory(String srcDirectory, String destDirectory){
this.copyDirectory(new File(srcDirectory),new File(destDirectory));
}
/**
* 功能:<br>
* 利用nio快速复制目录<br>
* @param srcDirectory 要移动的目录<br>
* @param destDirectory 移动后的目录<br>
* 2006-6-9 <br>
* 18:15:59 <br>
* @author csj <br>
*/
public void copyDirectory(File srcDirectory, File destDirectory){
/**
* 得到目录下的文件和目录数组
*/
//File srcDir = new File(srcDirectory);
//File[] fileList = srcDir.listFiles();
File[] fileList = srcDirectory.listFiles();
/**
* 循环处理数组
*/
for (int i = 0; i < fileList.length; i++) {
if (fileList[i].isFile()) {
/**
* 数组中的对象为文件
* 如果目标目录不存在,
* 创建目标目录
*/
/*
* File descDir = new File(destDirectory);
* if (!descDir.exists()) {
* descDir.mkdir();
* }
*/
if (!destDirectory.exists()) {
destDirectory.mkdir();
}
/**
* 复制文件到目标目录
*/
//this.copyBigFile(srcDirectory + File.separatorChar + fileList[i].getName(),destDirectory + File.separatorChar + fileList[i].getName());
this.copyBigFile(srcDirectory.getAbsolutePath() + File.separatorChar + fileList[i].getName(),destDirectory.getAbsolutePath() + File.separatorChar + fileList[i].getName());
} else {
/**
* 数组中的对象为目录
* 如果该子目录不存在就创建
* (其中也包含了对多级目录的处理)
*/
//File subDir = new File(destDirectory + File.separatorChar + fileList[i].getName());
File subDir = new File(destDirectory.getAbsolutePath() + File.separatorChar + fileList[i].getName());
if (!subDir.exists()) {
subDir.mkdir();
}
/**
* 递归处理子目录
* 循环调用自己
*/
//this.copyDirectory(srcDirectory + File.separatorChar + fileList[i].getName(),destDirectory + File.separatorChar + fileList[i].getName());
this.copyDirectory(new File(srcDirectory.getAbsolutePath() + File.separatorChar + fileList[i].getName()),new File(destDirectory.getAbsolutePath() + File.separatorChar + fileList[i].getName()));
}
}
//srcDir = null;
fileList = null;
}
/**
* 功能:<br>
* 利用nio快速剪切目录<br>
* @param srcDirectory 要移动的目录<br>
* @param destDirectory 移动后的目录<br>
* 2006-6-9 <br>
* 18:15:59 <br>
* @author csj <br>
*/
public void cutDirectory(String srcDirectory, String destDirectory){
this.cutDirectory(new File(srcDirectory),new File(destDirectory));
}
/**
* 功能:<br>
* 利用nio快速剪切目录<br>
* @param srcDirectory 要移动的目录<br>
* @param destDirectory 移动后的目录<br>
* 2006-6-9 <br>
* 18:15:59 <br>
* @author csj <br>
*/
public void cutDirectory(File srcDirectory, File destDirectory){
this.copyDirectory(srcDirectory,destDirectory);
srcDirectory.delete();
}
}
2/3
因为没说指定它们的类除数和被除数的类型所以默认为int类型,所以2/3使终为0,不好意思犯这样的错,我改了放在下面,希望你们还没出什么大问题。Runtime.getRuntime().freeMemory()*((long)(2f/3f))
ByteBuffer.allocate((new Long(this.freeMemory() * ((long)(2f/3f)))).intValue())
再次抱歉......
return (Runtime.getRuntime().freeMemory()) * (2/3);buffer=ByteBuffer.allocate((new Long(this.freeMemory() * (2/3))).intValue());
2/3
因为没说指定它们的类除数和被除数的类型所以默认为int类型,所以2/3使终为0,不好意思犯这样的错,我改了放在下面,希望你们还没出什么大问题。 private long freeMemory(){
long size=(long)(Runtime.getRuntime().freeMemory()*(1.8f/3f));
if(size>0){
return size;
}
return 1l;
}ByteBuffer.allocate((new Long((long)(this.freeMemory() * (2f/3f)))).intValue());再次抱歉......
private long freeMemory(){
long size=(long)(Runtime.getRuntime().freeMemory()*(1.8f/3f));
if(size>0){
return size;
}
return 1l;
} ByteBuffer.allocate((new Long((long)(this.freeMemory() * (2f/3f)))).intValue());