JAVA 文件操作

来源:互联网 发布:spark源码下载 编辑:程序博客网 时间:2024/04/30 14:49

平常经常使用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.         } 
0 0