반응형
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 확장 특성
파일 개체를 만들 때는 인터페이스일 뿐 파일이 생성되지 않습니다.
파일 작업을 더 쉽게 하기 위해 기존의.toFile
URI에 대한 함수
파일 및/또는 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
반응형
'sourcecode' 카테고리의 다른 글
spring.profiles.active에 의해 설정된 여러 Spring의 환경 프로파일이 있는 경우 우선순위는 무엇입니까? (0) | 2023.08.21 |
---|---|
부트스트랩 회전목마 : 자동 슬라이드 제거 (0) | 2023.08.21 |
앱스토어 밖에서 iOS 애플리케이션을 배포할 수 있는 방법이 있습니까? (0) | 2023.08.21 |
탐색 드로어:시작할 때 선택한 항목을 어떻게 설정합니까? (0) | 2023.08.21 |
LINQ Any()와 동등한 PowerShell? (0) | 2023.08.21 |