欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java中新建一個文件、目錄及路徑操作實例

 更新時間:2023年12月07日 11:27:38   作者:m0_45695898  
這篇文章主要給大家介紹了關(guān)于Java中新建一個文件、目錄及路徑操作的相關(guān)資料,新建文件、目錄及路徑是我們?nèi)粘i_發(fā)中經(jīng)常會遇到的一個需求,本文通過示例代碼介紹的非常詳細,需要的朋友可以參考下

前言

學(xué)習(xí)Java中如何新建文件、目錄、路徑

1-文件、目錄、路徑

文件fileName,就如我們在電腦中看到的.txt、.java、.doc等
目錄dir,可以理解成文件夾,里面可以包含多個文件夾或文件
路徑directoryPath,有絕對路徑和相對路徑,這個不需要多說,但需要注意的是,如果想把win11電腦上已經(jīng)存在的路徑用來創(chuàng)建File實例,需要注意加轉(zhuǎn)義符

2-在當前路徑下創(chuàng)建一個文件

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("文件創(chuàng)建異常!");
		}
	}
}

上面的代碼中,如果createAFileInCurrentPath方法傳入的directoryPath為"."也是可以的,就表示當前路徑

3-在當前路徑下創(chuàng)建一個文件夾(目錄)

3.1 測試1-路徑已經(jīng)存在

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 = "實習(xí)日志.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("文件創(chuàng)建異常!");
		}
	}
	static void createAFileInCertainPath(File file){
		try{
			file.createNewFile();
		}catch(Exception e){
			System.out.println(e);
		}
	}
}

測試結(jié)果:編譯通過、解釋運行正常,創(chuàng)建了新文件

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 = "實習(xí)日志.java";
		
		String unExistedPath1 = "D:\\JavaAndroidLearnningDiary\\《Java 2實用教程》(5版 耿祥義、張躍平)\\Chapter10\\Leson1\\Lesson1~4_Review1\\testDir1";
		String testFileName2 = "學(xué)習(xí)筆記.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("文件創(chuàng)建異常!");
		}
	}
	
	static void createAFileInCertainPath(File file){
		try{
			file.createNewFile();
		}catch(Exception e){
			System.out.println(e);
		}
	}
}

測試結(jié)果如下

3.2 創(chuàng)建不存在的路徑并新建文件

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 = "實習(xí)日志.java";
		
		String unExistedPath1 = "D:\\JavaAndroidLearnningDiary\\《Java 2實用教程》(5版 耿祥義、張躍平)\\Chapter10\\Leson1\\Lesson1~4_Review1\\testDir1";
		String testFileName2 = "學(xué)習(xí)筆記.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("文件創(chuàng)建異常!");
		}
	}
	
	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("在未存在的路徑下創(chuàng)建文件失敗!");
			}
		}
	}
}

測試結(jié)果:編譯通過、解釋運行,創(chuàng)建成功

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 = "實習(xí)日志.java";
		
		String unExistedPath1 = "D:\\JavaAndroidLearnningDiary\\《Java 2實用教程》(5版 耿祥義、張躍平)\\Chapter10\\Leson1\\Lesson1~4_Review1\\testDir2";
		String testFileName2 = "學(xué)習(xí)筆記.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("文件創(chuàng)建異常!");
		}
	}
	
	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("在未存在的路徑下創(chuàng)建文件失敗!");
			}
			
		}
		
	}
	
	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-總結(jié)

1.簡要學(xué)習(xí)了Java中如何創(chuàng)建文件、目錄

2.在調(diào)試過程中遇到了一些問題

(1)導(dǎo)包,本來想使用Nullable,但似乎沒有相關(guān)包

(2)直接在java文件的目錄下打開的Windows power Shell窗口中運行時,顯示無法加載主類MainActivity,而打開的cmd窗口卻可以運行

(3)如果實例化的File對象中的路徑是磁盤里不存在的,則isFile、isDirectory全返回false

附:java根據(jù)路徑創(chuàng)建文件夾

import java.io.File;

public class CreateFolderExample {
    public static void main(String[] args) {
        String folderPath = "path/to/folder";
        File folder = new File(folderPath);
        
        if (folder.exists()) {
            System.out.println("文件夾已存在");
            return;
        }
        
        if (folder.mkdir()) {
            System.out.println("文件夾創(chuàng)建成功");
        } else {
            System.out.println("文件夾創(chuàng)建失敗");
        }
    }
}

到此這篇關(guān)于Java中新建一個文件、目錄及路徑的文章就介紹到這了,更多相關(guān)Java新建文件目錄路徑內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringBoot深入分析講解監(jiān)聽器模式下

    SpringBoot深入分析講解監(jiān)聽器模式下

    監(jiān)聽器模式,大家應(yīng)該并不陌生,主要的組成要素包括了事件、監(jiān)聽器以及廣播器;當事件發(fā)生時,廣播器負責將事件傳遞給所有已知的監(jiān)聽器,而監(jiān)聽器會對自己感興趣的事件進行處理
    2022-07-07
  • 記一次用IDEA打開java項目后不能運行的解決方法

    記一次用IDEA打開java項目后不能運行的解決方法

    這篇文章主要介紹了記一次用IDEA打開java項目后不能運行的解決方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • SpringBoot集成JWT生成token及校驗方法過程解析

    SpringBoot集成JWT生成token及校驗方法過程解析

    這篇文章主要介紹了SpringBoot集成JWT生成token及校驗方法過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-04-04
  • IDEA 配置 JRebel 熱部署的方法(推薦)

    IDEA 配置 JRebel 熱部署的方法(推薦)

    這篇文章主要介紹了IDEA 配置 JRebel 熱部署的方法,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-01-01
  • Java線程實現(xiàn)時間動態(tài)顯示

    Java線程實現(xiàn)時間動態(tài)顯示

    這篇文章主要為大家詳細介紹了Java線程實現(xiàn)時間動態(tài)顯示,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-04-04
  • java內(nèi)存模型jvm虛擬機簡要分析

    java內(nèi)存模型jvm虛擬機簡要分析

    Java 內(nèi)存模型的主要目的是定義程序中各種變量的訪問規(guī)則, 關(guān)注在虛擬機中把變量值存儲到內(nèi)存和從內(nèi)存中取出變量值這樣的底層細節(jié)
    2021-09-09
  • SpringBoot+Spring Security基于內(nèi)存用戶認證的實現(xiàn)

    SpringBoot+Spring Security基于內(nèi)存用戶認證的實現(xiàn)

    本文介紹了SpringBoot+Spring Security基于內(nèi)存用戶認證的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-11-11
  • Java爬蟲范例之使用Htmlunit爬取學(xué)校教務(wù)網(wǎng)課程表信息

    Java爬蟲范例之使用Htmlunit爬取學(xué)校教務(wù)網(wǎng)課程表信息

    htmlunit 是一款開源的java 頁面分析工具,讀取頁面后,可以有效的使用htmlunit分析頁面上的內(nèi)容。項目可以模擬瀏覽器運行,被譽為java瀏覽器的開源實現(xiàn)。今天我們用這款分析工具來爬取學(xué)校教務(wù)網(wǎng)課程表信息
    2021-11-11
  • SpringBoot如何集成PageHelper分頁功能

    SpringBoot如何集成PageHelper分頁功能

    這篇文章主要介紹了SpringBoot如何集成PageHelper分頁功能,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-03-03
  • Java中的引用和動態(tài)代理的實現(xiàn)詳解

    Java中的引用和動態(tài)代理的實現(xiàn)詳解

    這篇文章主要介紹了Java中的引用和動態(tài)代理的實現(xiàn)詳解,涉及Java中的引用類型,JVMGC的可達性分析,代理模式等相關(guān)內(nèi)容,具有一定參考價值,需要的朋友可以了解下。
    2017-11-11

最新評論