List of usage examples for java.io File length
public long length()
From source file:Main.java
public static int checkFileStatus(String dirPath, String fileName, String suffix) { int fileStatus; String filePath = checkFilePath(dirPath, fileName, suffix); if (filePath == null) { return fileStatus = FILE_ERROR; }//from w ww. j a v a 2 s . co m try { File dirFile = new File(dirPath); if (!dirFile.exists()) { dirFile.mkdirs(); } File mFile = new File(filePath); if (!mFile.exists()) { fileStatus = FILE_CREATE; mFile.createNewFile(); } else { long size = mFile.length(); if (size > 0) { fileStatus = FILE_EXISTS; } else { fileStatus = FILE_EXISTS_NULL; } } } catch (IOException e) { e.printStackTrace(); return FILE_ERROR; } return fileStatus; }
From source file:jease.site.Streams.java
/** * Write given file to response./*from w ww. ja va 2 s .c o m*/ * * If the given content type denotes a browser supported image, the image * will be automatically scaled if either "scale" is present as request * paramter or JEASE_IMAGE_LIMIT is set in Registry. */ public static void write(HttpServletRequest request, HttpServletResponse response, File file, String contentType) throws IOException { if (Images.isBrowserCompatible(contentType)) { int scale = NumberUtils.toInt(request.getParameter("scale")); if (scale > 0) { java.io.File scaledImage = Images.scale(file, scale); scaledImage.deleteOnExit(); response.setContentType(contentType); response.setContentLength((int) scaledImage.length()); Files.copy(scaledImage.toPath(), response.getOutputStream()); return; } int limit = NumberUtils.toInt(Registry.getParameter(Names.JEASE_IMAGE_LIMIT)); if (limit > 0) { java.io.File scaledImage = Images.limit(file, limit); scaledImage.deleteOnExit(); response.setContentType(contentType); response.setContentLength((int) scaledImage.length()); Files.copy(scaledImage.toPath(), response.getOutputStream()); return; } } response.setContentType(contentType); response.setContentLength((int) file.length()); Files.copy(file.toPath(), response.getOutputStream()); }
From source file:org.openmrs.module.omodexport.util.OmodExportUtil.java
/** * Utility method for exporting a single module * /* w w w.ja va 2s . com*/ * @param module -The module to export * @param response */ public static void exportSingleModule(Module module, HttpServletResponse response) { File file = module.getFile(); response.setContentLength(new Long(file.length()).intValue()); response.setContentType("application/zip"); response.setHeader("Content-Disposition", "attachment; filename=\"" + file.getName() + "\""); try { FileCopyUtils.copy(new FileInputStream(file), response.getOutputStream()); } catch (IOException e) { e.printStackTrace(); } }
From source file:ca.rmen.android.networkmonitor.app.email.Emailer.java
/** * Append the given attachments to the message which is being written by the given writer. * * @param boundary separates each file attachment *//*w w w .ja va2 s . com*/ private static void appendAttachments(Writer writer, String boundary, Collection<File> attachments) throws IOException { for (File attachment : attachments) { ByteArrayOutputStream fileOs = new ByteArrayOutputStream((int) attachment.length()); FileInputStream fileIs = new FileInputStream(attachment); try { IoUtil.copy(fileIs, fileOs); } finally { IoUtil.closeSilently(fileIs, fileOs); } final String mimeType = attachment.getName().substring(attachment.getName().indexOf(".") + 1); writer.write("--" + boundary + "\n"); writer.write("Content-Type: application/" + mimeType + "; name=\"" + attachment.getName() + "\"\n"); writer.write("Content-Disposition: attachment; filename=\"" + attachment.getName() + "\"\n"); writer.write("Content-Transfer-Encoding: base64\n\n"); String encodedFile = Base64.encodeToString(fileOs.toByteArray(), Base64.DEFAULT); writer.write(encodedFile); writer.write("\n"); } }
From source file:edu.coeia.reports.IndexUtil.java
public static List<String> getAllFilesBetweenSize(final CaseFacade caseFacade, final long from, final long to) throws IOException { List<String> files = new ArrayList<String>(); for (String fileName : getAllFilePaths(caseFacade)) { File file = new File(fileName); long length = file.length(); if (length >= from && length <= to) { files.add(fileName);/*from w ww . j av a 2 s .c o m*/ } } return files; }
From source file:Main.java
public static byte[] encryptMD5File(File file) { FileInputStream in = null;/* ww w . j av a 2 s.c o m*/ try { in = new FileInputStream(file); FileChannel channel = in.getChannel(); MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, file.length()); MessageDigest md = MessageDigest.getInstance("MD5"); md.update(buffer); return md.digest(); } catch (NoSuchAlgorithmException | IOException e) { e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException ignored) { } } } return null; }
From source file:org.eclipse.oomph.setup.internal.sync.SyncUtil.java
public static void inititalizeFile(File file, EClass eClass, ResourceSet resourceSet) { if (file.length() == 0) { EObject rootObject = EcoreUtil.create(eClass); Resource resource = resourceSet .createResource(org.eclipse.emf.common.util.URI.createFileURI(file.getAbsolutePath())); resource.getContents().add(rootObject); BaseUtil.saveEObject(rootObject); }/*from w ww .jav a 2 s .co m*/ }
From source file:Main.java
public static byte[] readFromFile(String fileName, int offset, int len) { if (fileName == null) { return null; }/* w w w. ja v a2s . co m*/ File file = new File(fileName); if (!file.exists()) { Log.i(TAG, "readFromFile: file not found"); return null; } if (len == -1) { len = (int) file.length(); } Log.d(TAG, "readFromFile : offset = " + offset + " len = " + len + " offset + len = " + (offset + len)); if (offset < 0) { Log.e(TAG, "readFromFile invalid offset:" + offset); return null; } if (len <= 0) { Log.e(TAG, "readFromFile invalid len:" + len); return null; } if (offset + len > (int) file.length()) { Log.e(TAG, "readFromFile invalid file len:" + file.length()); return null; } byte[] b = null; try { RandomAccessFile in = new RandomAccessFile(fileName, "r"); b = new byte[len]; // in.seek(offset); in.readFully(b); in.close(); } catch (Exception e) { Log.e(TAG, "readFromFile : errMsg = " + e.getMessage()); e.printStackTrace(); } return b; }
From source file:Main.java
public static byte[] readFromFile(String fileName, int offset, int len) { if (fileName == null) { return null; }/* ww w .j av a2 s . c o m*/ File file = new File(fileName); if (!file.exists()) { Log.i(TAG, "readFromFile: file not found"); return null; } if (len == -1) { len = (int) file.length(); } Log.d(TAG, "readFromFile : offset = " + offset + " len = " + len + " offset + len = " + (offset + len)); if (offset < 0) { Log.e(TAG, "readFromFile invalid offset:" + offset); return null; } if (len <= 0) { Log.e(TAG, "readFromFile invalid len:" + len); return null; } if (offset + len > (int) file.length()) { Log.e(TAG, "readFromFile invalid file len:" + file.length()); return null; } byte[] b = null; try { RandomAccessFile in = new RandomAccessFile(fileName, "r"); b = new byte[len]; in.seek(offset); in.readFully(b); in.close(); } catch (Exception e) { Log.e(TAG, "readFromFile : errMsg = " + e.getMessage()); e.printStackTrace(); } return b; }
From source file:edu.unc.lib.dl.util.FileUtils.java
public static byte[] readFileToByteArray(File file) throws IOException { byte[] buffer = new byte[(int) file.length()]; InputStream ios = null;/*from w w w . ja v a2 s .c o m*/ try { ios = new FileInputStream(file); if (ios.read(buffer) == -1) { throw new IOException("EOF reached while trying to read the whole file"); } return buffer; } finally { if (ios != null) ios.close(); } }