通过VFS对文件进行一些操作,包括写入、读取文件,判断文件是否可读可写等示例。Apache commons VFS包和apache commons的其他基础性类有非常密切的关系。
整理了一些常用的方法,源代码如下:
package test.ffm83.commons.VFS;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemException;
import org.apache.commons.vfs2.FileSystemManager;
import org.apache.commons.vfs2.FileType;
import org.apache.commons.vfs2.VFS;
/**
* 通过VFS对文件进行一些操作,包括写入、读取文件,判断文件是否可读可写等使用2.0版本实现
*
* @author范芳铭
*/
public classVFSUtils {
private static FileSystemManager fsManager = null;
// 静态处理块
static {
try {
fsManager = VFS.getManager();
}catch(FileSystemException e) {
e.printStackTrace();
}
}
/**
* <读取文件内容>
*/
public static StringreadFileToString(String filePath, String encoding)
throws IOException {
if (StringUtils.isEmpty(filePath)){
throw new IOException("File ‘"+ filePath + "‘ is empty.");
}
FileObjectfileObj = null;
InputStreamin = null;
try {
fileObj= fsManager.resolveFile(filePath);
if (fileObj.exists()) {
if (FileType.FOLDER.equals(fileObj.getType())){
throw new IOException("File ‘"+ filePath
+"‘ exists but is a directory");
}else{
in= fileObj.getContent().getInputStream();
return IOUtils.toString(in,encoding);
// 返回List<String>,通过 IOUtils.readLines(in, encoding)的形式;
// 返回byte[],通过IOUtils.toByteArray(in)的形式;
}
}else{
throw new FileNotFoundException("File ‘"+ filePath
+"‘ does not exist");
}
}catch(FileSystemException e) {
throw new IOException("File ‘"+ filePath + "‘ resolveFile fail.");
}finally{
IOUtils.closeQuietly(in);
if (fileObj != null) {
fileObj.close();
}
}
}
/**
* <将内容写入文件中,如果文件不存在,那么创建。> <功能详细描述>
*/
public static voidwriteStringToFile(String filePath, String data,
Stringencoding) throwsIOException {
if (StringUtils.isEmpty(filePath)){
throw new IOException("File ‘"+ filePath + "‘ is empty.");
}
FileObjectfileObj = null;
OutputStreamout = null;
try {
fileObj= fsManager.resolveFile(filePath);
if (!fileObj.exists()) {
fileObj.createFile();
}else{
if (FileType.FOLDER.equals(fileObj.getType())){
throw new IOException("Write fail. File ‘"+ filePath
+"‘ exists but is a directory");
}
}
// 处理文件不可写的异常
if(!fileObj.isWriteable()) {
throw new IOException("Write fail. File ‘"+ filePath
+"‘ exists but isWriteable is false.");
}
out= fileObj.getContent().getOutputStream();
// commons io里的方法
IOUtils.write(data,out, encoding);
}catch(FileSystemException e) {
throw new IOException("File ‘"+ filePath + "‘ resolveFile fail.");
}finally{
IOUtils.closeQuietly(out);
if (fileObj != null) {
fileObj.close();
}
}
}
/**
* <修改文件的最后修改时间,将文件做旧。> <功能详细描述>
*/
public static void_changeLastModificationTime(String filePath,
StringlastTime) throwsException {
if (StringUtils.isEmpty(filePath)|| StringUtils.isEmpty(lastTime)) {
throw new IOException("File ‘"+ filePath + " or "+ lastTime
+"‘ is empty.");
}
SimpleDateFormatsdf = newSimpleDateFormat("yyyy/MM/dd HH:mm:ss");
long lastModifyTime =sdf.parse(lastTime).getTime();
FileObjectfileObj = null;
try {
fileObj= fsManager.resolveFile(filePath);
fileObj.getContent().setLastModifiedTime(lastModifyTime);
}catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
Stringfile_encode = "D:\\ffm83\\ffm_encode.txt";
Stringdata = "范芳铭在做Apache commons VFS简介的测试,关于ffm等";
// write
writeStringToFile(file_encode,data, "utf-8");
System.out.println(StringUtils.center("write file "+ file_encode, 50,
"-"));
// read
System.out.println(StringUtils.center("read file "+ file_encode, 50,
"-"));
StringreadStr = readFileToString(file_encode, "utf-8");
System.out.println("读取的内容为:" + readStr);
System.out.println(file_encode + ",可读?"
+fsManager.resolveFile(file_encode).isReadable());
System.out.println(file_encode + ",可写?"
+fsManager.resolveFile(file_encode).isWriteable());
//修改文件的最后的修改时间
_changeLastModificationTime(file_encode,"2012/1/1 12:12:12");
System.out.println(StringUtils.center("edit lastModifyTime: "+ file_encode, 50,
"-"));
}
}
运行结果如下:
--------write fileD:\ffm83\ffm_encode.txt--------
--------read file D:\ffm83\ffm_encode.txt---------
读取的内容为:范芳铭在做Apachecommons VFS简介的测试,关于ffm等
D:\ffm83\ffm_encode.txt,可读?true
D:\ffm83\ffm_encode.txt,可写?true
---edit lastModifyTime: D:\ffm83\ffm_encode.txt---
打开文件一看,文件的修改时间如期变成了2012年。
原文地址:http://blog.csdn.net/ffm83/article/details/42080387