用JAVA制作一个文件系统管理器文件目录浏览,创建目录,移动文件,文件改名,文件删除等等功能
发布网友
发布时间:2022-04-30 00:34
我来回答
共4个回答
热心网友
时间:2023-10-11 03:53
/**
* 取得当前目录下文件对象
* @return
*/
public static Iterator getFiles(File currentFile) {
Vector vector = new Vector();
File afile[] = currentFile.listFiles();
for (int i = 0; i < afile.length; i++)
if (afile[i].isFile())
vector.add(afile[i]);
return vector.iterator();
}
/**
* 取得当前目录下的子目录对象列表
* @return
*/
public static Iterator getAllDirectories(File currentFile) {
Vector vector = new Vector();
File afile[] = currentFile.listFiles();
//vector.add(new File(documentRoot, relativeFile + File.separator + "."));
try {
vector.add(new File(currentFile.getCanonicalFile() + File.separator + ".."));
for (int i = 0; i < afile.length; i++)
if (afile[i].isDirectory())
vector.add(afile[i]);
} catch (IOException e) {
if (log.isErrorEnabled()) log.error(e);
}
return vector.iterator();
}
/**
* 取得当前目录下的子目录对象列表
* @return
*/
public static Iterator getDirectories(File currentFile) {
Vector vector = new Vector();
File afile[] = currentFile.listFiles();
for (int i = 0; i < afile.length; i++)
if (afile[i].isDirectory())
vector.add(afile[i]);
return vector.iterator();
}
/**
* 将内容写入文件
* @param file
* @param content
* @throws IOException
*/
public static void writeFile(String file, String content)
throws IOException {
PrintWriter printwriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file),BIND_ENCODING)));
printwriter.write(content);
printwriter.close();
if (printwriter.checkError())
if (log.isErrorEnabled())
log.error("Error encountered while writing the file!");
else
return;
}
代码太长,分批发.
热心网友
时间:2023-10-11 03:53
package com.db.util;
import java.io.*;
/**
*
* 遍历指定文件夹
* @author Ben
*
*/
public class FileHeap
{
private File[] data;
private int size;
public FileHeap()
{
this(10);
}
public FileHeap(int size)
{
data = new File[size];
}
// 入栈
public void push(File c)
{
ensureCapacity(size + 1);
data[size++] = c;
}
// 出栈
public File pop()
{
return data[--size];
}
public boolean isEmpty()
{
return size <= 0;
}
// 扩展容量
private void ensureCapacity(int minSize)
{
int oldCapacity = data.length;
if (minSize > oldCapacity)
{
int newCapacity = (oldCapacity * 3 / 2 + 1) > minSize ? oldCapacity * 3 / 2 + 1
: minSize;
File[] copy = new File[newCapacity];
System.arraycopy(data, 0, copy, 0, data.length);
data = copy;
}
}
public static void main(String[] args)
{
FileHeap heap = new FileHeap(); // 堆栈结构,自定义
File file = new File("C:/");
heap.push(file);
while (!heap.isEmpty())
{
File[] files = heap.pop().listFiles();
if (files != null)
for (File tempFile : files)
{
if (tempFile.isDirectory())
heap.push(tempFile);
System.out.println(tempFile.getAbsolutePath());
}
}
}
}
热心网友
时间:2023-10-11 03:53
FileInfo
Directory
这两个类应该能满足你
不行的话
你搜下
java文件操作什么都有了
不行可以问我
热心网友
时间:2023-10-11 03:55
http://game.3533.com/ppcdown.asp?ppcid=3094这里是一个java的文件管理器 http://download.csdn.net/source/290847这里是源码