⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileutil.java

📁 自己做的Jsp网页版文件管理
💻 JAVA
字号:
package cumt.file;
import java.io.*;
import java.util.*;

/**
 * 用来进行常用的文件操作
 * @author mengke
 * @version 2008-3-10
 * @see 帮助文档
 * @since  2008.3.10
 * **/
public class FileUtil {

	final private static int sleetTime=100;//睡眠100毫秒 
	/**
	 * 删除文件操作<br>
	 * 路径为文件绝对路径 在此之前要获取文件路径
	 * @see cumt.Factory.servletPath
	 * **/
	public static boolean fileDelete(String path) {
		
		if(path==null)
		    return false;
		File f = new File(path);
		if (f.exists()) {
			f.delete();
			return true;
		} else
			return false;
		}
	
	public static boolean folderCreate(String path){
		if(path==null)
		    return false;
		File f = new File(path);
		if (!f.exists()) {
			return f.mkdir();			
		} else
			return false;
	}
	
	/**
	 * 判断文件是否<br>
	 * 路径为文件绝对路径 在此之前要获取文件路径
	 * @see cumt.Factory.servletPath
	 * **/
	public static boolean fileExists(String path){
		if(path==null)
		     return false;
		return new File(path).exists();
	}
	
	/**
	 * 完成文件的拷贝工作<br>
	 * 路径为文件绝对路径 在此之前要获取文件路径
	 * @see cumt.Factory.servletPath
	 * **/
	
	   public static boolean fileCopy(String srcpath,String destpath){
			File f=new File(srcpath);
			if(f.exists()){

				try{
					File destFile=new File(destpath);
						if(destFile.exists())
							destFile.delete();
						BufferedInputStream bin=null;
						BufferedOutputStream bout=null;
						try{
						bin=new BufferedInputStream(new FileInputStream(f));
						bout=new BufferedOutputStream(new FileOutputStream(destFile));
						 byte[] buf = new byte[1024];
						 int bytesRead;
						 while(-1 != (bytesRead = bin.read(buf, 0, buf.length))) {
							 bout.write(buf,0,bytesRead);
						 }
						 Thread.sleep(sleetTime);
						 bin.close();
						 bout.close();
						 return true;
						}catch(Exception exc){
							if(bin!=null)
								bin.close();
							if(bout!=null)
								bout.close();
							return false;
						}
						 					
				}catch(Exception e){
					
					return false;
				}
			
				
			}else{
			return false;
			}
	   }
		
		/**
		 * 把文本文件写到目录里去<br>
		 * 路径为文件绝对路径 在此之前要获取文件路径
		 * @see cumt.Factory.servletPath
		 * **/
	   public static boolean writeTextToFile(String s,StringBuffer text){
		   
			File f=new File(s);
			try{

			if(!f.exists()){
				f.createNewFile();
			}
		    PrintWriter out=new PrintWriter(new FileWriter(f));
		    out.append(text);
		    out.flush();
		    out.close();
		    
			return true;
			
			}catch(Exception e){
				return false;
			}
			
	   }
	   
		/**
		 * 获取一个目录下的所有文件名字<br>
		 * 返回 ArrayList<br>
		 * 路径为文件绝对路径 在此之前要获取文件路径
		 * @see cumt.Factory.servletPath
		 * **/
	   public static ArrayList<String> fileNameList(String path){
			File f=new File(path);
			if(f.isDirectory()){
				ArrayList<String> arraylist=new ArrayList<String>();
				File files[]=f.listFiles();
				for(int i=0;i<files.length;i++){
					arraylist.add(files[i].getName());
				}
				return arraylist;
			}else{
				return null;
			}
		}
		/**
		 * 获取一个目录下的所有文件<br>
		 * 返回 ArrayList<ShowFile><br>
		 * 路径为文件绝对路径 在此之前要获取文件路径
		 * @see cumt.Factory.servletPath
		 * @see ShowFile
		 * **/
	   public static ArrayList<ShowFile> listFile(String path){
			File f=new File(path);
			if(f.isDirectory()){
				ArrayList<ShowFile> arraylist=new ArrayList<ShowFile>();
				File files[]=f.listFiles();
				for(int i=0;i<files.length;i++){
					if(files[i].isFile())
					   arraylist.add(new ShowFile( files[i].getName(),files[i].getPath(),files[i].length(),files[i].lastModified(),files[i].isDirectory()));
				}
				return arraylist;
			}else{
				return null;
			}
		}
	   
	   
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -