JAVA 操作檔案

Allen_liyu發表於2017-08-15
public class demo {

	public static void main(String[] args) {
		//boolean result =createFile("D:\\360\\a.txt");
		//boolean result = createDirectory("D:\\360\\a");
		//boolean result = deleteFile("D:\\360\\a");
		//deleteDirectory("D:\\360\\a");

	}
	//建立檔案
	public static boolean createFile(String filePath){
		boolean result = false;
		File file = new File(filePath);
		if(!file.exists()){
			try {
				result = file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return result;
	}
	//建立資料夾
	public static boolean createDirectory(String directory){
		boolean result = false;
		File file = new File(directory);
		if(!file.exists()){
			result = file.mkdirs();
		}
		return result;
	}
	//刪除檔案
	public static boolean deleteFile(String filePath){
		boolean result = false;
		File file = new File(filePath);
		if(file.exists()&&file.isFile()){
			result = file.delete();
		}
		return result;
	}
	//刪除資料夾 遞迴刪除資料夾下面的子檔案和資料夾
	public static void deleteDirectory(String filePath){  
	    File file = new File(filePath);  
	    if(!file.exists()){  
	        return;  
	    }  
	    if(file.isDirectory()){
	    	  File[] files = file.listFiles();  
	          for (File myfile : files) {  
	              deleteDirectory(filePath + "\\" + myfile.getName());  
	          }  
	    }
	    file.delete();
	} 
	//讀檔案:1.以位元組為單位讀取檔案,常用於讀二進位制檔案,如圖片、聲音、影像等檔案
	public static String readFileByBytes(String filePath){  
	    File file = new File(filePath);  
	    if(!file.exists() || !file.isFile()){  
	        return null;  
	    }  
	      
	    StringBuffer content = new StringBuffer();  
	      
	    try {  
	        byte[] temp = new byte[1024];  
	        FileInputStream fileInputStream = new FileInputStream(file);  
	        while(fileInputStream.read(temp) != -1){  
	            content.append(new String(temp));  
	            temp = new byte[1024];  
	        }  
	          
	        fileInputStream.close();  
	    } catch (FileNotFoundException e) {  
	        e.printStackTrace();  
	    } catch (IOException e) {  
	        e.printStackTrace();  
	    }  
	      
	    return content.toString();  
	}  
	//2.以字元為單位讀取檔案,常用於讀文字,數字等型別的檔案,支援中文
	public static String readFileByChars(String filePath){
		File file = new File(filePath);
		if(!file.exists() || !file.isFile()){
			return null;
		}
		StringBuffer content = new StringBuffer();
		char[] temp = new char[1024];
		FileInputStream fileInputStream;
		try {
			fileInputStream = new FileInputStream(file);
			InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream,"GBK");
			while(inputStreamReader.read(temp)!=-1){
				content.append(new String(temp));
				temp = new char[1024];
			}
			fileInputStream.close();
			inputStreamReader.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}catch (IOException e) {
			e.printStackTrace();
		}
		
		return content.toString();
	}
	//3.以行為單位讀取檔案,常用於讀面向行的格式化檔案
	public static List<String> readFileByLines(String filePath){
		File file = new File(filePath);
		if(!file.exists() || !file.isFile()){
			return null;
		}
		List<String> content = new ArrayList<String>();
		try{
			FileInputStream fileInputStream = new FileInputStream(file);
			InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream,"GBK");
			BufferedReader reader = new BufferedReader(inputStreamReader);
			String lineContent = "";
			while((lineContent=reader.readLine()) !=null){
				content.add(lineContent);
				System.out.println(lineContent);
			}
			fileInputStream.close();
			inputStreamReader.close();
			reader.close();
		}catch(FileNotFoundException e){
			e.printStackTrace();
		}catch(IOException e){
			e.printStackTrace();
		}
		return content;
	}
	//6.寫檔案,字串寫入檔案的幾個類中,FileWriter效率最高,BufferedOutputStream次之,FileOutputStream最差。
	//1.通過FileOutStream寫入檔案
	public static void writeFileByFileOutputStram(String filePath,String content)throws  IOException{
		File file = new File(filePath);
		synchronized (file) {
			FileOutputStream fos = new FileOutputStream(filePath);
			fos.write(content.getBytes("GBK"));
			fos.close();
		}
	}
	//2.通過BufferedOutputStream寫入檔案
	public static void writeFileByBufferedOutputStream(String filePath,String content)throws IOException{
		File file = new File(filePath);
		synchronized (file) {
			BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(filePath));
			fos.write(content.getBytes("GBK"));
			fos.flush();
			fos.close();
		}
	}
	//3.通過FileWriter將字串寫入檔案
	public static void writeFileByWriter(String filePath,String content)throws IOException{
		File file = new File(filePath);
		synchronized (file) {
			FileWriter fw = new FileWriter(filePath);
			fw.write(content);
			fw.close();
		}
		
	}
}

相關文章