List of usage examples for java.io File length
public long length()
From source file:arena.utils.FileUtils.java
public static void copyFile(File from, File to, boolean appendNotReplace) throws IOException { InputStream in = null;/* w w w . j a v a 2 s . c o m*/ OutputStream out = null; try { if (!appendNotReplace && to.exists() && to.length() > 0) { to.delete(); } to.getParentFile().mkdirs(); in = new FileInputStream(from); out = new FileOutputStream(to, true); byte buffer[] = new byte[4096]; int read = 0; while ((read = in.read(buffer)) != -1) { out.write(buffer, 0, read); } } finally { if (in != null) { try { in.close(); } catch (IOException err) { } } if (out != null) { try { out.close(); } catch (IOException err) { } } } }
From source file:ch.admin.suis.msghandler.util.FileUtils.java
/** * Mantis: 0006311. Checks for free diskspace before copy or move a file! * * @param src The file to copy//from ww w.j a va2 s .c om * @param dest The destination * @throws IOException Ouch, IO problem here. Probably not enough disk space */ private static void checkForFreeDiskSpace(File src, File dest) throws IOException { long requiredBytes = src.length(); long freeSpaceBytes = getFreeDiskSpace(dest); if (requiredBytes > freeSpaceBytes) { String msg = "Not enough free disk space. Required Bytes: " + requiredBytes + ", available Bytes: " + freeSpaceBytes; LOG.error("Unable to copy file: src: " + src.getAbsolutePath() + " dest: " + dest.getAbsolutePath() + ". Message: " + msg); throw new IOException(msg); } }
From source file:$.FileUtils.java
/** * Read file of URL into file.//from w w w . j a v a 2s . co m * @param url URL where the input file is located * @return Result file */ public static File urlToFile(URL url, String ext) throws IOException { File fOut = null; fOut = getTmpFile("fromurl", "." + ext); URLConnection uc = url.openConnection(); logger.info("ContentType: " + uc.getContentType()); InputStream in = uc.getInputStream(); org.apache.commons.io.FileUtils.copyInputStreamToFile(in, fOut); logger.info("File of length " + fOut.length() + " created from URL " + url.toString()); in.close(); return fOut; }
From source file:eu.scape_project.archiventory.utils.IOUtils.java
public static byte[] getBytesFromFile(String filePath) throws IOException { File file = new File(filePath); if (!file.exists()) { throw new FileNotFoundException("File not available"); }//from w ww . java 2s . c o m InputStream is = null; byte[] bytes = null; try { is = new FileInputStream(file); long length = file.length(); if (length > Integer.MAX_VALUE) { throw new IllegalArgumentException("File object is too large"); } bytes = new byte[(int) length]; int offset = 0; int numRead = 0; while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) { offset += numRead; } } catch (IOException ex) { logger.error("I/O Error", ex); } finally { if (is != null) { try { is.close(); } catch (IOException _) { // ignore } } } return bytes; }
From source file:DiskIO.java
public static String readFileInString(File fromFile) throws IOException { StringBuffer strbuf = new StringBuffer((int) fromFile.length()); BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(fromFile), "ISO-8859-1")); String str;/*w w w . j a v a 2 s . c o m*/ strbuf = new StringBuffer((int) fromFile.length()); while ((str = in.readLine()) != null) { strbuf.append(str + "\n"); } in.close(); return strbuf.toString(); /* * int lineNumber = 0; byte[] buffer = new byte[1024]; int read; * StringBuffer out = new StringBuffer((int)fromFile.length()); * FileInputStream in = new FileInputStream( fromFile ); * * read = in.read(buffer); while ( read == 1024 ) { out.append(new * String(buffer,"ISO-8859-1")); read = in.read(buffer); } * * out.append(new String(buffer,0,read,"ISO-8859-1")); in.close(); * * return out.toString(); */ }
From source file:Main.java
public static double getDirSize(File file) { //check if file exist or not if (file.exists()) { // recursion to calculate total image size if (file.isDirectory()) { File[] children = file.listFiles(); double size = 0; for (File f : children) size += getDirSize(f);/*from w w w . j a v a 2 s .c o m*/ return size; } else {//if it is file, return the size, "M" unit double size = (double) file.length() / 1024 / 1024; return size; } } else { return 0.0; } }
From source file:com.hs.mail.imap.message.MailMessage.java
public static MailMessage createMailMessage(File file, Date internalDate, Flags flags) throws IOException { MailMessage message = new MailMessage(file); FileInputStream fis = null;/* w w w . ja v a 2 s .c om*/ try { fis = new FileInputStream(file); MessageHeader header = new MessageHeader(fis); message.setPhysMessageID(0); message.setHeader(header); message.setSize(file.length()); message.setInternalDate(internalDate); message.setFlags(flags); return message; } finally { IOUtils.closeQuietly(fis); } }
From source file:com.netsteadfast.greenstep.util.SystemFtpClientUtils.java
private static void logWarnFileSize(File file) throws Exception { long bytes = file.length(); if (bytes > 0 && (bytes / 1024) > MAX_SIZE_KB) { logger.warn("The file: " + file.getPath() + " is bigger then " + MAX_SIZE_KB + " kb size. please change SYS_FTP.TYPE type to " + SystemFtpClientModel.TRAN_GET); }// www . ja v a 2 s .c o m }
From source file:com.barchart.udt.AppServer.java
private static void copyFile(final Socket sock) { log.info("Inside copy file "); final Runnable runner = new Runnable() { public void run() { InputStream is = null; OutputStream os = null; try { is = sock.getInputStream(); final byte[] bytes = new byte[1024]; final int bytesRead = is.read(bytes); final String str = new String(bytes); if (str.startsWith("GET ")) { int nameIndex = 0; for (final byte b : bytes) { if (b == '\n') { break; }// w ww.ja v a 2 s .c om nameIndex++; } // Eat the \n. nameIndex++; final String fileName = new String(bytes, 4, nameIndex).trim(); log.info("File name: " + fileName); final File f = new File(fileName); final FileInputStream fis = new FileInputStream(f); os = sock.getOutputStream(); copy(fis, os, f.length()); os.close(); return; } int nameIndex = 0; int lengthIndex = 0; boolean foundFirst = false; //boolean foundSecond = false; for (final byte b : bytes) { if (!foundFirst) { nameIndex++; } lengthIndex++; if (b == '\n') { if (foundFirst) { break; } foundFirst = true; } } if (nameIndex < 2) { // First bytes was a LF? sock.close(); return; } String dataString = new String(bytes); int fileLengthIndex = dataString.indexOf("\n", nameIndex + 1); final String fileName = new String(bytes, 0, nameIndex).trim(); final String lengthString = dataString.substring(nameIndex, fileLengthIndex); log.info("lengthString " + lengthString); final long length = Long.parseLong(lengthString); final File file = new File(fileName); os = new FileOutputStream(file); final int len = bytesRead - lengthIndex; if (len > 0) { os.write(bytes, lengthIndex, len); } start = System.currentTimeMillis(); copy(is, os, length); } catch (final IOException e) { log.info("Exception reading file...", e); } finally { IOUtils.closeQuietly(is); IOUtils.closeQuietly(os); IOUtils.closeQuietly(sock); } } }; log.info("Executing copy..."); readPool.execute(runner); }
From source file:com.prowidesoftware.swift.utils.Lib.java
/** * Read the content of the given file into a string. * * @param file the file to be read/*from w w w. j a v a 2s .co m*/ * @param encoding encoding to use * @return the file contents or null if file is null or does not exist, or can't be read, or is not a file * @throws IOException if an error occurs during read */ public static String readFile(final File file, final String encoding) throws IOException { if (file == null || !file.exists() || !file.canRead() || !file.isFile()) { return null; } BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), encoding)); final StringBuilder sb = new StringBuilder((int) file.length()); try { int c = 0; while ((c = in.read()) != -1) { sb.append((char) c); } } finally { in.close(); } return sb.toString(); }