论坛首页 Java企业应用论坛

Java io流对文件的操作

浏览 2886 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2011-06-04   最后修改:2011-06-04
以前写的一个对文件内容的操作的公有类,但未考虑多用户对同一文件操作,每思至此,还是在博客写得东西,记录一些东西,我喜欢还是直接上代码比较直观。
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;
	}
}

   发表时间:2011-08-03  
天啊   我喜欢。。。顶楼主,
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics