List of usage examples for java.io File length
public long length()
From source file:Main.java
public static String getFileMD5String(File file) throws IOException { FileInputStream in = new FileInputStream(file); FileChannel ch = in.getChannel(); MappedByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, 0, file.length()); messagedigest.update(byteBuffer);/*from w ww. ja v a 2s . c om*/ return bufferToHex(messagedigest.digest()); }
From source file:FileTableHTML.java
public static String makeHTMLTable(String dirname) { // Look up the contents of the directory File dir = new File(dirname); String[] entries = dir.list(); // Set up an output stream we can print the table to. // This is easier than concatenating strings all the time. StringWriter sout = new StringWriter(); PrintWriter out = new PrintWriter(sout); // Print the directory name as the page title out.println("<H1>" + dirname + "</H1>"); // Print an "up" link, unless we're already at the root String parent = dir.getParent(); if ((parent != null) && (parent.length() > 0)) out.println("<A HREF=\"" + parent + "\">Up to parent directory</A><P>"); // Print out the table out.print("<TABLE BORDER=2 WIDTH=600><TR>"); out.print("<TH>Name</TH><TH>Size</TH><TH>Modified</TH>"); out.println("<TH>Readable?</TH><TH>Writable?</TH></TR>"); for (int i = 0; i < entries.length; i++) { File f = new File(dir, entries[i]); out.println("<TR><TD>" + (f.isDirectory() ? "<a href=\"" + f + "\">" + entries[i] + "</a>" : entries[i]) + "</TD><TD>" + f.length() + "</TD><TD>" + new Date(f.lastModified()) + "</TD><TD align=center>" + (f.canRead() ? "x" : " ") + "</TD><TD align=center>" + (f.canWrite() ? "x" : " ") + "</TD></TR>"); }//from w w w. jav a 2 s. c o m out.println("</TABLE>"); out.close(); // Get the string of HTML from the StringWriter and return it. return sout.toString(); }
From source file:documentToVector.spotlight.evaluation.external.AnnotationClient.java
protected static String readFileAsString(File file) throws IOException { byte[] buffer = new byte[(int) file.length()]; BufferedInputStream f = new BufferedInputStream(new FileInputStream(file)); f.read(buffer);/*from w ww . j a va2s . co m*/ f.close(); return new String(buffer); }
From source file:Main.java
public static Document addXMLItems(File xmlfile, String tag) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc;// w w w. ja v a 2 s.co m if (xmlfile.length() == 0) { doc = builder.newDocument(); Element rootElement = doc.createElement(tag + "s"); doc.appendChild(rootElement); } else { doc = builder.parse(xmlfile); } return doc; }
From source file:org.phpmaven.pear.impl.Helper.java
/** * Returns the binary file contents./* w w w . ja v a 2 s . co m*/ * @param uri URI of the resource. * @return the files content. * @throws IOException thrown on errors. */ public static byte[] getBinaryFileContents(String uri) throws IOException { // is it inside the local filesystem? if (uri.startsWith("file://")) { final File channelFile = new File(uri.substring(7)); final byte[] result = new byte[(int) channelFile.length()]; final FileInputStream fis = new FileInputStream(channelFile); fis.read(result); return result; } // try http connection final HttpClient client = new DefaultHttpClient(); final HttpGet httpget = new HttpGet(uri); final HttpResponse response = client.execute(httpget); if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { throw new IOException("Invalid http status: " + response.getStatusLine().getStatusCode() + " / " + response.getStatusLine().getReasonPhrase()); } final HttpEntity entity = response.getEntity(); if (entity == null) { throw new IOException("Empty response."); } return EntityUtils.toByteArray(entity); }
From source file:au.org.ala.layers.util.BatchProducer.java
private static int getFieldCount(String batchPath, String batchId) throws IOException { int count = 0; String dir = batchPath + File.separator + batchId + File.separator; if (new File(dir).exists()) { File f = new File(dir + "fids.txt"); if (f.exists() && f.length() > 0) { count = FileUtils.readFileToString(new File(dir + "fids.txt")).split(",").length; }/*w ww . ja v a 2 s.co m*/ } return count; }
From source file:mysynopsis.ImageProcess.java
public static String toImageString(String path) throws FileNotFoundException, IOException { String imageString = ""; try {/*from w w w . ja v a 2s.co m*/ File f = new File(path); try (FileInputStream fis = new FileInputStream(f)) { byte byteArray[] = new byte[(int) f.length()]; fis.read(byteArray); imageString = Base64.encodeBase64String(byteArray); fis.close(); } } catch (IOException iOException) { return ""; } return imageString; }
From source file:au.org.ala.layers.util.BatchProducer.java
private static int getPointCount(String batchPath, String batchId) throws IOException { int count = 0; String dir = batchPath + File.separator + batchId + File.separator; if (new File(dir).exists()) { File f = new File(dir + "points.txt"); if (f.exists() && f.length() > 0) { count = (StringUtils.countOccurrencesOf(FileUtils.readFileToString(f), ",") + 1) / 2; }//from w w w . j av a2 s . c o m } return count; }
From source file:at.tuwien.minimee.emulation.EmulationService.java
/** * utility method to read a bytestream from a file * @param file//from w w w. j a va 2 s.c o m * @return * @throws IOException */ public static byte[] getBytesFromFile(File file) throws IOException { InputStream is = new FileInputStream(file); // Get the size of the file long length = file.length(); if (length > Integer.MAX_VALUE) { // File is too large } // Create the byte array to hold the data byte[] 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) { throw new IOException("Could not completely read file " + file.getName()); } // Close the input stream and return bytes is.close(); return bytes; }
From source file:com.bigdata.dastor.streaming.StreamOut.java
/** * Transfers a group of sstables from a single table to the target endpoint * and then marks them as ready for local deletion. *///from w w w. j a v a 2 s . c o m public static void transferSSTables(InetAddress target, List<SSTableReader> sstables, String table) throws IOException { PendingFile[] pendingFiles = new PendingFile[SSTable.FILES_ON_DISK * sstables.size()]; int i = 0; for (SSTableReader sstable : sstables) { for (String filename : sstable.getAllFilenames()) { File file = new File(filename); pendingFiles[i++] = new PendingFile(file.getAbsolutePath(), file.length(), table); } } logger.info("Stream context metadata " + StringUtils.join(pendingFiles, ", " + " " + sstables.size() + " sstables.")); StreamOutManager.get(target).addFilesToStream(pendingFiles); StreamInitiateMessage biMessage = new StreamInitiateMessage(pendingFiles); Message message = StreamInitiateMessage.makeStreamInitiateMessage(biMessage); message.setHeader(StreamOut.TABLE_NAME, table.getBytes()); logger.info("Sending a stream initiate message to " + target + " ..."); MessagingService.instance.sendOneWay(message, target); if (pendingFiles.length > 0) { logger.info("Waiting for transfer to " + target + " to complete"); StreamOutManager.get(target).waitForStreamCompletion(); // todo: it would be good if there were a dafe way to remove the StreamManager for target. // (StreamManager will delete the streamed file on completion.) logger.info("Done with transfer to " + target); } }