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

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

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

File.exists介绍

[英]Returns a boolean indicating whether this file can be found on the underlying file system.
[中]返回一个布尔值,指示是否可以在基础文件系统上找到此文件。

代码示例

canonical example by Tabnine

public long getDirectorySize(File file) {
 if (!file.exists()) {
  return 0;
 }
 if (file.isFile()) {
  return file.length();
 }
 File[] files;
 if (!file.isDirectory() || (files = file.listFiles()) == null) {
  return 0;
 }
 return Arrays.stream(files).mapToLong(f -> getDirectorySize(f)).sum();
}

代码示例来源:origin: iluwatar/java-design-patterns

/**
 * @return True, if the file given exists, false otherwise.
 */
public boolean fileExists() {
 return new File(this.fileName).exists();
}

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

File f = new File(filePathString);
if(f.exists() && !f.isDirectory()) { 
  // do something
}

代码示例来源: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: alibaba/nacos

public static void updateTerm(long term) throws Exception {
  File file = new File(META_FILE_NAME);
  if (!file.exists() && !file.getParentFile().mkdirs() && !file.createNewFile()) {
    throw new IllegalStateException("failed to create meta file");
  }
  try (FileOutputStream outStream = new FileOutputStream(file)) {
    // write meta
    meta.setProperty("term", String.valueOf(term));
    meta.store(outStream, null);
  }
}

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

private void copyAndReplace (String outputDir, Project project, Map<String, String> values) {
  File out = new File(outputDir);
  if (!out.exists() && !out.mkdirs()) {
    throw new RuntimeException("Couldn't create output directory '" + out.getAbsolutePath() + "'");
  }
  for (ProjectFile file : project.files) {
    copyFile(file, out, values);
  }
}

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

private File ensureOutputDirectory() {
  File outputDir = new File(OUTPUT_DIRECTORY);
  if (!outputDir.exists()) {
    outputDir.mkdirs();
  }
  return outputDir;
}

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

File imgFile = new  File("/sdcard/Images/test_image.jpg");

if(imgFile.exists()){

  Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

  ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);

  myImage.setImageBitmap(myBitmap);

}

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

File theDir = new File("new folder");

// if the directory does not exist, create it
if (!theDir.exists()) {
  System.out.println("creating directory: " + directoryName);
  boolean result = false;

  try{
    theDir.mkdir();
    result = true;
  } 
  catch(SecurityException se){
    //handle it
  }        
  if(result) {    
    System.out.println("DIR created");  
  }
}

代码示例来源:origin: apache/incubator-druid

@Test
public void testDecompressBzip2() throws IOException
{
 final File tmpDir = temporaryFolder.newFolder("testDecompressBzip2");
 final File bzFile = new File(tmpDir, testFile.getName() + ".bz2");
 Assert.assertFalse(bzFile.exists());
 try (final OutputStream out = new BZip2CompressorOutputStream(new FileOutputStream(bzFile))) {
  ByteStreams.copy(new FileInputStream(testFile), out);
 }
 try (final InputStream inputStream = CompressionUtils.decompress(new FileInputStream(bzFile), bzFile.getName())) {
  assertGoodDataStream(inputStream);
 }
}

代码示例来源:origin: apache/incubator-druid

@Before
public void setUp() throws IOException
{
 cgroupDir = temporaryFolder.newFolder();
 procDir = temporaryFolder.newFolder();
 TestUtils.setUpCgroups(procDir, cgroupDir);
 cpuacctDir = new File(
   cgroupDir,
   "cpu,cpuacct/system.slice/some.service/f12ba7e0-fa16-462e-bb9d-652ccc27f0ee"
 );
 Assert.assertTrue((cpuacctDir.isDirectory() && cpuacctDir.exists()) || cpuacctDir.mkdirs());
 TestUtils.copyResource("/cpuacct.usage_all", new File(cpuacctDir, "cpuacct.usage_all"));
}

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

protected void deleteConfigurationFile() {
 try {
  File f = new File(getFilename());
  if (f.exists()) {
   f.delete();
  }
 } catch (SecurityException e) {
  System.err.println("Cannot delete " + getFilename() +
    " because a security violation occured.");
 }
}

代码示例来源: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: apache/storm

public static void deleteDir(String dir) {
  File d = new File(dir);
  if (!d.exists()) {
    LOG.warn("dir {} does not exist!", dir);
    return;
  }
  if (!d.isDirectory()) {
    throw new RuntimeException("dir " + dir + " is not a directory!");
  }
  if (!d.delete()) {
    throw new RuntimeException("Cannot delete dir " + dir);
  }
}

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

private InputFile(File file) throws IOException {
  if (!file.exists()) {
    throw new IOException("File not found: " + file.getAbsolutePath());
  }
  this.file = file;
}

代码示例来源: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: org.testng/testng

public static void copyFile(InputStream from, File to) throws IOException {
 if (! to.getParentFile().exists()) {
  to.getParentFile().mkdirs();
 }
 try (OutputStream os = new FileOutputStream(to)) {
  byte[] buffer = new byte[65536];
  int count = from.read(buffer);
  while (count > 0) {
   os.write(buffer, 0, count);
   count = from.read(buffer);
  }
 }
}

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

public boolean updateConsoleLog(File dest, InputStream in) {
  File parentFile = dest.getParentFile();
  parentFile.mkdirs();
  LOGGER.trace("Updating console log [{}]", dest.getAbsolutePath());
  try (OutputStream out = new BufferedOutputStream(new FileOutputStream(dest, dest.exists()))) {
    IOUtils.copy(in, out);
  } catch (IOException e) {
    LOGGER.error("Failed to update console log at : [{}]", dest.getAbsolutePath(), e);
    return false;
  }
  LOGGER.trace("Console log [{}] saved.", dest.getAbsolutePath());
  return true;
}

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

private static void checkFile(File file) {
  if (!file.exists()) {
    throw new JadxArgsValidateException("File not found " + file.getAbsolutePath());
  }
  if (file.isDirectory()) {
    throw new JadxArgsValidateException("Expected file but found directory instead: " + file.getAbsolutePath());
  }
}

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

@Override
  protected void processDir (Entry entryDir, ArrayList<Entry> value) throws Exception {
    if (!entryDir.outputDir.exists()) {
      if (!entryDir.outputDir.mkdirs())
        throw new Exception("Couldn't create output directory '" + entryDir.outputDir + "'");
    }
  }
}

相关文章