1

我试图为我的 android 手机制作一个 FileManager,我是 Java 的新手......我找到了这个源,我想在我的源代码中实现它,并且我想在按下应用程序中的菜单按钮时控制一切。

import java.io.File;

public class FileUtils {

  public static boolean delete(String path) {
    final File deleteTarget = new File(path);
    return delete(deleteTarget);
  }

  public static boolean delete(File targetFile) {
    boolean deleted = true;
    try {
      deleted = targetFile.delete();
      if(!deleted && ! targetFile.isFile()) {
        throw new Exception(String.format("File could not be deleted. Path: %s", targetFile.getAbsolutePath()));
      }
      deleted = true;
    } catch (Exception e) {
      deleted = false;
    }

    return deleted;
  }

  public static boolean createDirectory(File directory) {
    boolean created = true;
    try {
      created = directory.mkdirs();
      if(!created && ! directory.isDirectory()) {
        throw new Exception(String.format("Directory could not be created. Path: %s", directory.getAbsolutePath()));
      }
      created = true;
    } catch (Exception e) {
      created = false;
    }

    return created;
  }
}
4

0 回答 0