List of usage examples for java.io File getParentFile
public File getParentFile()
null
if this pathname does not name a parent directory. From source file:Main.java
public static void writeContentToFile(String fileName, String contents) throws IOException { Log.d("writeContentToFile", fileName); File f = new File(fileName); f.getParentFile().mkdirs(); File tempFile = new File(fileName + ".tmp"); FileWriter fw = new FileWriter(tempFile); BufferedWriter bw = new BufferedWriter(fw); int length = contents.length(); if (length > 0) { bw.write(contents);//from ww w . j av a 2s . co m // int apart = Math.min(length, 65536); // int times = length / apart; // for (int i = 0; i < times; i++) { // bw.write(contents, i * apart, apart); // } // if (length % apart != 0) { // bw.write(contents, times * apart, length - times * apart); // } bw.flush(); fw.flush(); bw.close(); fw.close(); f.delete(); tempFile.renameTo(f); } }
From source file:Main.java
public static File writeAssetToFile(Context ctx, String assetName, String destinationFile) throws IOException { File destination = new File(destinationFile); if (!destination.getParentFile().exists()) destination.getParentFile().mkdirs(); if (destination.exists()) destination.delete();/* ww w . j a v a 2 s .co m*/ InputStream is = null; FileOutputStream fos = null; try { is = ctx.getAssets().open(assetName); fos = new FileOutputStream(destination); int read; byte[] buf = new byte[65536]; while ((read = is.read(buf, 0, buf.length)) != -1) { fos.write(buf, 0, read); } } finally { if (is != null) is.close(); if (fos != null) fos.close(); } return destination; }
From source file:fr.norad.servlet.sample.html.template.ContextUtils.java
public static void servletContextResourceToFile(ServletContext context, String contextPath, String output) throws IOException { @SuppressWarnings("unchecked") Set<String> resources = context.getResourcePaths(contextPath); if (resources == null) { new File(output + contextPath).mkdirs(); return;/*www . j a v a 2 s.c o m*/ } for (String resource : resources) { InputStream resourceAsStream = context.getResourceAsStream(resource); if (resourceAsStream == null) { // this should be a directory servletContextResourceToFile(context, resource, output); } else { // this is a file try { File full = new File(output + resource); full.getParentFile().mkdirs(); FileUtils.copyInputStreamToFile(resourceAsStream, full); } finally { resourceAsStream.close(); } } } }
From source file:Utils.java
/** * unpack a segment from a zip/*from w ww.j a v a 2 s. com*/ * * @param addsi * @param packetStream * @param version */ public static void unpack(InputStream source, File destination) throws IOException { ZipInputStream zin = new ZipInputStream(source); ZipEntry zipEntry = null; FileOutputStream fout = null; byte[] buffer = new byte[4096]; while ((zipEntry = zin.getNextEntry()) != null) { long ts = zipEntry.getTime(); // the zip entry needs to be a full path from the // searchIndexDirectory... hence this is correct File f = new File(destination, zipEntry.getName()); f.getParentFile().mkdirs(); fout = new FileOutputStream(f); int len; while ((len = zin.read(buffer)) > 0) { fout.write(buffer, 0, len); } zin.closeEntry(); fout.close(); f.setLastModified(ts); } fout.close(); }
From source file:Main.java
/** * Compress a String to a zip file that has only one entry like zipName * The name shouldn't have .zip//from www . j av a2 s . com * * @param content * @param fName * @throws IOException */ public static void zipAContentAsFileName(String fName, String content, String charset) throws IOException { ByteArrayInputStream bais = new ByteArrayInputStream(content.getBytes(charset)); BufferedInputStream bis = new BufferedInputStream(bais); File f = new File(fName); File parentFile = f.getParentFile(); if (!parentFile.exists()) { parentFile.mkdirs(); } ZipOutputStream zf = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(f + ".zip"))); ZipEntry entry = new ZipEntry(f.getName()); zf.putNextEntry(entry); byte[] barr = new byte[8192]; int len = 0; while ((len = bis.read(barr)) != -1) { zf.write(barr, 0, len); } zf.flush(); zf.close(); bis.close(); bais.close(); }
From source file:Main.java
/** * Devuelve el fully qualified name de UseCaseName a partir * del path de una pagina (preferentemente el absoluto) *//* w w w .j a va 2s. c o m*/ public static String getImportUseCaseName(String path) { File arch = new File(path); //legar hasta client: pages=>caso de uso=>useCases=>client File client = arch.getParentFile().getParentFile().getParentFile().getParentFile(); return client.getParentFile().getName() + "." + client.getName() + "." + "UseCaseNames"; }
From source file:Main.java
public static void makedirs(File file) throws IOException { checkFile(file);/*w w w .j a v a 2 s .c om*/ File parentFile = file.getParentFile(); if (parentFile != null) { if (!parentFile.exists() && !parentFile.mkdirs()) { throw new IOException("Creating directories " + parentFile.getPath() + " failed."); } } }
From source file:com.mobius.software.mqtt.performance.runner.util.FileUtil.java
public static void logErrors(UUID scenarioID, List<ClientReport> reports) { if (reports.isEmpty()) return;/*from ww w . ja va 2 s . c o m*/ String filename = DIRECTORY_NAME + File.separator + scenarioID.toString() + LOG_EXTENSION; File log = new File(filename); if (!log.getParentFile().exists()) log.getParentFile().mkdir(); try (PrintWriter pw = new PrintWriter(log)) { for (ClientReport clientReport : reports) { List<ErrorReport> errorReports = clientReport.getErrors(); if (!errorReports.isEmpty()) { String errorContent = ReportBuilder.buildError(clientReport.getIdentifier(), errorReports); pw.println(errorContent); } } } catch (IOException e) { logger.error("An error occured while writing error reports to file:" + e.getMessage()); } if (log.length() == 0) log.delete(); }
From source file:com.netflix.spinnaker.clouddriver.appengine.artifacts.StorageUtils.java
public static void writeStreamToFile(InputStream sourceStream, File target) throws IOException { File parent = target.getParentFile(); if (!parent.exists()) { parent.mkdirs();//from ww w . ja va2s. c o m } OutputStream targetStream = new FileOutputStream(target); IOUtils.copy(sourceStream, targetStream); targetStream.close(); }
From source file:Main.java
public static void saveBitmap(Bitmap bitmap, String descPath) { File file = new File(descPath); if (!file.getParentFile().exists()) { file.getParentFile().mkdirs();//from w w w .ja v a 2 s. co m } if (!file.exists()) { try { bitmap.compress(CompressFormat.JPEG, 30, new FileOutputStream(file)); } catch (FileNotFoundException e) { e.printStackTrace(); } } if (null != bitmap) { bitmap.recycle(); bitmap = null; } }