當前位置:首頁 » 編程語言 » java讀取java文件是否存在

java讀取java文件是否存在

發布時間: 2023-03-07 01:08:35

java中怎樣根據文件的路徑去判斷該文件夾中是否存在該文件

1.File testFile = new File(testFilePath);
if(!testFile .exists()){
testFile.mkdirs();
System.out.println("測試文件夾不存在");
}

2.File testFile = new File(testFilePath);
if(!testFile .exists()){
testFile.createNewFile();
System.out.println("測試文件不存在");
}
java中File類自帶一個檢測方法exists可以判斷文件或文件夾是否存在,一般與mkdirs方法(該方法相較於mkdir可以創建包括父級路徑,推薦使用該方法)或者createNewFile方法合作使用。
1,如果路徑不存在,就創建該路徑

2,如果文件不存在,就新建該文件

⑵ java怎麼判斷一個文件是否存在

用java.io.File類的public boolean exists()方法就能判斷,如:
import java.io.*;
public class Demo
{
public static void main(String[] args) throws Exception
{
//將p指定為文件的路徑
String p="test.txt";
File f=new File(p);
if(f.isFile())
{
if(f.exists())
{
System.out.println("文件"+p+"存在。");
}
else
{
System.out.println("文件"+p+"不存在。");
}
}
else
{
System.out.println(p+"不是文件。");
}
}
}

⑶ java怎樣判斷一個文件中是否存在內容

packagetest1701;

importjava.io.FileInputStream;
importjava.io.IOException;

publicclassTest10{
publicstaticvoidmain(String[]args){
Stringpath="文件路徑";
=null;
try{
fileInputStream=newFileInputStream(path);
if(fileInputStream.available()==0){
System.out.println("文件為空");
}
}catch(Exceptione){
e.printStackTrace();
}finally{
if(fileInputStream!=null){
try{
fileInputStream.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
}
}
}

⑷ java 根據文件名判斷文件是否存在

File類自帶判斷文件(或者路徑)是否存在的方法。舉個例子:

Stringpath="D:\";
Stringfilename="test.txt";
Filefile=newFile(path+filename);
if(file.exists()){
System.out.println("文件"+filename+"存在");
}else{
System.out.println("文件"+filename+"不存在")
}

⑸ java 如何判斷文件路徑是否存在

exists
public boolean exists()測試此抽象路徑名表示的文件或目錄是否存在。

返回:
當且僅當此抽象路徑名表示的文件或目錄存在時,返回 true;否則返回 false

isFile
public boolean isFile()測試此抽象路徑名表示的文件是否是一個標准文件。如果該文件不是一個目錄,並且滿足其他與系統有關的標准,那麼該文件是標准 文件。由 Java 應用程序創建的所有非目錄文件一定是標准文件。

返回:
當且僅當此抽象路徑名表示的文件存在且 是一個標准文件時,返回 true;否則返回 false

⑹ java 怎麼判斷某路徑下的某個文件是否存在

public class DirectoryList {
public static void main(String[] args){
String fileName="lady gaga、lady gaga - telephone ft. beyonce.mp3";//要判斷的文件或文件夾
try{
File path = new File("D:/KuGou");
String[] myList;//定義一個字元串數組
if(fileName == null && fileName.length() == 0)//不含自變數則顯示所有文件
myList = path.list();
else
myList = path.list(new DirectoryFilter(fileName));
for(int i = 0; i< myList.length;i++)//輸出文件列表
System.out.println(myList[i]);
}catch(Exception e)
{
e.printStackTrace();
}

}
}//DirectoryList ends 實現filename 的過濾器
class DirectoryFilter implements FilenameFilter
{
String myString;
DirectoryFilter(String myString)
{
this.myString = myString;
}
public boolean accept(File dir,String name)
{//FilenameFilter.accept(File dir, String name)
// 測試指定文件是否應該包含在某一文件列表中。
String f= new File(name).getName();
return f.equals(myString);
}
}

熱點內容
java來源 發布:2024-05-22 09:37:44 瀏覽:676
linux查看桌面 發布:2024-05-22 09:16:47 瀏覽:251
伺服器災難如何恢復 發布:2024-05-22 09:16:02 瀏覽:694
設置mysql資料庫字元集 發布:2024-05-22 09:07:25 瀏覽:904
mp4加密視頻破解 發布:2024-05-22 08:55:52 瀏覽:354
c語言是匯編語言 發布:2024-05-22 08:55:06 瀏覽:956
類庫編譯 發布:2024-05-22 08:50:09 瀏覽:678
編譯程序是c加密 發布:2024-05-22 08:48:36 瀏覽:821
河南伺服器cpu散熱片雲空間 發布:2024-05-22 08:40:10 瀏覽:127
小陳訪問 發布:2024-05-22 08:23:18 瀏覽:222