6-Java中新建一个文件、目录、路径

前言

学习Java中如何新建文件、目录、路径

1-文件、目录、路径

文件fileName,就如我们在电脑中看到的.txt、.java、.doc等
目录dir,可以理解成文件夹,里面可以包含多个文件夹或文件
路径directoryPath,有绝对路径和相对路径,这个不需要多说,但需要注意的是,如果想把win11电脑上已经存在的路径用来创建File实例,需要注意加转义符

2-在当前路径下创建一个文件

Main.java

class MainActivity{
	public static void main(String[] args){
		System.out.println("Main thread is running...");
		FileTest1.createAFileInCurrentPath("DefaultJavaFile1.java", null);
		
	}
}

FileTest1.java

import java.io.*;



class FileTest1{
	// the path of the fileName
	String directoryPath;
	// the name of the fileName
	String fileName;
	
	FileTest1(){
		
	}
	
	FileTest1(String directoryPath, String fileName){
		this.directoryPath = directoryPath;
		this.fileName = fileName;
	}
	
	static void createAFileInCurrentPath(String fileName, String directoryPath){
		if (null == directoryPath){
			directoryPath = ".";
		}
		File tempFile = new File(directoryPath, fileName);
		try{
			tempFile.createNewFile();
		}catch (IOException e){
			System.out.println("文件创建异常!");
		}
	}
	
}

上面的代码中,如果createAFileInCurrentPath方法传入的directoryPath为"."也是可以的,就表示当前路径

3-在当前路径下创建一个文件夹(目录)

3.1 测试1-路径已经存在

Main.java

import java.io.*;


class MainActivity{
	public static void main(String[] args){
		System.out.println("Main thread is running...");
		
		String existedPath1 = "D:\\JavaAndroidLearnningDiary\\《Java 2实用教程》(5版 耿祥义、张跃平)\\Chapter10\\Leson1";
		String testFileName1 = "实习日志.java";
		
		//create a file in current path
		FileTest1.createAFileInCurrentPath("DefaultJavaFile1.java", ".");
		
		//create a file in certain path
		File testFile1 = new File(existedPath1, testFileName1);
		FileTest1.createAFileInCertainPath(testFile1);
		
	}
}

FileTest1.java

import java.io.*;



class FileTest1{
	// the path of the fileName
	String directoryPath;
	// the name of the fileName
	String fileName;
	
	FileTest1(){
		
	}
	
	FileTest1(String directoryPath, String fileName){
		this.directoryPath = directoryPath;
		this.fileName = fileName;
	}
	
	static void createAFileInCurrentPath(String fileName, String directoryPath){
		if (null == directoryPath){
			directoryPath = ".";
		}
		File tempFile = new File(directoryPath, fileName);
		try{
			tempFile.createNewFile();
		}catch (IOException e){
			System.out.println("文件创建异常!");
		}
	}
	
	static void createAFileInCertainPath(File file){
		try{
			file.createNewFile();
		}catch(Exception e){
			System.out.println(e);
		}
	}
	
}

测试结果:编译通过、解释运行正常,创建了新文件

3.2 测试2-路径不存在

Main.java

import java.io.*;


class MainActivity{
	public static void main(String[] args){
		System.out.println("Main thread is running...");
		
		String existedPath1 = "D:\\JavaAndroidLearnningDiary\\《Java 2实用教程》(5版 耿祥义、张跃平)\\Chapter10\\Leson1\\Lesson1~4_Review1";
		String testFileName1 = "实习日志.java";
		
		String unExistedPath1 = "D:\\JavaAndroidLearnningDiary\\《Java 2实用教程》(5版 耿祥义、张跃平)\\Chapter10\\Leson1\\Lesson1~4_Review1\\testDir1";
		String testFileName2 = "学习笔记.java";
		
		//create a file in current path
		FileTest1.createAFileInCurrentPath("DefaultJavaFile1.java", ".");
		
		//create a file in certain and existed path
		File testFile1 = new File(existedPath1, testFileName1);
		FileTest1.createAFileInCertainPath(testFile1);
		//create a file in certain but not existed path
		File testFile2 = new File(unExistedPath1, testFileName2);
		FileTest1.createAFileInCertainPath(testFile2);
	}
}

FileTest1.java

import java.io.*;



class FileTest1{
	// the path of the fileName
	String directoryPath;
	// the name of the fileName
	String fileName;
	
	FileTest1(){
		
	}
	
	FileTest1(String directoryPath, String fileName){
		this.directoryPath = directoryPath;
		this.fileName = fileName;
	}
	
	static void createAFileInCurrentPath(String fileName, String directoryPath){
		if (null == directoryPath){
			directoryPath = ".";
		}
		File tempFile = new File(directoryPath, fileName);
		try{
			tempFile.createNewFile();
		}catch (IOException e){
			System.out.println("文件创建异常!");
		}
	}
	
	static void createAFileInCertainPath(File file){
		try{
			file.createNewFile();
		}catch(Exception e){
			System.out.println(e);
		}
	}
	
}

测试结果如下
在这里插入图片描述

3.2 创建不存在的路径并新建文件

Main.java

import java.io.*;


class MainActivity{
	public static void main(String[] args){
		System.out.println("Main thread is running...");
		
		String existedPath1 = "D:\\JavaAndroidLearnningDiary\\《Java 2实用教程》(5版 耿祥义、张跃平)\\Chapter10\\Leson1\\Lesson1~4_Review1";
		String testFileName1 = "实习日志.java";
		
		String unExistedPath1 = "D:\\JavaAndroidLearnningDiary\\《Java 2实用教程》(5版 耿祥义、张跃平)\\Chapter10\\Leson1\\Lesson1~4_Review1\\testDir1";
		String testFileName2 = "学习笔记.java";
		
		//create a file in current path
		FileTest1.createAFileInCurrentPath("DefaultJavaFile1.java", ".");
		
		//create a file in certain and existed path
		File testFile1 = new File(existedPath1);
		FileTest1.createAFileInCertainPath(testFile1);
		
		//create a file in certain but not existed path
		
		FileTest1.createAFileInCertainPath(testFileName2, unExistedPath1);
		
		
	}
}

FileTest1.java

import java.io.*;



class FileTest1{
	// the path of the fileName
	String directoryPath;
	// the name of the fileName
	String fileName;
	
	FileTest1(){
		
	}
	
	FileTest1(String directoryPath, String fileName){
		this.directoryPath = directoryPath;
		this.fileName = fileName;
	}
	
	static void createAFileInCurrentPath(String fileName, String directoryPath){
		if (null == directoryPath){
			directoryPath = ".";
		}
		File tempFile = new File(directoryPath, fileName);
		try{
			tempFile.createNewFile();
		}catch (IOException e){
			System.out.println("文件创建异常!");
		}
	}
	
	static void createAFileInCertainPath(File file){
		try{
			if (file.exists()){
				file.createNewFile();
			}else{
				System.out.println("the path is not existed ! here are the information of the path:");
				System.out.println("Name :"+file.getName());
				System.out.println("AbsoluteFile :"+file.getAbsoluteFile());
				System.out.println("AbsolutePath :"+file.getAbsolutePath());
			}
			
		}catch(Exception e){
			System.out.println(e);
		}
	}
	
	static void createAFileInCertainPath(String fileName, String directoryPath){
		File tempFileName, tempDirectoryPath;
		
		if (null != directoryPath){
			tempDirectoryPath = new File(directoryPath);
			System.out.println("Is tempFileName a directory :"+tempDirectoryPath.isDirectory());
			tempDirectoryPath.mkdirs();
		}
		if (null != fileName){
			tempFileName = new File(directoryPath, fileName);
			System.out.println("Is tempFileName a file :"+tempFileName.isFile());
			try{
				tempFileName.createNewFile();
			}catch(Exception e){
				System.out.println("在未存在的路径下创建文件失败!");
			}
			
		}
		
	}
	
}

测试结果:编译通过、解释运行,创建成功

3.3 删除已存在的文件并新建

Main.java

import java.io.*;


class MainActivity{
	public static void main(String[] args){
		System.out.println("Main thread is running...");
		
		String existedPath1 = "D:\\JavaAndroidLearnningDiary\\《Java 2实用教程》(5版 耿祥义、张跃平)\\Chapter10\\Leson1\\Lesson1~4_Review1";
		String testFileName1 = "实习日志.java";
		
		String unExistedPath1 = "D:\\JavaAndroidLearnningDiary\\《Java 2实用教程》(5版 耿祥义、张跃平)\\Chapter10\\Leson1\\Lesson1~4_Review1\\testDir2";
		String testFileName2 = "学习笔记.java";
		
		//create a file in current path
		
		
		//create a file in certain and existed path
		File testFile1 = new File(existedPath1);
		FileTest1.createAFileInCertainPath(testFile1);
		
		//create a file in certain but not existed path
		FileTest1.createAFileInCertainPath(testFileName2, unExistedPath1);
		
		//delete a file in current path
		FileTest1.deleteAFileInCurrentPath("DefaultJavaFile1.java");
		
		//delete a file in certain path
		String deleteTestPath1 = "D:\\TestPath1\\TestDir1\\TestDir2\\TestDir3\\TestDir3_1\\测试.txt";
		FileTest1.deleteAFileInCeratainPath("D:\\TestPath1\\TestDir1\\TestDir2\\TestDir3\\TestDir3_1", "测试.txt");
		
		//delete a dir in certain path
		FileTest1.deleteADirInCertainPath("D:\\TestPath1\\TestDir1\\TestDir2\\TestDir3\\TestDir3_2");
		
	}
}

FileTest1.java

import java.io.*;



class FileTest1{
	// the path of the fileName
	String directoryPath;
	// the name of the fileName
	String fileName;
	
	FileTest1(){
		
	}
	
	FileTest1(String directoryPath, String fileName){
		this.directoryPath = directoryPath;
		this.fileName = fileName;
	}
	
	static void createAFileInCurrentPath(String fileName, String directoryPath){
		if (null == directoryPath){
			directoryPath = ".";
		}
		File tempFile = new File(directoryPath, fileName);
		try{
			tempFile.createNewFile();
		}catch (IOException e){
			System.out.println("文件创建异常!");
		}
	}
	
	static void createAFileInCertainPath(File file){
		try{
			if (!file.exists()){
				file.createNewFile();
			}else{
				
			}
			
		}catch(Exception e){
			System.out.println(e);
		}
	}
	
	static void createAFileInCertainPath(String fileName, String directoryPath){
		File tempFileName, tempDirectoryPath;
		
		if (null != directoryPath){
			tempDirectoryPath = new File(directoryPath);
			System.out.println("Is tempFileName a directory :"+tempDirectoryPath.isDirectory());
			tempDirectoryPath.mkdirs();
		}
		if (null != fileName){
			tempFileName = new File(directoryPath, fileName);
			System.out.println("Is tempFileName a file :"+tempFileName.isFile());
			try{
				tempFileName.createNewFile();
			}catch(Exception e){
				System.out.println("在未存在的路径下创建文件失败!");
			}
			
		}
		
	}
	
	static void deleteAFileInCurrentPath(String fileName){
		System.out.println("Function deleteAFileInCurrentPath is running---------");
		File tempFile = new File(fileName);
		try{
			if (tempFile.exists()){
				System.out.println(tempFile.getName()+" 文件存在");
				tempFile.delete();
			}else{
				System.out.println("文件不存在");
			}
		}catch(Exception e){
			System.out.println("删除文件失败!");
		}
		System.out.println("Function deleteAFileInCurrentPath is finished---------");
	}
	
	static void deleteAFileInCeratainPath(String directory, String fileName){
		System.out.println("Function deleteAFileInCeratainPath is running---------");
		File tempFile = new File(directory, fileName);
		try{
			if (tempFile.exists()){
				System.out.println(tempFile.getName()+" 文件存在");
				tempFile.delete();
			}else{
				System.out.println("文件不存在");
			}
		}catch(Exception e){
			System.out.println("删除文件失败!");
		}
		System.out.println("Function deleteAFileInCeratainPath is finished---------");
	}
	
	static void deleteADirInCertainPath(String directory){
		System.out.println("Function deleteADirInCertainPath is running---------");
		File tempFile = new File(directory);
		try{
			if (tempFile.exists()){
				System.out.println(tempFile.getName()+" 文件存在");
				tempFile.delete();
			}else{
				System.out.println("文件不存在");
			}
		}catch(Exception e){
			System.out.println("删除文件失败!");
		}
		System.out.println("Function deleteADirInCertainPath is finished---------");
	}
	
}

4-总结

1.简要学习了Java中如何创建文件、目录
2.在调试过程中遇到了一些问题
(1)导包,本来想使用Nullable,但似乎没有相关包
(2)直接在java文件的目录下打开的Windows power Shell窗口中运行时,显示无法加载主类MainActivity,而打开的cmd窗口却可以运行
(3)如果实例化的File对象中的路径是磁盘里不存在的,则isFile、isDirectory全返回false

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值