List of usage examples for java.io File length
public long length()
From source file:fr.asso.vieillescharrues.parseurs.TelechargeFichier.java
/** * Mthode statique grant le tlechargement de fichiers * @param url Adresse du fichier// ww w . j a v a 2 s . c o m * @param fichierDest Nom du ficher en local */ public static void DownloadFromUrl(URL url, String fichierDest) throws IOException { File file; if (fichierDest.endsWith(".jpg")) file = new File(PATH + "images/", fichierDest); else file = new File(PATH, fichierDest); file.getParentFile().mkdirs(); URLConnection ucon = url.openConnection(); try { tailleDistant = ucon.getHeaderFieldInt("Content-Length", 0); //Rcupre le header HTTP Content-Length tailleLocal = (int) file.length(); } catch (Exception e) { e.printStackTrace(); } // Compare les tailles des fichiers if ((tailleDistant == tailleLocal) && (tailleLocal != 0)) return; InputStream is = ucon.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is); ByteArrayBuffer baf = new ByteArrayBuffer(50); int current = 0; while ((current = bis.read()) != -1) { baf.append((byte) current); } FileOutputStream fos = new FileOutputStream(file); fos.write(baf.toByteArray()); fos.close(); }
From source file:com.asakusafw.bulkloader.testutil.UnitTestUtil.java
public static boolean assertFile(File expected, File actual) throws FileNotFoundException, IOException { byte[] b1 = new byte[(int) expected.length()]; byte[] b2 = new byte[(int) actual.length()]; new FileInputStream(expected).read(b1); new FileInputStream(actual).read(b2); if (b1.length != b2.length) { return false; }//www. ja v a2s . co m for (int i = 0; i < b1.length; i++) { if (b1[i] != b2[i]) { return false; } } return true; }
From source file:illab.nabal.util.Util.java
/** * Extract Base64-encoded string from file content. * /*from w w w . j ava 2 s.com*/ * @param photoPath absoulte system path of photo * @param maxSize max file size in kilobytes * @return Base64-encoded string of file content * @throws Exception */ public static String extractBase64StringFromFile(String photoPath, int maxSize) throws Exception { String imageContent = ""; File file = new File(photoPath); if (file.length() > maxSize * 1024) { throw new SystemException("File size must be less than " + maxSize + " kilobytes."); } byte[] buffer = new byte[(int) file.length()]; InputStream ios = null; try { ios = new FileInputStream(file); if (ios.read(buffer) == -1) { throw new IOException("EOF reached while trying to read the whole file"); } } finally { try { if (ios != null) ios.close(); } catch (IOException e) { } } imageContent = Base64.encodeToString(buffer, Base64.DEFAULT); //Log.d(TAG, "imageContent :\n" + imageContent); return imageContent; }
From source file:Main.java
public static byte[] readContentBytesFromFile(File fileForRead) { if (fileForRead == null) { return null; } else if (fileForRead.exists() && fileForRead.isFile()) { ReentrantReadWriteLock.ReadLock readLock = getLock(fileForRead.getAbsolutePath()).readLock(); readLock.lock();//from w w w . j a v a2 s. c o m Object data = null; BufferedInputStream input = null; try { byte[] data1 = new byte[(int) fileForRead.length()]; int e = 0; input = new BufferedInputStream(new FileInputStream(fileForRead), 8192); while (e < data1.length) { int bytesRemaining = data1.length - e; int bytesRead = input.read(data1, e, bytesRemaining); if (bytesRead > 0) { e += bytesRead; } } byte[] bytesRemaining1 = data1; return bytesRemaining1; } catch (IOException var10) { } finally { closeQuietly(input); readLock.unlock(); } return null; } else { return null; } }
From source file:eu.openanalytics.rsb.component.JobProcessor.java
private static void uploadFileToR(final RServi rServi, final File file, final Set<String> filesUploadedToR) throws FileNotFoundException, CoreException { final FileInputStream fis = new FileInputStream(file); rServi.uploadFile(fis, file.length(), file.getName(), 0, null); IOUtils.closeQuietly(fis);/*from w w w . j ava2s. c o m*/ filesUploadedToR.add(file.getName()); }
From source file:com.dreikraft.axbo.sound.SoundPackageUtil.java
/** * calculate the size of all audio files in this package that will be uploaded * to the aXbo clock.// w w w .j av a 2 s . c om * * @param soundPackage * @return */ public static long calculateSoundFilesSize(SoundPackage soundPackage) { int size = 0; for (Sound sound : soundPackage.getSounds()) { File f = new File(sound.getAxboFile().getPath()); size += (f.length() - WAV_PREAMBEL_LEN); } return size; }
From source file:de.erdesignerng.dialect.msaccess.MSAccessFileFormat.java
private static int findInFile(String aFileName, String aSearchFor) { int theBufferSize = 5242880; // 5MB boolean theSearchOn = true; String theStringBuffer;//from www.j a v a 2s . c o m int theOffset = 0; int theRead = theBufferSize; int thePosition; int theOverhead = aSearchFor.length() - 1; int theResult = -1; if (theBufferSize >= aSearchFor.length()) { try { File file = new File(aFileName); RandomAccessFile ra = new RandomAccessFile(aFileName, "r"); byte[] theByteBuffer = new byte[theBufferSize]; while ((theOffset < file.length()) && (theSearchOn) && (theRead == theBufferSize)) { theRead = ra.read(theByteBuffer); if (theRead >= 0) { theStringBuffer = new String(theByteBuffer, 0, theRead); thePosition = theStringBuffer.indexOf(aSearchFor); if (thePosition >= 0) { theResult = theOffset + thePosition; theSearchOn = false; LOGGER.debug( "Found '" + aSearchFor + "' in '" + aFileName + "' at position " + theResult); } else { if (theRead == theBufferSize) { theOffset += (theRead - theOverhead); ra.seek(theOffset); } } } } ra.close(); } catch (FileNotFoundException ex) { LOGGER.error("Cannot find database file " + aFileName, ex); } catch (IOException ex) { LOGGER.error("Cannot read database file " + aFileName, ex); } } else { throw new RuntimeException("The string to find is too long. Only strings of lenght up to " + theBufferSize + " can be found!"); } return theResult; }
From source file:konditer_reorganized_database.dao.ReorganizedDatabase.java
private static byte[] getBytesFromFile(File file) throws IOException { InputStream is = new FileInputStream(file); long length = file.length(); if (length > Integer.MAX_VALUE) { // File is too large }//from ww w .j ava 2 s.co m byte[] 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; } if (offset < bytes.length) { throw new IOException("Could not completely read file " + file.getName()); } is.close(); return bytes; }
From source file:com.tinspx.util.io.ChannelSourceTest.java
static void testFileSource(File file) throws IOException { System.out.printf("Testing %s; Length: %s%n", file, file.length()); byte[] ref = ByteUtils.toByteArray(file); testFileSource(ref, file, null);/*from w ww .j ava2 s . c o m*/ testFileSource(ref, file, fromRandomAccessFile()); testFileSource(ref, file, fromRandomAccessFile("r")); testFileSource(ref, file, fromRandomAccessFile("rws")); testFileSource(ref, file, fromRandomAccessFile("rwd")); }
From source file:com.bellman.bible.service.common.FileManager.java
public static boolean copyFile(File fromFile, File toFile) { boolean ok = false; try {/*from ww w . j a v a 2s . c o m*/ // don't worry if tofile exists, allow overwrite if (fromFile.exists()) { //ensure the target dir exists or FileNotFoundException is thrown creating dst FileChannel File toDir = toFile.getParentFile(); toDir.mkdir(); long fromFileSize = fromFile.length(); log.debug("Source file length:" + fromFileSize); if (fromFileSize > CommonUtils.getFreeSpace(toDir.getPath())) { // not enough room on SDcard ok = false; } else { // move the file FileInputStream srcStream = new FileInputStream(fromFile); FileChannel src = srcStream.getChannel(); FileOutputStream dstStream = new FileOutputStream(toFile); FileChannel dst = dstStream.getChannel(); try { dst.transferFrom(src, 0, src.size()); ok = true; } finally { src.close(); dst.close(); srcStream.close(); dstStream.close(); } } } else { // fromfile does not exist ok = false; } } catch (Exception e) { log.error("Error moving file to sd card", e); } return ok; }