Java學習第六課 JAVA 文件操作

平常經常使用JAVA對文件進行讀寫等操作,這裏彙總一下常用的文件操作。

1、創建文件

[java] view plaincopy
  1. public static boolean createFile(String filePath){  
  2.     boolean result = false;  
  3.     File file = new File(filePath);  
  4.     if(!file.exists()){  
  5.         try {  
  6.             result = file.createNewFile();  
  7.         } catch (IOException e) {  
  8.             e.printStackTrace();  
  9.         }  
  10.     }  
  11.       
  12.     return result;  
  13. }  

2、創建文件夾

[java] view plaincopy
  1. public static boolean createDirectory(String directory){  
  2.     boolean result = false;  
  3.     File file = new File(directory);  
  4.     if(!file.exists()){  
  5.         result = file.mkdirs();  
  6.     }  
  7.       
  8.     return result;  
  9. }  

3、刪除文件

[java] view plaincopy
  1. public static boolean deleteFile(String filePath){  
  2.     boolean result = false;  
  3.     File file = new File(filePath);  
  4.     if(file.exists() && file.isFile()){  
  5.         result = file.delete();  
  6.     }  
  7.       
  8.     return result;  
  9. }  

4、刪除文件夾

遞歸刪除文件夾下面的子文件和文件夾

[java] view plaincopy
  1. public static void deleteDirectory(String filePath){  
  2.     File file = new File(filePath);  
  3.     if(!file.exists()){  
  4.         return;  
  5.     }  
  6.       
  7.     if(file.isFile()){  
  8.         file.delete();  
  9.     }else if(file.isDirectory()){  
  10.         File[] files = file.listFiles();  
  11.         for (File myfile : files) {  
  12.             deleteDirectory(filePath + "/" + myfile.getName());  
  13.         }  
  14.           
  15.         file.delete();  
  16.     }  
  17. }  

5、讀文件

(1)以字節爲單位讀取文件,常用於讀二進制文件,如圖片、聲音、影像等文件

[java] view plaincopy
  1. public static String readFileByBytes(String filePath){  
  2.     File file = new File(filePath);  
  3.     if(!file.exists() || !file.isFile()){  
  4.         return null;  
  5.     }  
  6.       
  7.     StringBuffer content = new StringBuffer();  
  8.       
  9.     try {  
  10.         byte[] temp = new byte[1024];  
  11.         FileInputStream fileInputStream = new FileInputStream(file);  
  12.         while(fileInputStream.read(temp) != -1){  
  13.             content.append(new String(temp));  
  14.             temp = new byte[1024];  
  15.         }  
  16.           
  17.         fileInputStream.close();  
  18.     } catch (FileNotFoundException e) {  
  19.         e.printStackTrace();  
  20.     } catch (IOException e) {  
  21.         e.printStackTrace();  
  22.     }  
  23.       
  24.     return content.toString();  
  25. }  

 (2)以字符爲單位讀取文件,常用於讀文本,數字等類型的文件,支持讀取中文

[java] view plaincopy
  1. public static String readFileByChars(String filePath){  
  2.     File file = new File(filePath);  
  3.     if(!file.exists() || !file.isFile()){  
  4.         return null;  
  5.     }  
  6.       
  7.     StringBuffer content = new StringBuffer();  
  8.     try {  
  9.         char[] temp = new char[1024];  
  10.         FileInputStream fileInputStream = new FileInputStream(file);  
  11.         InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "GBK");  
  12.         while(inputStreamReader.read(temp) != -1){  
  13.             content.append(new String(temp));  
  14.             temp = new char[1024];  
  15.         }  
  16.           
  17.         fileInputStream.close();  
  18.         inputStreamReader.close();  
  19.     } catch (FileNotFoundException e) {  
  20.         e.printStackTrace();  
  21.     } catch (IOException e) {  
  22.         e.printStackTrace();  
  23.     }  
  24.       
  25.     return content.toString();  
  26. }  

(3)以行爲單位讀取文件,常用於讀面向行的格式化文件

[java] view plaincopy
  1. public static List<String> readFileByLines(String filePath){  
  2.     File file = new File(filePath);  
  3.     if(!file.exists() || !file.isFile()){  
  4.         return null;  
  5.     }  
  6.       
  7.     List<String> content = new ArrayList<String>();  
  8.     try {  
  9.         FileInputStream fileInputStream = new FileInputStream(file);  
  10.         InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "GBK");  
  11.         BufferedReader reader = new BufferedReader(inputStreamReader);  
  12.         String lineContent = "";  
  13.         while ((lineContent = reader.readLine()) != null) {  
  14.             content.add(lineContent);  
  15.             System.out.println(lineContent);  
  16.         }  
  17.           
  18.         fileInputStream.close();  
  19.         inputStreamReader.close();  
  20.         reader.close();  
  21.     } catch (FileNotFoundException e) {  
  22.         e.printStackTrace();  
  23.     } catch (IOException e) {  
  24.         e.printStackTrace();  
  25.     }  
  26.       
  27.     return content;  
  28. }  

6、寫文件

字符串寫入文件的幾個類中,FileWriter效率最高,BufferedOutputStream次之,FileOutputStream最差。

(1)通過FileOutputStream寫入文件

[java] view plaincopy
  1. public static void writeFileByFileOutputStream(String filePath, String content) throws IOException{  
  2.     File file = new File(filePath);  
  3.     synchronized (file) {  
  4.         FileOutputStream fos = new FileOutputStream(filePath);  
  5.         fos.write(content.getBytes("GBK"));  
  6.         fos.close();  
  7.     }  
  8. }  

(2)通過BufferedOutputStream寫入文件

[java] view plaincopy
  1. public static void writeFileByBufferedOutputStream(String filePath, String content) throws IOException{  
  2.     File file = new File(filePath);  
  3.     synchronized (file) {  
  4.         BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(filePath));  
  5.         fos.write(content.getBytes("GBK"));  
  6.         fos.flush();  
  7.         fos.close();  
  8.     }  
  9. }  

(3)通過FileWriter將字符串寫入文件

[java] view plaincopy
  1. public static void writeFileByFileWriter(String filePath, String content) throws IOException{  
  2.         File file = new File(filePath);  
  3.         synchronized (file) {  
  4.             FileWriter fw = new FileWriter(filePath);  
  5.             fw.write(content);  
  6.             fw.close();  
  7.         }  
  8.     }  


 本文轉載自:http://blog.csdn.net/brushli/article/details/12356695

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章