最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

前言

學習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 = "實習日志.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);
		}
	}
}

測試結果:編譯通過、解釋運行正常,創(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 = "實習日志.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("文件創(chuàng)建異常!");
		}
	}
	
	static void createAFileInCertainPath(File file){
		try{
			file.createNewFile();
		}catch(Exception e){
			System.out.println(e);
		}
	}
}

測試結果如下

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 = "實習日志.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("文件創(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)建文件失??!");
			}
		}
	}
}

測試結果:編譯通過、解釋運行,創(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 = "實習日志.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("文件創(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-總結

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

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

(1)導包,本來想使用Nullable,但似乎沒有相關包

(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)建失敗");
        }
    }
}

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

相關文章

  • 關于BigDecimal類型數(shù)據(jù)的絕對值和相除求百分比

    關于BigDecimal類型數(shù)據(jù)的絕對值和相除求百分比

    這篇文章主要介紹了關于BigDecimal類型數(shù)據(jù)的絕對值和相除求百分比,Java在java.math包中提供的API類BigDecimal,用來對超過16位有效位的數(shù)進行精確的運算,需要的朋友可以參考下
    2023-07-07
  • Maven編譯錯誤:程序包com.sun.*包不存在的三種解決方案

    Maven編譯錯誤:程序包com.sun.*包不存在的三種解決方案

    J2SE中的類大致可以劃分為以下的各個包:java.*,javax.*,org.*,sun.*,本文文章主要介紹了maven編譯錯誤:程序包com.sun.xml.internal.ws.spi不存在的解決方案,感興趣的可以了解一下
    2024-02-02
  • Spring Cloud如何切換Ribbon負載均衡模式

    Spring Cloud如何切換Ribbon負載均衡模式

    這篇文章主要介紹了Spring Cloud如何切換Ribbon負載均衡模式,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-12-12
  • SpringBoot中的事務處理問題

    SpringBoot中的事務處理問題

    這篇文章主要介紹了SpringBoot中的事務處理問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • 淺談SpringMVC的執(zhí)行流程

    淺談SpringMVC的執(zhí)行流程

    下面小編就為大家?guī)硪黄獪\談SpringMVC的執(zhí)行流程。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • xml與Java對象的轉(zhuǎn)換詳解

    xml與Java對象的轉(zhuǎn)換詳解

    這篇文章主要介紹了xml與Java對象的轉(zhuǎn)換詳解的相關資料,需要的朋友可以參考下
    2017-04-04
  • Spring Boot Logging Level設置為off時的Bug

    Spring Boot Logging Level設置為off時的Bug

    這篇文章主要介紹了Spring Boot Logging Level設置為off時的Bug,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • Spring實戰(zhàn)之獲得Bean本身的id操作示例

    Spring實戰(zhàn)之獲得Bean本身的id操作示例

    這篇文章主要介紹了Spring實戰(zhàn)之獲得Bean本身的id操作,結合實例形式分析了spring獲取Bean本身id的相關配置與實現(xiàn)技巧,需要的朋友可以參考下
    2019-11-11
  • Feign之Multipartfile文件傳輸填坑

    Feign之Multipartfile文件傳輸填坑

    這篇文章主要介紹了Feign之Multipartfile文件傳輸埋坑及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • 淺談Spring 的Controller 是單例or多例

    淺談Spring 的Controller 是單例or多例

    這篇文章主要介紹了淺談Spring 的Controller 是單例or多例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-08-08

最新評論

阿尔山市| 玉田县| 桓仁| 周至县| 广东省| 华安县| 民丰县| 紫金县| 福泉市| 沐川县| 新蔡县| 汕头市| 同心县| 毕节市| 沂水县| 东至县| 江都市| 油尖旺区| 通城县| 大港区| 英吉沙县| 历史| 阳泉市| 洛川县| 棋牌| 布尔津县| 晋中市| 阳谷县| 灵台县| 罗田县| 郓城县| 郁南县| 洛隆县| 青冈县| 孟州市| 龙游县| 金湖县| 永德县| 承德县| 平利县| 长宁区|