List of usage examples for java.io File createNewFile
public boolean createNewFile() throws IOException
From source file:Main.java
public static File touch(String fullFilePath) throws IOException { if (fullFilePath == null) { return null; }/*from www .j a v a2 s.co m*/ File file = new File(fullFilePath); file.getParentFile().mkdirs(); if (!file.exists()) file.createNewFile(); return file; }
From source file:Main.java
public static void writeFile(File file, byte[] content) throws IOException { if (!file.exists()) { try {/*from ww w . j a va2 s.c om*/ file.createNewFile(); } catch (IOException e) { throw new IOException("not crete file=" + file.getAbsolutePath()); } } FileOutputStream fileOutputStream = null; ByteArrayInputStream bis = null; try { bis = new ByteArrayInputStream(content); fileOutputStream = new FileOutputStream(file, false); byte[] buffer = new byte[1024]; int length = 0; while ((length = bis.read(buffer)) != -1) { fileOutputStream.write(buffer, 0, length); } fileOutputStream.flush(); } finally { if (fileOutputStream != null) { fileOutputStream.close(); } if (bis != null) { bis.close(); } } }
From source file:com.comcast.cats.service.util.YAMLUtils.java
@SuppressWarnings("unchecked") public static synchronized <T> T loadFromYAML(String filePath, T resultObject, Constructor constructor) throws IllegalClassException, IOException { Yaml yaml;/*from ww w . j a v a2 s.c o m*/ FileInputStream fileIS = null; if (constructor != null) { yaml = new Yaml(constructor); } else { yaml = new Yaml(); } try { fileIS = new FileInputStream(filePath); resultObject = (T) yaml.load(fileIS); } catch (FileNotFoundException exception) { File file = new File(filePath); file.createNewFile(); // create file if one does not exist. fileIS = new FileInputStream(filePath); resultObject = (T) yaml.load(fileIS); } catch (ClassCastException e) { logger.warn( "YAML content found at " + filePath + " does not conform to passed object " + e.getMessage()); throw new IllegalClassException( "YAML content found at " + filePath + " does not conform to passed object " + e.getMessage()); } finally { if (fileIS != null) { fileIS.close(); } } return resultObject; }
From source file:Main.java
public static Properties loadPropertyInstance(String filePath, String fileName) { try {//w w w . j av a 2 s . co m File d = new File(filePath); if (!d.exists()) { d.mkdirs(); } File f = new File(d, fileName); if (!f.exists()) { f.createNewFile(); } Properties p = new Properties(); InputStream is = new FileInputStream(f); p.load(is); is.close(); return p; } catch (Exception e) { e.printStackTrace(); return null; } }
From source file:Main.java
public static boolean saveBitmap2File(Bitmap bitmap, File file) { FileOutputStream fos = null;/*from w w w . ja v a2 s . c o m*/ try { file.deleteOnExit(); file.createNewFile(); fos = new FileOutputStream(file); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); fos.flush(); } catch (IOException e) { e.printStackTrace(); return false; } finally { if (fos != null) { try { fos.close(); } catch (IOException e1) { e1.printStackTrace(); } } } return true; }
From source file:com.comcast.cats.service.util.YAMLUtils.java
public static synchronized <T> void saveAsYAML(String filePath, T objectToSave, Representer representer) throws FileNotFoundException { if (objectToSave != null) { Yaml yaml;//w w w. j ava 2s . co m FileWriter fw = null; if (representer == null) { yaml = new Yaml(); } else { yaml = new Yaml(representer); } try { File file = new File(filePath); file.createNewFile(); // create file if one does not exist. fw = new FileWriter(filePath); yaml.dump(objectToSave, fw); } catch (Exception e) { e.printStackTrace(); } finally { if (fw != null) { try { fw.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
From source file:Main.java
public static void unZipFolder(InputStream input, String outPathString) throws Exception { java.util.zip.ZipInputStream inZip = new java.util.zip.ZipInputStream(input); java.util.zip.ZipEntry zipEntry = null; String szName = ""; while ((zipEntry = inZip.getNextEntry()) != null) { szName = zipEntry.getName();/* w ww . j a v a2 s .c om*/ if (zipEntry.isDirectory()) { // get the folder name of the widget szName = szName.substring(0, szName.length() - 1); java.io.File folder = new java.io.File(outPathString + java.io.File.separator + szName); folder.mkdirs(); } else { java.io.File file = new java.io.File(outPathString + java.io.File.separator + szName); file.createNewFile(); // get the output stream of the file java.io.FileOutputStream out = new java.io.FileOutputStream(file); int len; byte[] buffer = new byte[1024]; // read (len) bytes into buffer while ((len = inZip.read(buffer)) != -1) { // write (len) byte from buffer at the position 0 out.write(buffer, 0, len); out.flush(); } out.close(); } } //end of while inZip.close(); }
From source file:Main.java
private static void createNoMediaFile(File file) { try {/*from w ww . ja va 2 s .c om*/ File noMediaFile = new File(file, NO_MEDIA_FILE); if (!noMediaFile.exists()) { noMediaFile.createNewFile(); } } catch (IOException e) { Log.w(TAG, e.toString()); } }
From source file:com.z2data.files.WriteOperations.java
/** * Write status Data for URL To file//from w w w . j a v a 2 s . co m * * @param outputPath * @param urlData */ @SuppressWarnings("deprecation") public static void writeURLData(String outputPath, URLData urlData) { try { File file = new File(outputPath); if (!file.exists()) { file.createNewFile(); } FileUtils.writeStringToFile(file, urlData.getParentURL() + "\t" + urlData.getAnchorTitle() + "\t" + urlData.getAnchor() + "\t" + urlData.getDownloadStatus() + "\t" + urlData.getDownloadPath(), true); FileUtils.writeStringToFile(file, System.lineSeparator(), true); } catch (Exception ex) { ex.printStackTrace(); } }
From source file:Main.java
@SuppressLint("NewApi") public static void getURL(String path) { String fileName = ""; String dir = "/IndoorNavi/"; File sdRoot = Environment.getExternalStorageDirectory(); try {/*w w w . j a va 2 s. c o m*/ // Open the URLConnection for reading URL u = new URL(path); // URL u = new URL("http://www.baidu.com/"); HttpURLConnection uc = (HttpURLConnection) u.openConnection(); int code = uc.getResponseCode(); String response = uc.getResponseMessage(); //System.out.println("HTTP/1.x " + code + " " + response); for (int j = 1;; j++) { String key = uc.getHeaderFieldKey(j); String header = uc.getHeaderField(j); if (!(key == null)) { if (key.equals("Content-Name")) fileName = header; } if (header == null || key == null) break; //System.out.println(uc.getHeaderFieldKey(j) + ": " + header); } Log.i("zhr", fileName); //System.out.println(); try (InputStream in = new BufferedInputStream(uc.getInputStream())) { // chain the InputStream to a Reader Reader r = new InputStreamReader(in); int c; File mapFile = new File(sdRoot, dir + fileName); mapFile.createNewFile(); FileOutputStream filecon = new FileOutputStream(mapFile); while ((c = r.read()) != -1) { //System.out.print((char) c); filecon.write(c); filecon.flush(); } filecon.close(); } } catch (MalformedURLException ex) { System.err.println(path + " is not a parseable URL"); } catch (IOException ex) { System.err.println(ex); } }