Java中新建一個文件、目錄及路徑操作實例
前言
學習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ù)的絕對值和相除求百分比,Java在java.math包中提供的API類BigDecimal,用來對超過16位有效位的數(shù)進行精確的運算,需要的朋友可以參考下2023-07-07
Maven編譯錯誤:程序包com.sun.*包不存在的三種解決方案
J2SE中的類大致可以劃分為以下的各個包:java.*,javax.*,org.*,sun.*,本文文章主要介紹了maven編譯錯誤:程序包com.sun.xml.internal.ws.spi不存在的解決方案,感興趣的可以了解一下2024-02-02
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獲取Bean本身id的相關配置與實現(xiàn)技巧,需要的朋友可以參考下2019-11-11

