首页 > 开发 > Java > 正文

Java编程中最基础的文件和目录操作方法详解

2024-07-13 09:56:07
字体:
来源:转载
供稿:网友

这篇文章主要介绍了Java编程中最基础的文件和目录操作方法详解,是Java入门学习中的基础知识,需要的朋友可以参考下

文件操作

平常经常使用JAVA对文件进行读写等操作,这里汇总一下常用的文件操作。

1、创建文件

 

 
  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、创建文件夹

 

 
  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、删除文件

 

 
  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;  

4、删除文件夹

递归删除文件夹下面的子文件和文件夹

 

 
  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)以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件

 

 
  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)以字符为单位读取文件,常用于读文本,数字等类型的文件,支持读取中文

 

 
  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)以行为单位读取文件,常用于读面向行的格式化文件

 

 
  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写入文件

 

 
  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写入文件

 

 
  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将字符串写入文件

 

 
  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. }  

目录操作

目录是一个文件可以包含其他文件和目录的列表。你想要在目录中列出可用文件列表,可以通过使用 File 对象创建目录,获得完整详细的能在 File 对象中调用的以及有关目录的方法列表。

创建目录

这里有两个有用的文件方法,能够创建目录:

mkdir( ) 方法创建了一个目录,成功返回 true ,创建失败返回 false。失败情况是指文件对象的路径已经存在了,或者无法创建目录,因为整个路径不存在。

mkdirs( ) 方法创建一个目录和它的上级目录。

以下示例创建 “/ tmp / user / java / bin” 目录:

 

 
  1. import java.io.File; 
  2.  
  3. public class CreateDir { 
  4. public static void main(String args[]) { 
  5. String dirname = "/tmp/user/java/bin"
  6. File d = new File(dirname); 
  7. // Create directory now. 
  8. d.mkdirs(); 

编译并执行以上代码创建 “/ tmp /user/ java / bin”。

提示:Java 自动按 UNIX 和 Windows 约定来处理路径分隔符。如果在 Windows 版本的 Java 中使用正斜杠(/),仍然可以得到正确的路径。

目录列表

如下,你能够用 File 对象提供的 list() 方法来列出目录中所有可用的文件和目录

 

 
  1. import java.io.File; 
  2.  
  3. public class ReadDir { 
  4. public static void main(String[] args) { 
  5.  
  6. File file = null
  7. String[] paths; 
  8.  
  9. try{  
  10. // create new file object 
  11. file = new File("/tmp"); 
  12.  
  13. // array of files and directory 
  14. paths = file.list(); 
  15.  
  16. // for each name in the path array 
  17. for(String path:paths) 
  18. // prints filename and directory name 
  19. System.out.println(path); 
  20. }catch(Exception e){ 
  21. // if any error occurs 
  22. e.printStackTrace(); 

基于你/ tmp目录下可用的目录和文件,将产生以下结果:

 

 
  1. test1.txt 
  2. test2.txt 
  3. ReadDir.java 
  4. ReadDir.class 


注:相关教程知识阅读请移步到JAVA教程频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表