sourcecode

Android;새 파일을 만들지 않고 파일이 있는지 확인

codebag 2023. 8. 21. 21:15
반응형

Android;새 파일을 만들지 않고 파일이 있는지 확인

내 패키지 폴더에 파일이 있는지 확인하고 싶지만 새 파일을 만들고 싶지 않습니다.

File file = new File(filePath);
if(file.exists()) 
     return true;

이 코드는 새 파일을 만들지 않고 확인합니까?

코드 덩어리는 새 코드를 만들지 않고 이미 존재하는지 여부만 확인합니다.

File file = new File(filePath);
if(file.exists())      
//Do something
else
// Do something else.

이 코드를 사용하면 새 파일을 만드는 것이 아니라 해당 파일에 대한 개체 참조를 만들고 해당 파일의 존재 여부를 테스트하는 것입니다.

File file = new File(filePath);
if(file.exists()) 
    //do something

제게 효과가 있었습니다.

File file = new File(getApplicationContext().getFilesDir(),"whatever.txt");
    if(file.exists()){
       //Do something
    }
    else{
       //Nothing
     }

"in you package folder"라고 하면 로컬 앱 파일을 말하는 건가요?이 경우 Context.fileList() 메서드를 사용하여 목록을 가져올 수 있습니다.반복해서 파일을 찾으십시오.즉, Context.openFileOutput()으로 원본 파일을 저장했다고 가정합니다.

샘플 코드(활동):

public void onCreate(...) {
    super.onCreate(...);
    String[] files = fileList();
    for (String file : files) {
        if (file.equals(myFileName)) {
            //file exits
        }
    }
}

methods경로 클래스에는 구문이 있습니다. 즉, 경로 인스턴스에서 작동합니다.하지만 결국에는 액세스해야 합니다.file특정 경로가 존재하는지 확인하는 시스템

 File file = new File("FileName");
 if(file.exists()){
 System.out.println("file is already there");
 }else{
 System.out.println("Not find file ");
 }
public boolean FileExists(String fname) {
        File file = getBaseContext().getFileStreamPath(fname);
        return file.exists();
}
if(new File("/sdcard/your_filename.txt").exists())){
              // Your code goes here...
}

Kotlin 확장 특성

파일 개체를 만들 때는 인터페이스일 뿐 파일이 생성되지 않습니다.

파일 작업을 더 쉽게 하기 위해 기존의.toFileURI에 대한 함수

파일 및/또는 URI에 확장 속성을 추가하여 사용을 더욱 단순화할 수도 있습니다.

val File?.exists get() = this?.exists() ?: false
val Uri?.exists get() = File(this.toString).exists()

그럼 그냥 사용하세요.uri.exists또는file.exists확인해야 합니다.

언급URL : https://stackoverflow.com/questions/16237950/android-check-if-file-exists-without-creating-a-new-one

반응형