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

📄 readfileutil.java

📁 Java SWT代码
💻 JAVA
字号:
package com;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.net.URL;
import java.util.Iterator;
import java.util.List;

import util.Dialog;

/**
 * 
 * @author NJ
 *
 */
public class ReadFileUtil {
	
	public static String getPathAndFile(List<String> listRootFile,String strFile){
		for (Iterator iter = listRootFile.iterator(); iter.hasNext();) {
			String strRootFile = (String) iter.next();
			if(strFile.indexOf(strRootFile)==0){
				strFile=strFile.substring(strRootFile.length(), strFile.length());
				return strFile;
			}
		}
		return "";
	}
	
	/**
	 * 拷贝文件
	 * @param filePc
	 * @param fileMobile
	 * @throws Exception
	 */
	public static void copyFile(File filePc,File fileMobile,boolean isCoypHidden) throws Exception{
		 
		FileInputStream fis=null;
		BufferedInputStream bis=null;
		FileOutputStream fos=null;
		BufferedOutputStream bos=null;
		
		String strPath=fileMobile.getPath();
		
		File fileTest=new File(strPath.substring(0,strPath.lastIndexOf("\\")));
		
		if(!fileTest.canExecute() || !fileTest.canRead()){
			fileTest.mkdirs();
		}
		
		if(filePc.isHidden() && !isCoypHidden){
			return;
		}
		
		if(!filePc.canRead()){
			return;
		}
		
		fis=new FileInputStream(filePc);
		
		bis=new BufferedInputStream(fis);
		  
		fos=new FileOutputStream(fileMobile);
		
		bos=new BufferedOutputStream(fos);
		
		byte[] bytes=new byte[1024];
		int c;
		while((c=bis.read(bytes))!=-1)
			bos.write(bytes,0,c);
		
		if(fis!=null)
			fis.close();
		if(bis!=null)
			bis.close();
		if(bos!=null)
			bos.close();
		if(fos!=null)
			fos.close();
	}
	
	public static File[] readFile(String strPath){
		File file=new File(strPath); 
		if(file.isDirectory()){
			File files[]=file.listFiles();
			return files;
		}else{
			return null;
		}
	}
	
	public static void createFile(String strFileName) throws Exception{
		File file=new File(strFileName); 
		if(file.canRead()){
			Dialog.openInformationDialog("该文件已经存在不能新增!");
		}else{
			file.createNewFile(); 
		}
	}
	
	public static String getPath(){
		try {
			URL url=ReadFileUtil.class.getResource("");
			  
			String path=url.getPath();
			
			path=path.replaceAll("file:", "");
			
			path=path.substring(0, path.indexOf(":")+2);
			
			path=path+"Temp/resource/";
			
			if (path.length() > 0 && path.charAt(0) == '/') {
				path = path.substring(1, path.length());
			}
			
			File file=new File(path);
			
			if(!file.canRead()){
				file.mkdirs();
				
				createFile(path+InitProperties.TYPE_MOBILE2PC);
				createFile(path+InitProperties.TYPE_PC2MOBILE);
			} 
			return path;
		} catch (Exception e) {
			Dialog.openInformationDialog(e.getMessage());
			return null;
		}
	}
	 

	public static File getFile(String strType) throws Exception {
		 
		String path=getPath()+strType; 
		
		File file = new File(path);
		if (!file.isFile()) {
			file.createNewFile();
		}
		return file;
	} 
	
	public static String getStatus(File file){
		return ""/*(file.getTotalSpace()-file.getUsableSpace())/1024+"KB"*/;
	}
}

⌨️ 快捷键说明

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