java.io.File.delete()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(6.5k)|赞(0)|评价(0)|浏览(243)

本文整理了Java中java.io.File.delete()方法的一些代码示例,展示了File.delete()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。File.delete()方法的具体详情如下:
包路径:java.io.File
类名称:File
方法名:delete

File.delete介绍

[英]Deletes this file. Directories must be empty before they will be deleted.

Note that this method does not throw IOException on failure. Callers must check the return value.
[中]删除此文件。目录必须为空才能删除。
请注意,此方法不会在失败时引发IOException。调用者必须检查返回值。

代码示例

代码示例来源:origin: square/okhttp

@Override public void delete(File file) throws IOException {
 // If delete() fails, make sure it's because the file didn't exist!
 if (!file.delete() && file.exists()) {
  throw new IOException("failed to delete " + file);
 }
}

代码示例来源:origin: hankcs/HanLP

/**
 * 删除本地文件
 * @param path
 * @return
 */
public static boolean deleteFile(String path)
{
  return new File(path).delete();
}

代码示例来源:origin: Tencent/tinker

public static final boolean deleteFile(String filePath) {
  if (filePath == null) {
    return true;
  }
  File file = new File(filePath);
  if (file.exists()) {
    return file.delete();
  }
  return true;
}

代码示例来源:origin: voldemort/voldemort

private File getVersionDirectory() {
  File versionDir = new File(this.directory, VERSION_DIRECTORY);
  if(!versionDir.exists() || !versionDir.isDirectory()) {
    versionDir.delete();
    versionDir.mkdirs();
  }
  return versionDir;
}

代码示例来源:origin: libgdx/libgdx

static private boolean deleteDirectory (File file) {
  if (file.exists()) {
    File[] files = file.listFiles();
    if (files != null) {
      for (int i = 0, n = files.length; i < n; i++) {
        if (files[i].isDirectory())
          deleteDirectory(files[i]);
        else
          files[i].delete();
      }
    }
  }
  return file.delete();
}

代码示例来源:origin: stackoverflow.com

protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
  switch (requestCode) {
    case GlobalConstants.IMAGE_CAPTURE:
      Uri u;
      if (hasImageCaptureBug()) {
        File fi = new File("/sdcard/tmp");
        try {
          u = Uri.parse(android.provider.MediaStore.Images.Media.insertImage(getContentResolver(), fi.getAbsolutePath(), null, null));
          if (!fi.delete()) {
            Log.i("logMarker", "Failed to delete " + fi);
          }
        } catch (FileNotFoundException e) {
          e.printStackTrace();
        }
      } else {
       u = intent.getData();
     }
 }

代码示例来源:origin: gocd/gocd

private void deleteLockFile() {
  if (touchLoop == null) {
    //I haven't got the lock
    return;
  }
  if (!lockFile.exists()) {
    LOG.warn("No lock file found at {}", lockFile.getAbsolutePath());
    return;
  }
  if (!lockFile.delete()) {
    LOG.error("Unable to delete lock file at {}", lockFile.getAbsolutePath());
  }
}

代码示例来源:origin: jenkinsci/jenkins

@Override
    public Void invoke(File f, VirtualChannel channel) throws IOException {
      // JENKINS-16846: if f.getName() is the same as one of the files/directories in f,
      // then the rename op will fail
      File tmp = new File(f.getAbsolutePath()+".__rename");
      if (!f.renameTo(tmp))
        throw new IOException("Failed to rename "+f+" to "+tmp);
      File t = new File(target.getRemote());
      for(File child : reading(tmp).listFiles()) {
        File target = new File(t, child.getName());
        if(!stating(child).renameTo(creating(target)))
          throw new IOException("Failed to rename "+child+" to "+target);
      }
      deleting(tmp).delete();
      return null;
    }
}

代码示例来源:origin: androidannotations/androidannotations

private void deleteDirectoryRecursively(File directory) {
  File[] childs = directory.listFiles();
  if (childs != null) {
    for (File file : childs) {
      if (file.isDirectory()) {
        deleteDirectoryRecursively(file);
      } else {
        file.delete();
      }
    }
  }
  directory.delete();
}

代码示例来源:origin: stackoverflow.com

File dir = new File(Environment.getExternalStorageDirectory()+"Dir_name_here"); 
if (dir.isDirectory()) 
{
  String[] children = dir.list();
  for (int i = 0; i < children.length; i++)
  {
    new File(dir, children[i]).delete();
  }
}

代码示例来源:origin: commons-io/commons-io

void openOutputStream_noParent(final boolean createFile) throws Exception {
  final File file = new File("test.txt");
  assertNull(file.getParentFile());
  try {
    if (createFile) {
      TestUtils.createLineBasedFile(file, new String[]{"Hello"});
    }
    try (FileOutputStream out = FileUtils.openOutputStream(file)) {
      out.write(0);
    }
    assertTrue(file.exists());
  } finally {
    if (!file.delete()) {
      file.deleteOnExit();
    }
  }
}

代码示例来源:origin: stackoverflow.com

public static File createTempDirectory()
  throws IOException
{
  final File temp;

  temp = File.createTempFile("temp", Long.toString(System.nanoTime()));

  if(!(temp.delete()))
  {
    throw new IOException("Could not delete temp file: " + temp.getAbsolutePath());
  }

  if(!(temp.mkdir()))
  {
    throw new IOException("Could not create temp directory: " + temp.getAbsolutePath());
  }

  return (temp);
}

代码示例来源:origin: skylot/jadx

private Object test(Object obj) {
    File file = new File("r");
    FileOutputStream output = null;
    try {
      output = new FileOutputStream(file);
      if (obj.equals("a")) {
        return new Object();
      } else {
        return null;
      }
    } catch (IOException e) {
      System.out.println("Exception");
      return null;
    } finally {
      if (output != null) {
        try {
          output.close();
        } catch (IOException e) {
          // Ignored
        }
      }
      file.delete();
    }
  }
}

代码示例来源:origin: apache/flink

protected static void deleteRecursively(File f) throws IOException {
  if (f.isDirectory()) {
    FileUtils.deleteDirectory(f);
  } else if (!f.delete()) {
    System.err.println("Failed to delete file " + f.getAbsolutePath());
  }
}

代码示例来源:origin: facebook/stetho

public void cleanupFiles() {
 for (File file : mContext.getFilesDir().listFiles()) {
  if (file.getName().startsWith(FILENAME_PREFIX)) {
   if (!file.delete()) {
    LogRedirector.w(TAG, "Failed to delete " + file.getAbsolutePath());
   }
  }
 }
 LogRedirector.i(TAG, "Cleaned up temporary network files.");
}

代码示例来源:origin: facebook/stetho

private static void truncateAndDeleteFile(File file) throws IOException {
 FileOutputStream out = new FileOutputStream(file);
 out.close();
 if (!file.delete()) {
  throw new IOException("Failed to delete " + file);
 }
}

代码示例来源:origin: apache/zookeeper

@Test
public void testValidateFileInputDirectory() throws Exception {
  File file = File.createTempFile("test", ".junit", testData);
  file.deleteOnExit();
  // delete file, as we need directory not file
  file.delete();
  file.mkdir();
  String absolutePath = file.getAbsolutePath();
  String error = ZKUtil.validateFileInput(absolutePath);
  assertNotNull(error);
  String expectedMessage = "'" + absolutePath + "' is a direcory. it must be a file.";
  assertEquals(expectedMessage, error);
}

代码示例来源:origin: voldemort/voldemort

private File getTempDirectory() {
  File tempDir = new File(this.directory, TEMP_DIRECTORY);
  if(!tempDir.exists() || !tempDir.isDirectory()) {
    tempDir.delete();
    tempDir.mkdirs();
  }
  return tempDir;
}

代码示例来源:origin: libgdx/libgdx

static private boolean deleteDirectory (File file) {
  if (file.exists()) {
    File[] files = file.listFiles();
    if (files != null) {
      for (int i = 0, n = files.length; i < n; i++) {
        if (files[i].isDirectory())
          deleteDirectory(files[i]);
        else
          files[i].delete();
      }
    }
  }
  return file.delete();
}

代码示例来源:origin: xuxueli/xxl-job

public static void deleteFile(String fileName) {
  // file
  File file = new File(fileName);
  if (file.exists()) {
    file.delete();
  }
}

相关文章