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(); }
File f = new File(filePathString); if(f.exists() && !f.isDirectory()) { // do something }
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); } }
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); } }
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); }
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"); } }
@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); } }
@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")); }
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); } }
private InputFile(File file) throws IOException { if (!file.exists()) { throw new IOException("File not found: " + file.getAbsolutePath()); } this.file = file; }
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(); }
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); } } }
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; }
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()); } }