Java中新建一個文件、目錄及路徑操作實例
前言
學(xué)習(xí)Java中如何新建文件、目錄、路徑
1-文件、目錄、路徑
| 文件 | fileName,就如我們在電腦中看到的.txt、.java、.doc等 |
|---|---|
| 目錄 | dir,可以理解成文件夾,里面可以包含多個文件夾或文件 |
| 路徑 | directoryPath,有絕對路徑和相對路徑,這個不需要多說,但需要注意的是,如果想把win11電腦上已經(jīng)存在的路徑用來創(chuàng)建File實例,需要注意加轉(zhuǎn)義符 |
2-在當(dāng)前路徑下創(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為"."也是可以的,就表示當(dāng)前路徑
3-在當(dāng)前路徑下創(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集成JWT生成token及校驗方法過程解析
這篇文章主要介紹了SpringBoot集成JWT生成token及校驗方法過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-04-04
SpringBoot+Spring Security基于內(nèi)存用戶認(rèn)證的實現(xiàn)
本文介紹了SpringBoot+Spring Security基于內(nèi)存用戶認(rèn)證的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-11-11
Java爬蟲范例之使用Htmlunit爬取學(xué)校教務(wù)網(wǎng)課程表信息
htmlunit 是一款開源的java 頁面分析工具,讀取頁面后,可以有效的使用htmlunit分析頁面上的內(nèi)容。項目可以模擬瀏覽器運行,被譽(yù)為java瀏覽器的開源實現(xiàn)。今天我們用這款分析工具來爬取學(xué)校教務(wù)網(wǎng)課程表信息2021-11-11

