List of usage examples for java.io File length
public long length()
From source file:com.aurel.track.exchange.latex.exporter.LaTeXExportBL.java
/** * Serializes the docx content into the response's output stream * @param response/*from ww w. j av a 2s.c om*/ * @param wordMLPackage * @return */ public static String prepareReportResponse(HttpServletResponse response, TWorkItemBean workItem, ReportBeans reportBeans, TPersonBean user, Locale locale, String templateDir, String templateFile) { ReportBeansToLaTeXConverter rl = new ReportBeansToLaTeXConverter(); File pdf = rl.generatePdf(workItem, reportBeans.getItems(), true, locale, user, "", "", false, new File(templateFile), new File(templateDir)); String fileName = workItem.getSynopsis() + ".pdf"; String contentType = "application/pdf"; if (pdf.length() < 10) { pdf = new File(pdf.getParent() + "/errors.txt"); fileName = workItem.getSynopsis() + ".txt"; contentType = "text"; } OutputStream outputStream = null; try { response.reset(); response.setHeader("Content-Type", contentType); response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); DownloadUtil.prepareCacheControlHeader(ServletActionContext.getRequest(), response); outputStream = response.getOutputStream(); InputStream is = new FileInputStream(pdf); IOUtils.copy(is, outputStream); is.close(); } catch (FileNotFoundException e) { LOGGER.error(ExceptionUtils.getStackTrace(e)); } catch (IOException e) { LOGGER.error("Getting the output stream failed with " + e.getMessage()); LOGGER.error(ExceptionUtils.getStackTrace(e)); } catch (Exception e) { LOGGER.error(ExceptionUtils.getStackTrace(e)); } // Docx4J.save(wordMLPackage, outputStream, Docx4J.FLAG_NONE); // //wordMLPackage.save(outputStream); // /*SaveToZipFile saver = new SaveToZipFile(wordMLPackage); // saver.save(outputStream);*/ // } catch (Exception e) { // LOGGER.error("Exporting the docx failed with throwable " + e.getMessage()); // LOGGER.debug(ExceptionUtils.getStackTrace(e)); // } return null; }
From source file:com.flexive.shared.FxFileUtils.java
/** * Load a file and base64 encode it/*from ww w .j ava2s .co m*/ * * @param file the file to load * @return base64 encoded file content * @throws IOException on errors */ public static String loadBase64Encoded(File file) throws IOException { if (file == null || !file.exists() || file.length() == 0) return ""; return FxFormatUtils.encodeBase64(getBytes(file)); }
From source file:com.microsoft.windowsazure.management.compute.ComputeManagementIntegrationTestBase.java
protected static void uploadFileToBlob(String storageAccountName, String storageContainer, String fileName, String filePath)//from w w w. j a va 2 s .c om throws InvalidKeyException, URISyntaxException, StorageException, InterruptedException, IOException { MockCloudBlobClient blobClient = createBlobClient(storageAccountName, storageAccountKey); MockCloudBlobContainer container = blobClient.getContainerReference(storageContainer); MockCloudPageBlob pageblob = container.getPageBlobReference(fileName); File source = new File(filePath + fileName); pageblob.upload(new FileInputStream(source), source.length()); //make sure it created and available, otherwise vm deployment will fail with storage/container still creating boolean found = false; while (found == false) { // Loop over blobs within the container and output the URI to each of them for (MockListBlobItem item : container.listBlobs()) { if (item.getUri().getPath().contains(fileName) == true) { found = true; } } if (found == false) { Thread.sleep(1000 * 10); } else if (!IS_MOCKED) { Thread.sleep(1000 * 20); } } }
From source file:Main.java
/** *Valid plugin md5// w ww .j av a 2s .c om * @param path bundle archvie path * @param md5Sum target file md5 * @return if md5 matched,return true * ***/ public static boolean validFileMD5(String path, String md5Sum) { try { MessageDigest messageDigest = MessageDigest.getInstance("MD5"); File mFile = new File(path); if (mFile == null || !mFile.exists() || !mFile.isFile()) { return false; } FileInputStream in = new FileInputStream(mFile); FileChannel ch = in.getChannel(); MappedByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, 0, mFile.length()); messageDigest.update(byteBuffer); String digest = String.format("%032x", new BigInteger(1, messageDigest.digest())); return md5Sum.equals(digest.toString()); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { } return false; }
From source file:Main.java
public static StringBuilder listFilesByName(File f, String lineSep) { Log.d("listFileByName f", "" + f); StringBuilder sb = new StringBuilder(); if (f != null) { final Stack<File> stk = new Stack<>(); if (f.isDirectory()) { stk.push(f);//from www. j a v a2 s. c om } else { sb.append(f.getAbsolutePath()).append(": ").append(f.length()).append(" bytes.").append(lineSep); } File fi = null; File[] fs; while (stk.size() > 0) { fi = stk.pop(); fs = fi.listFiles(); for (File f2 : fs) { if (f2.isDirectory()) { stk.push(f2); } else { sb.append(f2.getAbsolutePath()).append(": ").append(f2.length()).append(" bytes.") .append(lineSep); } } } } return sb; }
From source file:ch.entwine.weblounge.common.impl.util.TestUtils.java
/** * Loads the <code>XML</code> data from the specified file on the class path * and returns it after having stripped off any newlines, line breaks and * otherwise disturbing spaces and characters. * // w w w . ja v a 2s.c o m * @param path * the resource path * @return the contents of the resource */ public static String loadXmlFromResource(String path) { File templateFile = new File(TestUtils.class.getResource(path).getPath()); String template = null; try { byte[] buffer = new byte[(int) templateFile.length()]; FileInputStream f = new FileInputStream(templateFile); f.read(buffer); f.close(); template = new String(buffer, "utf-8").replaceFirst("<\\?.*?>", ""); template = template.replaceAll("(>\\s*)+", ">").replaceAll("(\\s*<)+", "<"); } catch (IOException e) { throw new RuntimeException("Error reading test resource at " + path); } return template; }
From source file:com.flexive.shared.FxFileUtils.java
/** * Load a file into a byte array/*from www. j a v a 2 s. co m*/ * * @param file the file to load * @return byte[] * @throws IOException on errors */ public static byte[] getBytes(File file) throws IOException { InputStream is = null; if (file.length() > Integer.MAX_VALUE) throw new IOException("File " + file.getAbsolutePath() + " is too large!"); byte[] bytes = new byte[(int) file.length()]; try { is = new FileInputStream(file); int curr = 0; int read; while (curr < bytes.length && (read = is.read(bytes, curr, bytes.length - curr)) >= 0) curr += read; if (curr < bytes.length) throw new IOException("Failed to fully read " + file.getAbsolutePath() + "!"); } finally { if (is != null) is.close(); } return bytes; }
From source file:com.maydesk.base.util.PDUtil.java
public static byte[] readBytesFromFile(File file) throws IOException { byte[] bytesOfFile = new byte[(int) file.length()]; DataInputStream dis = new DataInputStream(new FileInputStream(file)); dis.readFully(bytesOfFile);//from w w w .j ava 2 s .co m dis.close(); return bytesOfFile; }
From source file:ch.entwine.weblounge.common.impl.util.TestUtils.java
/** * Loads the <code>JSON</code> data from the specified file on the class path * and returns it after having stripped off any newlines, line breaks and * otherwise disturbing spaces and characters. * //from www.j a v a 2 s . c o m * @param path * the resource path * @return the contents of the resource */ public static String loadJsonFromResource(String path) { File templateFile = new File(TestUtils.class.getResource(path).getPath()); String template = null; try { byte[] buffer = new byte[(int) templateFile.length()]; FileInputStream f = new FileInputStream(templateFile); f.read(buffer); f.close(); template = new String(buffer, "utf-8"); template = template.replaceAll("(\"\\s*)", "\"").replaceAll("(\\s*\")+", "\""); template = template.replaceAll("(\\s*\\{\\s*)", "{").replaceAll("(\\s*\\}\\s*)", "}"); template = template.replaceAll("(\\s*\\[\\s*)", "[").replaceAll("(\\s*\\]\\s*)", "]"); template = template.replaceAll("(\\s*,\\s*)", ","); } catch (IOException e) { throw new RuntimeException("Error reading test resource at " + path); } return template; }
From source file:jetbrains.exodus.env.EnvironmentTestsBase.java
public static void archiveDB(final String location, final String target) { try {//from w w w .j a v a 2 s.c om System.out.println("Dumping " + location + " to " + target); final File root = new File(location); final File targetFile = new File(target); TarArchiveOutputStream tarGz = new TarArchiveOutputStream( new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream(targetFile)), 0x1000)); for (final File file : IOUtil.listFiles(root)) { final long fileSize = file.length(); if (file.isFile() && fileSize != 0) { CompressBackupUtil.archiveFile(tarGz, "", file, fileSize); } } tarGz.close(); } catch (IOException ioe) { System.out.println("Can't create backup"); } }