List of usage examples for java.io File length
public long length()
From source file:com.hernandez.rey.crypto.TripleDES.java
/** * Read a TripleDES secret key from the specified file * /* w w w.j a va 2s . c om*/ * @param keyFile key file to read * @return secret key appropriate for encryption/decryption with 3DES * @throws IOException * @throws NoSuchAlgorithmException * @throws InvalidKeyException * @throws InvalidKeySpecException */ public static SecretKey readKey(final File keyFile) throws IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidKeySpecException { // Read the raw bytes from the keyfile final DataInputStream in = new DataInputStream(new FileInputStream(keyFile)); final byte[] rawkey = new byte[(int) keyFile.length()]; in.readFully(rawkey); in.close(); // Convert the raw bytes to a secret key like this final DESedeKeySpec keyspec = new DESedeKeySpec(rawkey); final SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("DESede"); final SecretKey key = keyfactory.generateSecret(keyspec); return key; }
From source file:net.alteiar.utils.file.SerializableFile.java
private static byte[] getBytesFromFile(File file) throws IOException { InputStream is = null;/*from w w w .j av a2 s .co m*/ IOException ex = null; byte[] bytes = null; try { is = new FileInputStream(file); // Get the size of the file long length = file.length(); if (length > Integer.MAX_VALUE) { // File is too large Logger.getLogger(SerializableFile.class).error("Impossible de lire le fichier, fichier trop gros"); return null; } // Create the byte array to hold the data bytes = new byte[(int) length]; // Read in the bytes int offset = 0; int numRead = 0; while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) { offset += numRead; } // Ensure all the bytes have been read in if (offset < bytes.length) { ex = new IOException("Could not completely read file " + file.getName()); } } catch (IOException e) { ex = e; } finally { if (is != null) { try { is.close(); } catch (IOException e) { ex = e; } } } if (ex != null) { throw ex; } return bytes; }
From source file:iqq.util.ImageUtil.java
/** * ??//from w ww .j a v a2s . c o m * * @param imageFile * @return ? */ public static String getFormatName(File imageFile) { if (imageFile == null || imageFile.length() == 0) { return null; } try { String formatName = null; ImageInputStream imageInputStream = ImageIO.createImageInputStream(imageFile); Iterator<ImageReader> iterator = ImageIO.getImageReaders(imageInputStream); if (!iterator.hasNext()) { return null; } ImageReader imageReader = iterator.next(); if (imageReader instanceof JPEGImageReader) { formatName = JPEG_FORMAT_NAME; } else if (imageReader instanceof GIFImageReader) { formatName = GIF_FORMAT_NAME; } else if (imageReader instanceof BMPImageReader) { formatName = BMP_FORMAT_NAME; } else if (imageReader instanceof PNGImageReader) { formatName = PNG_FORMAT_NAME; } imageInputStream.close(); return formatName; } catch (IOException e) { e.printStackTrace(); return null; } }
From source file:com.twosigma.beaker.r.rest.RShellRest.java
private static boolean addPngResults(String name, SimpleEvaluationObject obj) { try {/*from w w w . j av a 2s . co m*/ File file = new File(name); if (file.length() > 0) { obj.finished(new ImageIcon(ImageIO.read(file))); return true; } } catch (IOException e) { System.out.println("IO error on " + name + " " + e); } return false; }
From source file:cn.fql.utility.FileUtility.java
/** * read file to byte[] with specified file path * * @param filePath specified file path/*from w w w. j a v a2 s .co m*/ * @return <code>byte[]</code> file content */ public static byte[] readFile(String filePath) { File infoFile = new File(filePath); byte[] result = null; if (infoFile.exists()) { result = new byte[(int) infoFile.length()]; try { FileInputStream fis = new FileInputStream(infoFile); fis.read(result); fis.close(); } catch (IOException e) { e.printStackTrace(); } } return result; }
From source file:de.longri.cachebox3.Utils.java
/** * berprft ob ein File existiert! Und nicht leer ist (0 Bytes) * * @param filename/*from ww w. j a va 2s .c o m*/ * @return true, wenn das File existiert, ansonsten false. */ public static boolean FileExistsNotEmpty(String filename) { File file = new File(filename); if (!file.exists()) return false; if (file.length() <= 0) return false; return true; }
From source file:Main.java
private static float getSize(String path, Float size) { File file = new File(path); if (file.exists()) { if (file.isDirectory()) { String[] children = file.list(); for (int fileIndex = 0; fileIndex < children.length; ++fileIndex) { float tmpSize = getSize(file.getPath() + File.separator + children[fileIndex], size) / 1000; size += tmpSize;/*from w w w .j a v a 2 s . c o m*/ } } else if (file.isFile()) { size += file.length(); } } return size; }
From source file:com.googlecode.xmlzen.utils.FileUtils.java
/** * Reads a {@link File} and returns the contents as {@link String} * //from w w w. ja v a 2 s . c o m * @param file File to read * @param charset Charset of this File * @return File contents as String */ public static String readFile(final File file, final String charset) { InputStream in = null; try { if (!file.isFile()) { return null; } in = new FileInputStream(file); final BufferedInputStream buffIn = new BufferedInputStream(in); final byte[] buffer = new byte[(int) Math.min(file.length(), BUFFER)]; int read; final StringBuilder result = new StringBuilder(); while ((read = buffIn.read(buffer)) != -1) { result.append(new String(buffer, 0, read, charset)); } return result.toString(); } catch (final Exception e) { throw new XmlZenException("Failed reading file: " + file, e); } finally { close(in); } }
From source file:com.sds.acube.ndisc.xnapi.XNApiUtils.java
/** * ?? ? ? ?/*w w w.j ava 2s. co m*/ * * @param nFile * NFile * @return ? ? ? NFile * @throws Exception */ public static NFile[] makeRegInfo(NFile[] nFile) throws Exception { try { for (int i = 0; i < nFile.length; i++) { File file = new File(nFile[i].getName()); if (!file.exists()) { throw new FileException("file not found : " + nFile[i].getName()); } nFile[i].setSize((int) file.length()); if (null == nFile[i].getId() || "".equals(nFile[i].getId())) { nFile[i].setId(XNApiConfig.NDISC_NA_RESERV); } if (null == nFile[i].getCreatedDate() || "".equals(nFile[i].getCreatedDate())) { nFile[i].setCreatedDate(XNApiConfig.NDISC_NA_RESERV); } } } catch (Exception e) { throw e; } finally { } return nFile; }
From source file:de.tudarmstadt.ukp.wikipedia.util.StringUtils.java
public static String getFileContent(String filename, String encoding) { File file = new File(filename); InputStream is;//w w w. j a va2s.co m String textContents = ""; try { is = new FileInputStream(file); // as the whole file is read at once -> buffering not necessary // InputStream is = new BufferedInputStream(new FileInputStream(file)); byte[] contents = new byte[(int) file.length()]; is.read(contents); textContents = new String(contents, encoding); } catch (FileNotFoundException e) { logger.error("File " + file.getAbsolutePath() + " not found."); e.printStackTrace(); } catch (IOException e) { logger.error("IO exception while reading file " + file.getAbsolutePath()); e.printStackTrace(); } return textContents; }