294 | * The underlying {@link android.provider.DocumentsProvider} only defines a
295 | * forward mapping from parent to child, so the reverse mapping of child to
296 | * parent offered here is purely a convenience method, and it may be
297 | * incorrect if the underlying tree structure changes.
298 | *
299 | * @return parent of the file, or null if it is the top of the file tree
300 | */
301 | @Nullable
302 | public UniFile getParentFile() {
303 | return mParent;
304 | }
305 |
306 | /**
307 | * Indicates if this file represents a directory.
308 | *
309 | * @return {@code true} if this file is a directory, {@code false}
310 | * otherwise.
311 | * @see android.provider.DocumentsContract.Document#MIME_TYPE_DIR
312 | */
313 | public abstract boolean isDirectory();
314 |
315 | /**
316 | * Indicates if this file represents a file.
317 | *
318 | * @return {@code true} if this file is a file, {@code false} otherwise.
319 | * @see android.provider.DocumentsContract.Document#COLUMN_MIME_TYPE
320 | */
321 | public abstract boolean isFile();
322 |
323 | /**
324 | * Returns the time when this file was last modified, measured in
325 | * milliseconds since January 1st, 1970, midnight. Returns -1 if the file
326 | * does not exist, or if the modified time is unknown.
327 | *
328 | * @return the time when this file was last modified, -1L if can't get it
329 | * @see android.provider.DocumentsContract.Document#COLUMN_LAST_MODIFIED
330 | */
331 | public abstract long lastModified();
332 |
333 | /**
334 | * Returns the length of this file in bytes. Returns -1 if the file does not
335 | * exist, or if the length is unknown. The result for a directory is not
336 | * defined.
337 | *
338 | * @return the number of bytes in this file, -1L if can't get it
339 | * @see android.provider.DocumentsContract.Document#COLUMN_SIZE
340 | */
341 | public abstract long length();
342 |
343 | /**
344 | * Indicates whether the current context is allowed to read from this file.
345 | *
346 | * @return {@code true} if this file can be read, {@code false} otherwise.
347 | */
348 | public abstract boolean canRead();
349 |
350 | /**
351 | * Indicates whether the current context is allowed to write to this file.
352 | *
353 | * @return {@code true} if this file can be written, {@code false}
354 | * otherwise.
355 | * @see android.provider.DocumentsContract.Document#COLUMN_FLAGS
356 | * @see android.provider.DocumentsContract.Document#FLAG_SUPPORTS_DELETE
357 | * @see android.provider.DocumentsContract.Document#FLAG_SUPPORTS_WRITE
358 | * @see android.provider.DocumentsContract.Document#FLAG_DIR_SUPPORTS_CREATE
359 | */
360 | public abstract boolean canWrite();
361 |
362 | /**
363 | * Deletes this file.
364 | *
365 | * Note that this method does not throw {@code IOException} on 366 | * failure. Callers must check the return value. 367 | * 368 | * @return {@code true} if this file was deleted, {@code false} otherwise. 369 | * @see android.provider.DocumentsContract#deleteDocument(ContentResolver, 370 | * Uri) 371 | */ 372 | public abstract boolean delete(); 373 | 374 | /** 375 | * Returns a boolean indicating whether this file can be found. 376 | * 377 | * @return {@code true} if this file exists, {@code false} otherwise. 378 | */ 379 | public abstract boolean exists(); 380 | 381 | /** 382 | * Returns an array of files contained in the directory represented by this 383 | * file. 384 | * 385 | * @return an array of files or {@code null}. 386 | * @see android.provider.DocumentsContract#buildChildDocumentsUriUsingTree(Uri, 387 | * String) 388 | */ 389 | @Nullable 390 | public abstract UniFile[] listFiles(); 391 | 392 | /** 393 | * Gets a list of the files in the directory represented by this file. This 394 | * list is then filtered through a FilenameFilter and the names of files 395 | * with matching names are returned as an array of strings. 396 | * 397 | * @param filter the filter to match names against, may be {@code null}. 398 | * @return an array of files or {@code null}. 399 | */ 400 | @Nullable 401 | public abstract UniFile[] listFiles(FilenameFilter filter); 402 | 403 | /** 404 | * Test there is a file with the display name in the directory. 405 | * 406 | * @return the file if found it, or {@code null}. 407 | */ 408 | @Nullable 409 | public abstract UniFile findFile(String displayName); 410 | 411 | /** 412 | * Renames this file to {@code displayName}. 413 | *
414 | * Note that this method does not throw {@code IOException} on 415 | * failure. Callers must check the return value. 416 | *
417 | * Some providers may need to create a new file to reflect the rename, 418 | * potentially with a different MIME type, so {@link #getUri()} and 419 | * {@link #getType()} may change to reflect the rename. 420 | *
421 | * When renaming a directory, children previously enumerated through 422 | * {@link #listFiles()} may no longer be valid. 423 | * 424 | * @param displayName the new display name. 425 | * @return true on success. 426 | * @see android.provider.DocumentsContract#renameDocument(ContentResolver, 427 | * Uri, String) 428 | */ 429 | public abstract boolean renameTo(String displayName); 430 | 431 | /** 432 | * Open a stream on to the content associated with the file, clean it if it exists 433 | * 434 | * @return the {@link OutputStream} 435 | * @throws IOException 436 | */ 437 | @NonNull 438 | public abstract OutputStream openOutputStream() throws IOException; 439 | 440 | /** 441 | * Open a stream on to the content associated with the file 442 | * 443 | * @param append {@code true} for do not clean it if it exists 444 | * @return the {@link OutputStream} 445 | * @throws IOException 446 | */ 447 | @NonNull 448 | public abstract OutputStream openOutputStream(boolean append) throws IOException; 449 | 450 | /** 451 | * Open a stream on to the content associated with the file 452 | * 453 | * @return the {@link InputStream} 454 | * @throws IOException 455 | */ 456 | @NonNull 457 | public abstract InputStream openInputStream() throws IOException; 458 | 459 | /** 460 | * Get a random access stuff of the UniFile 461 | * 462 | * @param mode "r" or "rw" 463 | * @return the random access stuff 464 | * @throws IOException 465 | */ 466 | @NonNull 467 | public abstract UniRandomAccessFile createRandomAccessFile(String mode) throws IOException; 468 | } 469 | --------------------------------------------------------------------------------