File f = new File(filePathString); if(f.exists() && !f.isDirectory()) { // do something }
// create a File object for the parent directory File wallpaperDirectory = new File("/sdcard/Wallpaper/"); // have the object build the directory structure, if needed. wallpaperDirectory.mkdirs(); // create a File object for the output file File outputFile = new File(wallpaperDirectory, filename); // now attach the OutputStream to the file object, instead of a String representation FileOutputStream fos = new FileOutputStream(outputFile);
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); } }
resolveFile = System.getProperty("dubbo.resolve.file"); if (StringUtils.isEmpty(resolveFile)) { File userResolveFile = new File(new File(System.getProperty("user.home")), "dubbo-resolve.properties"); if (userResolveFile.exists()) { resolveFile = userResolveFile.getAbsolutePath(); try (FileInputStream fis = new FileInputStream(new File(resolveFile))) { properties.load(fis); } catch (IOException e) {
private void save(String path) throws IOException { System.out.print("Saving classifier to " + path + "... "); // make sure the directory specified by path exists int lastSlash = path.lastIndexOf(File.separator); if (lastSlash > 0) { File dir = new File(path.substring(0, lastSlash)); if (! dir.exists()) dir.mkdirs(); } ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(path)); out.writeObject(weights); out.writeObject(featureIndex); out.writeObject(labelIndex); out.close(); System.out.println("done."); }
String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/PhysicsSketchpad"; File dir = new File(file_path); if(!dir.exists()) dir.mkdirs(); File file = new File(dir, "sketchpad" + pad.t_id + ".png"); FileOutputStream fOut = new FileOutputStream(file); bmp.compress(Bitmap.CompressFormat.PNG, 85, fOut); fOut.flush(); fOut.close();
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"); } }
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); }
@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")); }
private File setupFile(String testDir, String fileName, String content) throws Exception { File file = new File(testDir, fileName); if (file.exists()) { file.delete(); } BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8")); out.write(content); out.close(); return file; }
ByteArrayOutputStream bytes = new ByteArrayOutputStream(); _bitmapScaled.compress(Bitmap.CompressFormat.JPEG, 40, bytes); //you can create a new file name "test.jpg" in sdcard folder. File f = new File(Environment.getExternalStorageDirectory() + File.separator + "test.jpg"); f.createNewFile(); //write the bytes in file FileOutputStream fo = new FileOutputStream(f); fo.write(bytes.toByteArray()); // remember close de FileOutput fo.close();
public void run() throws Exception { File file = new File("README.md"); Request request = new Request.Builder() .url("https://api.github.com/markdown/raw") .post(RequestBody.create(MEDIA_TYPE_MARKDOWN, file)) .build(); try (Response response = client.newCall(request).execute()) { if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); System.out.println(response.body().string()); } }
private static void setRunningLocation(CliMain m) { mRunningLocation = m.getClass().getProtectionDomain().getCodeSource().getLocation().getPath(); try { mRunningLocation = URLDecoder.decode(mRunningLocation, "utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } if (mRunningLocation.endsWith(".jar")) { mRunningLocation = mRunningLocation.substring(0, mRunningLocation.lastIndexOf(File.separator) + 1); } File f = new File(mRunningLocation); mRunningLocation = f.getAbsolutePath(); }
private InputStream getFileAsStream(String filePath) throws FileNotFoundException { InputStream inStream = null; File file = new File(filePath); if (file.exists()) { inStream = new FileInputStream(file); } else { inStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath); } return inStream; } }
File folder = new File("your/path"); File[] listOfFiles = folder.listFiles(); for (int i = 0; i < listOfFiles.length; i++) { if (listOfFiles[i].isFile()) { System.out.println("File " + listOfFiles[i].getName()); } else if (listOfFiles[i].isDirectory()) { System.out.println("Directory " + listOfFiles[i].getName()); } }
File sdCard = Environment.getExternalStorageDirectory(); File dir = new File (sdCard.getAbsolutePath() + "/dir1/dir2"); dir.mkdirs(); File file = new File(dir, "filename"); FileOutputStream f = new FileOutputStream(file); ...
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(); } } }
private Object test(Object obj) { FileOutputStream output = null; try { output = new FileOutputStream(new File("f")); return new Object(); } catch (FileNotFoundException e) { System.out.println("Exception"); return null; } } }