以前写的一个对文件内容的操作的公有类,但未考虑多用户对同一文件操作,每思至此,还是在博客写得东西,记录一些东西,我喜欢还是直接上代码比较直观。
package com.manage.tpl;
import java.util.List;
import org.springframework.web.multipart.MultipartFile;
/**
* 模板Service
*
* @author liufang
*
*/
public interface TplManager {
/**
* 获得模板列表。根据前缀,用于选择模板。
*
* @param prefix
* 前缀
* @return
*/
public List<? extends Tpl> getListByPrefix(String prefix);
public List<String> getNameListByPrefix(String prefix);
/**
* 获得下级模板列表。根据路径,用于展现下级目录和文件。
*
* @param path
* 路径
* @return
*/
public List<? extends Tpl> getChild(String path);
/**
* 保存模板
*
* @param name
* 模板名称
* @param source
* 模板内容
* @param isDirecotry
* 是否目录
*/
public void save(String name, String source, boolean isDirectory);
/**
* 保存模板
*
* @param path
* @param file
*/
public void save(String path, String file);
/**
* 获得模板
*
* @param 文件路径
* @return
*/
public Tpl get(String path);
/**
* 更新模板
*
* @param name
* 模板文件路径
* @param source
* 模板内容
*/
public void update(String path, String source);
/**
* 修改模板名称或路径
*
* @param orig源
* @param dist目标
*/
public void rename(String orig, String dist);
/**
* 删除模板
*
* @param names
* 模板名称数组
* @return 被删除的模板数量
*/
public int delete(String[] names);
/**
* 查询文件路径是否符合归则
* 返回true 或false
* true
*
*/
public boolean isCheckPath(String path);
}
package com.manage.tpl;
import java.util.Date;
/**
* 模板接口
*
* @author liufang
*
*/
public interface Tpl {
/**
* 获得模板完整名称,是文件的唯一标识。
*
* @return
*/
public String getName();
/**
* 获得路径,不包含文件名的路径。
*
* @return
*/
public String getPath();
/**
* 获得模板名称,不包含路径的文件名。
*
* @return
*/
public String getFilename();
/**
* 获得模板内容
*
* @return
*/
public String getSource();
/**
* 获得最后修改时间(毫秒)
*
* @return
*/
public long getLastModified();
/**
* 获得最后修改时间(日期)
*
* @return
*/
public String getLastModifiedDate();
/**
* 获得文件大小,单位bytes
*
* @return
*/
public long getLength();
/**
* 获得文件大小,单位K bytes
*
* @return
*/
public int getSize();
/**
* 是否目录
*
* @return
*/
public boolean isDirectory();
}
package com.manage.tpl;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
public class FileTplManagerImpl implements TplManager {
private Logger log = Logger.getLogger(this.getClass());
/**
* 删除文件或文件夹
*/
private String root;// 应用所在绝对路径
public FileTplManagerImpl(String root) {
this.root = root;
}
@Override
public int delete(String[] names) {
// TODO Auto-generated method stub
File file = null;
int count = 0;
for (String name : names) {
name = this.getRoot() + "/" + name;
file = new File(name);
if (file.isDirectory()) {
try {
FileUtils.deleteDirectory(file);
count++;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else if (file.isFile()) {
file.delete();
count++;
}
}
return 0;
}
/**
* 获取单个文件
*/
@Override
public Tpl get(String path) {
// TODO Auto-generated method stub
File file = new File(path);
if (file.exists()) {
return new FileTpl(file, path);
}
return null;
}
public List<Tpl> getChild(String path) {
File file = new File(path);
File[] child = file.listFiles();
if (child != null) {
List<Tpl> list = new ArrayList<Tpl>();
for (File f : child) {
if (f.exists()) {
list.add(new FileTpl(f, f.getPath()));
}
}
return list;
} else {
return new ArrayList<Tpl>(0);
}
}
@Override
public List<? extends Tpl> getListByPrefix(String prefix) {
return null;
}
@Override
public List<String> getNameListByPrefix(String prefix) {
// TODO Auto-generated method stub
return null;
}
@Override
public void rename(String orig, String dist) {
File source = new File(this.getRoot() + "/" + orig);
File targer = new File(this.getRoot() + "/" + dist);
source.renameTo(targer);
}
@Override
public void save(String path, String source, boolean isDirectory) {
File file = new File(path);
if (isDirectory) {
if (!file.exists()) {
file.mkdir();// 创建目录
}
} else {
try {
if (!file.exists()) {
file.createNewFile();
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
BufferedWriter writer = null;
FileOutputStream output =null;
OutputStreamWriter stream =null;
try {
output =new FileOutputStream(path);
stream =new OutputStreamWriter(output,"UTF-8");
writer = new BufferedWriter(stream);
writer.write(source);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
writer.close();
stream.close();
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Override
public void update(String path, String source) {
File file = new File(path);
if (file.isFile() && file.exists()) {
BufferedWriter writer = null;
FileOutputStream output =null;
OutputStreamWriter stream =null;
try {
output =new FileOutputStream(path);
stream =new OutputStreamWriter(output,"UTF-8");
writer = new BufferedWriter(stream);
writer.write(source);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
writer.close();
stream.close();
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Override
public void save(String path, String source) {
// TODO Auto-generated method stub
}
@Override
public boolean isCheckPath(String path) {
// TODO Auto-generated method stub
if (path != null && !path.trim().equals("")
&& path.startsWith(this.getRoot())) {
return true;
}
return false;
}
public String getRoot() {
return root;
}
public void setRoot(String root) {
this.root = root;
}
}
package com.manage.tpl;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.io.FileUtils;
import com.manage.util.Constant;
public class FileTpl implements Tpl {
private File file;
// 应用的根目录
private String root;
public FileTpl(File file, String root) {
this.file = file;
this.root = root;
}
public String getName() {
return file.getName();
}
public String getPath() {
return "/"+getName();
}
public String getFilename() {
return file.getName();
}
public String getSource() {
if (file.isDirectory()) {
return null;
}
try {
return FileUtils.readFileToString(this.file, Constant.TPL_ENCODE);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public String getLastModifiedDate() {
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return format.format(new Date(this.getLastModified()));
}
public long getLength() {
return file.length();
}
public int getSize() {
return (int) (getLength() / 1024) + 1;
}
public boolean isDirectory() {
return file.isDirectory();
}
@Override
public long getLastModified() {
// TODO Auto-generated method stub
return file.lastModified();
}
}
package com.manage.tpl;
import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;
import org.apache.commons.io.FileUtils;
import com.manage.util.Constant;
/**
* 父目录是文件异常
*
* 当移动文件或创建文件时,父目录也是文件,可以导致该异常。
*
* @author liufang
*
*/
@SuppressWarnings("serial")
public class ParentDirIsFileExceptioin extends RuntimeException {
private String parentDir;
/**
* @param parentDir
* The parent dir, which is a file.
*/
public ParentDirIsFileExceptioin(String parentDir) {
this.parentDir = parentDir;
}
@Override
public String getMessage() {
return "parent directory is a file: " + parentDir;
}
/**
* Get the parent dir, which is a file.
*
* @return
*/
public String getParentDir() {
return parentDir;
}
}
分享到:
相关推荐
主要介绍了Java基于IO流读取文件的方法,涉及Java文件流操作的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
文件流 读写操作,对java.io.*;中文件读写的一些相关信息
java I\O流及文件操作,是初学者很好的资源。
java IO流操作,包括文件的读取,写入,上传和下载,支持文本数据和音频等文件
主要介绍了Java IO流和文件操作实现过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
关于java的IO流基础的练习,适合刚接触IO流,没有基础的同学
文件操作 IO流(File FileOutputStream OutputStreamWrite BufferWrite ......)代码及注释详解
完整的文件io 流操作,有四个函数, Writer read inputStream outputStream自己总结的东东,让大家用用,不图得分,
JAVA IO流文件读写,字节流字符流的转换、读取、存盘,不乱码操作,文件移动读取操作,使用了多种流机制
对Java IO流操作感兴趣的开发人员 需要进行文件读写、网络传输等IO操作的程序员 使用场景 该代码资源可以用于教学、自学以及实际项目开发中的参考。 教学:作为教学示例演示给学生 自学:通过查看示例代码来学习和...
// //操作流 法一 //// int b = 0; //// while((b = in.read()) != -1){ //// out.write(b); //// } // //操作流 法二 //// byte[] b = new byte[1024]; //// while(in.read(b) != -1){ //// ...
一、 流的分类 • 按数据流动方向 – 输入流:只能从中读取字节数据,而不能向其写出数据 – 输出流:只能向其写入字节数据,而不能从中读取数据 ...– 节点流:从/向一个特定的IO设备读/写数据的流。(低级流) – 处理
对于文本文件(.txt,.java,.c,.cpp),使用字符流处理 对于非文本文件(.jpg,.mp3,.mp4,.avi,.doc,.ppt,...),使用字节流处理 2.数据的流向:输入流、输出流 3.流的角色:节点流、处理流 节点流:直接从数据...
io流中的文件操作,直接粘贴去eclipse或者cmd就可以运行。
javaIO操作各种文件,筛选文件,拷贝文件,上传下载,字符流和字节流都有,根据实际项目情况直接贴就可以了,根据文件类型选择不同的testclss就可以。全部使用效率最高的流
主要做了对JAVA-IO流主要类的操作demo,包括了对TXT文件的操作,主要是对博客http://blog.csdn.net/yczz/article/details/38761237的代码重现,方便阅读,供以后开发参考。
Java IO文件操作类与复制文件实例,有关文本编辑、FileWriter文件写入、FileReade文件读娶DataStream文件数据流、BufferedReader文件暂存器、FileViewer文件浏览器、复制文件Copy等方面的操作要领,仅供学习参考。
主要介绍了java IO流文件的读写具体实例,有需要的朋友可以参考一下
学习Java IO,不得不提到的就是JavaIO流。 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象。即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输特性将流抽象为各种类,方便更...
基于java文件流操作IO.pdf