设为首页 收藏本站
查看: 1023|回复: 0

[经验分享] apache commons-vfs简单用法

[复制链接]

尚未签到

发表于 2017-1-1 11:25:31 | 显示全部楼层 |阅读模式
这段时间又开始忙起来了,所以apache commons-vfs源码赏析就暂时搁置起来了,在这里先写一下apache commons-vfs的一些简单的用法,也可以去参考apache commons-vfs源文件中自己提供的一些demo.废话不多说,直接上代码

package com.zzg.util;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.vfs.FileObject;
import org.apache.commons.vfs.FileSystemException;
import org.apache.commons.vfs.FileSystemManager;
import org.apache.commons.vfs.FileType;
import org.apache.commons.vfs.Selectors;
import org.apache.commons.vfs.VFS;
public class FileUtil {
private static FileSystemManager fsm = null;
static {
try {
fsm = VFS.getManager();
} catch (FileSystemException e) {
e.printStackTrace();
}
}
public static FileObject getFile(String path){
try {
return fsm.resolveFile(path);
} catch (FileSystemException e) {
e.printStackTrace();
return null;
}
}
public static void delete(String path) {
try {
FileObject fo = fsm.resolveFile(path);
fo.delete();
} catch (FileSystemException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static boolean isDirectory(String path) {
try {
FileObject fo = fsm.resolveFile(path);
return fo.getType().equals(FileType.FOLDER);
} catch (FileSystemException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
public static InputStream getInputStream(String path){
try {
FileObject fo = fsm.resolveFile(path);
return fo.getContent().getInputStream();
} catch (FileSystemException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static OutputStream getOutputStream(String path){
try {
FileObject fo = fsm.resolveFile(path);
return fo.getContent().getOutputStream();
} catch (FileSystemException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static boolean isFile(String path) {
try {
FileObject fo = fsm.resolveFile(path);
return fo.getType().equals(FileType.FILE);
} catch (FileSystemException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
/**
* 函数描述:根据传入的文件路径创建文件夹(包括各级父文件夹)。如果路径中有文件,会自动去掉文件名。 (文件的判断是
* 以最后一个"/"之后是否有"."为标识的,)
*
* @param path
* @return 如果创建成功,返回true;否则,返回false;
*/
public static boolean mkdirs(String path) {
String realPath = "";
path = path.replaceAll("\\\\", "/");
// 如果该路径已"/"结尾,则整个字符串都是路径
if (path.endsWith("/")) {
realPath = path;
} else {
int fileNamePoint = path.lastIndexOf("/");
// 获取真正的路径
if (fileNamePoint >= 0) {
realPath = path.substring(0, fileNamePoint);
}
// 读取文件名
String fileName = path.substring(fileNamePoint + 1);
// 如果读取的文件名中没有".",说明整个字符串都是路径
if (fileName.indexOf(".") < 0) {
realPath = path;
}
}
try {
FileObject fo = fsm.resolveFile(realPath);
fo.createFolder();
return true;
} catch (Exception e) {
return false;
}
}
/**
* 函数描述:对文件进行copy
*
* @param sourceFilePath
*            源文件的全部路径+文件名
* @param targetFilePath
*            目标文件的全部路径+文件名
* @param overWrite
*            如果目标文件存在,是否覆盖。true:覆盖;false:不覆盖(当源文件和目标文件都存在并且不覆盖时,返回true)。
* @return true:成功;false:失败; (当源文件和目标文件都存在并且不覆盖时,返回true)。
*/
public static boolean copyFile(String sourceFilePath, String targetFilePath, boolean overWrite) throws IOException {
if (StringUtils.isBlank(sourceFilePath) || StringUtils.isBlank(targetFilePath)) {
throw new IOException("源文件或者目标文件为空");
}
FileObject from = fsm.resolveFile(sourceFilePath);
FileObject to = fsm.resolveFile(targetFilePath);
if (to.exists()) {
if (to.getType() == FileType.FILE) {
if (overWrite && !to.delete()) {
throw new IOException("目标文件[" + targetFilePath + "]被保护,不能被覆盖!");
} else if (!overWrite) {
throw new IOException("目标文件[" + targetFilePath + "]已经存在!");
}
}
}
to.copyFrom(from, Selectors.SELECT_ALL);
return true;
}
/**
* 函数描述:
*
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
//copyFile("e:/crpt.txt","e:/crpt",true);
moveFile("M:/u-dsm/store/store/enterprise/a9845232-bea2-4d9e-92be-d16cfcdcea01/ad5b00475fd0408aaed2eb195bf90913.docx","M:/u-dms/a9845232-bea2-4d9e-92be-d16cfcdcea01/ad5b00475fd0408aaed2eb195bf90913.docx", true);
// copyFile("ftp://zzg:zzg@192.168.1.100/test/teeee/fff","smb://user:user@192.168.1.100/共享/1.txt",true);
// copyFile("smb://user:user@192.168.1.100/共享/1.txt","file://e:/ffff",true);
//moveFile("ftp://zzg:zzg@192.168.1.100/test1/teeee2/fff1", "ftp://unis:unis@192.168.1.100/test/teeee/fff", true);
// copyFile("file://e:/crpt.txt","file://e:/crpt",true);
}
/**
* Moving a File to Another File ,没有进行磁盘空间大小的判断
*
* @param srcFile
*            源文件 eg: c:\windows\abc.txt
* @param targetFile
*            目标文件 eg: c:\temp\abc.txt
* @param overwrite
*            如果目标文件存在,是否覆盖
* @return success
*/
public static boolean moveFile(String srcFile, String targetFile, boolean overWrite) throws IOException {
if (srcFile.equals(targetFile)) {
return true;
}
FileObject src = fsm.resolveFile(srcFile);
// File (or directory) to be moved
if (StringUtils.isNotBlank(srcFile) && !src.exists()) {
throw new IOException("源文件[" + srcFile + "]不存在");
}
// Destination directory
FileObject to = fsm.resolveFile(targetFile);
if (to.exists()) {
if (to.getType() == FileType.FILE) {
if (overWrite && !to.delete()) {
throw new IOException("目标文件[" + targetFile + "]被保护,不能被覆盖!");
} else if (!overWrite) {
throw new IOException("目标文件[" + targetFile + "]已经存在!");
}
}
}
src.moveTo(to);
return true;
}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-322408-1-1.html 上篇帖子: Apache POI 下篇帖子: [转]Apache Log4j使用实例
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表