패키지 폴더에 파일이 있는지 확인하고 싶지만 새 폴더를 만들고 싶지 않습니다.
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
}
답변
“패키지 폴더에”라고 말하면 로컬 앱 파일을 의미합니까? 그렇다면 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();
}
답변
코 틀린 확장 속성
File 객체를 만들 때 파일이 생성되지 않으며 인터페이스 일뿐입니다.
파일 작업을보다 쉽게하기 위해 .toFile
Uri에 기존 기능이 있습니다.
File 및 / 또는 Uri에 확장 속성을 추가하여 사용을 더욱 단순화 할 수도 있습니다.
val File?.exists get() = this?.exists() ?: false
val Uri?.exists get() = File(this.toString).exists()
그럼 그냥 사용 uri.exists
또는 file.exists
확인 할 수 있습니다.