Tabnine Logo
File.exists
Code IndexAdd Tabnine to your IDE (free)

How to use
exists
method
in
java.io.File

Best Java code snippets using java.io.File.exists (Showing top 20 results out of 137,250)

Refine searchRefine arrow

  • File.<init>
  • File.getAbsolutePath
  • File.mkdirs
  • File.isDirectory
  • FileOutputStream.<init>
  • File.delete
  • PrintStream.println
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 + "'");
    }
  }
}
java.ioFileexists

Javadoc

Returns a boolean indicating whether this file can be found on the underlying file system.

Popular methods of File

  • <init>
    Creates a new File instance by converting the givenfile: URI into an abstract pathname. The exact fo
  • getAbsolutePath
    Returns the absolute pathname string of this abstract pathname. If this abstract pathname is already
  • getName
    Returns the name of the file or directory denoted by this abstract pathname. This is just the last n
  • isDirectory
  • mkdirs
  • delete
    Deletes the file or directory denoted by this abstract pathname. If this pathname denotes a director
  • listFiles
    Returns an array of abstract pathnames denoting the files and directories in the directory denoted b
  • getParentFile
    Returns the abstract pathname of this abstract pathname's parent, or null if this pathname does not
  • getPath
    Converts this abstract pathname into a pathname string. The resulting string uses the #separator to
  • isFile
  • length
    Returns the length of the file denoted by this abstract pathname. The return value is unspecified if
  • toURI
  • length,
  • toURI,
  • createTempFile,
  • createNewFile,
  • toPath,
  • mkdir,
  • lastModified,
  • toString,
  • getCanonicalPath

Popular in Java

  • Running tasks concurrently on multiple threads
  • findViewById (Activity)
  • setRequestProperty (URLConnection)
  • getSharedPreferences (Context)
  • BufferedWriter (java.io)
    Wraps an existing Writer and buffers the output. Expensive interaction with the underlying reader is
  • EOFException (java.io)
    Thrown when a program encounters the end of a file or stream during an input operation.
  • KeyStore (java.security)
    KeyStore is responsible for maintaining cryptographic keys and their owners. The type of the syste
  • Random (java.util)
    This class provides methods that return pseudo-random values.It is dangerous to seed Random with the
  • JLabel (javax.swing)
  • JOptionPane (javax.swing)
  • Top Vim plugins
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now