这是一个简单的模拟网络硬盘的功能具体实现。在实现网络硬盘功能的系统中经常会遇到关于文件的各种操作:创建文件,删除文件,读取文件等。为了提高程序性能的阅读性、维护性,通常会在系统中创建一个工具类FileUtils.java来实现对文件内容的各种操作。
下面我贴上该类的具体内容实现代码。
package com.bkybk.webdisk.util;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.Writer;
public class FileUtils {
/**
* 得到路径下对应所有的文件及文件夹
* @param path
* @return
*/
public static File[] fileList(String path) {
File file = new File(path);
return file.listFiles();
}
/**
* 得到路径下符合条件的文件和文件夹
* @param path
* @param cond
* */
public static File[] fileList(String path, final String cond) {
File file = new File(path);
return file.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.indexOf(cond) >= 0;
}
});
}
/**
* 读取文件
* @param path
* @return
* @throws IOException
* */
public static String readFile(String path) throws IOException {
File file = new File(path);
StringBuffer strb = new StringBuffer();
Reader reader = null;
BufferedReader breader = null;
try {
reader = new FileReader(file);
breader = new BufferedReader(reader);
String strline = null;
while ((strline = breader.readLine()) != null) {
strb.append(strline + "\n");
}
} catch (FileNotFoundException fnfe) {
throw new FileNotFoundException("文件不能被发现");
} catch (IOException ioe) {
throw new IOException("文件读取中出现了异常,请稍后在试。");
} finally {
if (breader != null) {
breader.close();
}
if (reader != null) {
reader.close();
}
}
return strb.toString();
}
/**
* 写文件
* @param path
* @author content
* @throws IOException
* */
public static void writerFile(String path, String content) throws IOException {
File file = new File(path);
Writer writer = null;
BufferedWriter bwriter = null;
PrintWriter out = null;
try {
writer = new FileWriter(file, true);
bwriter = new BufferedWriter(writer);
out = new PrintWriter(bwriter);
out.println(content);
} catch (IOException e) {
throw new IOException("写文件时出现了异常,请稍候在试!");
} finally {
if (out != null ) {
out.close();
}
if (bwriter != null) {
bwriter.close();
}
if (writer != null) {
writer.close();
}
}
}
/**
* 创建文件
* @param path
* @return
* @throws IOException
*/
public static boolean createFile(String path) throws IOException {
File file = new File(path);
try {
return file.createNewFile();
} catch (IOException ioe) {
throw new IOException("文件创建失败!");
}
}
/**
* 创建多个文件夹
* @param path
* @return
*/
public static boolean mkdirs(String path){
File file = new File(path);
return file.mkdirs();
}
/**
* 更换名称
* @param oldpath
* @param newfname
* @return
*/
public static boolean renameFile(String oldpath, String newfname){
File oldfile = new File(oldpath);
File newfile = new File(oldpath.substring(0, oldpath.indexOf("\\")));
return oldfile.renameTo(newfile);
}
/**
* 删除文件及文件夹
* @param path
* @return
*/
public static boolean delete(String path){
File file = new File(path);
return file.delete();
}
}上面的代码中,通过JDK提供的关于IO和File的API类,实现一些常用的操作文件的功能。各个方法的具体作用如下:
- fileList(path):获取路径下面的所有文件和文件夹
- fileList(path,cond):获取路径下面符合条件的文件和文件夹
- readFile():读取文件
- writeFile():写入文件
- createFile():创建文件
- mkdirs():创建多个文件夹
- delete():删除文件功能
评论