List of usage examples for java.io FileInputStream read
public int read(byte b[], int off, int len) throws IOException
len
bytes of data from this input stream into an array of bytes. From source file:com.clustercontrol.jobmanagement.util.JobmapIconImageUtil.java
/** * ?/*from ww w . j a v a 2 s .com*/ * @param filepath * @throws Exception */ private static byte[] getFileData(String filepath) throws Exception { File file = new File(filepath); String filename = file.getName(); int filesize = (int) file.length(); if (filesize > MAX_FILESIZE) { m_log.warn("getFileData(), file size is too large"); throw new Exception(Messages.getString("message.job.143")); } byte[] filedata = null; /* * */ FileInputStream stream = null; try { stream = new FileInputStream(filepath); filedata = new byte[filesize]; int readsize = stream.read(filedata, 0, filesize); m_log.debug("UploadImage readsize = " + readsize + ", filesize = " + filesize); m_log.debug("path=" + filepath + ", name=" + filename); } catch (FileNotFoundException e) { m_log.warn("getFileData(), " + e.getMessage(), e); throw new Exception(Messages.getString("file.not.found"), e); } catch (Exception e) { m_log.warn("getFileData(), " + e.getMessage(), e); throw e; } finally { if (stream != null) { try { stream.close(); } catch (IOException e) { m_log.warn("getFileData(), " + e.getMessage(), e); } stream = null; } } return filedata; }
From source file:Main.java
public static void copyFile(File src, File dest) { if (null == src || null == dest) { return;//from www . j av a2 s.c o m } FileInputStream in = null; FileOutputStream out = null; try { in = new FileInputStream(src); out = new FileOutputStream(dest); int byteCount = 8096; byte[] buffer = new byte[byteCount]; int count = 0; while ((count = in.read(buffer, 0, byteCount)) != -1) { out.write(buffer, 0, count); } out.flush(); } catch (IOException e) { e.printStackTrace(); } finally { try { in.close(); out.close(); } catch (IOException e) { e.printStackTrace(); } } }
From source file:CompressTransfer.java
public static void transfer(FileInputStream srcIs, FileOutputStream destOut) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); BZip2CompressorOutputStream gos = new BZip2CompressorOutputStream(destOut); int count;/*from www. java2s . c o m*/ byte data[] = new byte[BUFFER]; while ((count = srcIs.read(data, 0, BUFFER)) != -1) { //out.write(data, 0, count); gos.write(data, 0, count); } gos.flush(); gos.close(); }
From source file:Main.java
public static boolean copyFile(String quelle, String ziel) { if ((new File(quelle)).equals(new File(ziel))) { return false; // Quelle und Ziel sind dieselbe Datei! }//ww w. ja v a2 s.c o m final int BUFSIZE = 4096; FileInputStream f1; FileOutputStream f2; byte[] buf = new byte[BUFSIZE]; int n; if (!canOpenFile(quelle)) { return false; } try { f1 = new FileInputStream(quelle); f2 = new FileOutputStream(ziel); while ((n = f1.read(buf, 0, BUFSIZE)) > 0) { f2.write(buf, 0, n); } f1.close(); f2.close(); } catch (IOException e) { return false; } return true; }
From source file:Main.java
public static void toTargz(String srcFile, String targetFile) throws IOException { File sourceFile = new File(srcFile); File target = new File(targetFile); FileInputStream in = null; GZIPOutputStream gout = null; try {/* w ww. ja v a2 s . c o m*/ in = new FileInputStream(sourceFile); gout = new GZIPOutputStream(new FileOutputStream(target)); byte[] array = new byte[BUFFER_LEN]; int number = -1; while ((number = in.read(array, 0, array.length)) != -1) { gout.write(array, 0, number); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } if (gout != null) { try { gout.close(); } catch (IOException e) { e.printStackTrace(); } } } }
From source file:ai.h2o.servicebuilder.Util.java
/** * Create jar archive out of files list. Names in archive have paths starting from relativeToDir * * @param tobeJared list of files/* w w w . j a v a 2 s.c o m*/ * @param relativeToDir starting directory for paths * @return jar as byte array * @throws IOException */ public static byte[] createJarArchiveByteArray(File[] tobeJared, String relativeToDir) throws IOException { int BUFFER_SIZE = 10240; byte buffer[] = new byte[BUFFER_SIZE]; ByteArrayOutputStream stream = new ByteArrayOutputStream(); JarOutputStream out = new JarOutputStream(stream, new Manifest()); for (File t : tobeJared) { if (t == null || !t.exists() || t.isDirectory()) { if (t != null && !t.isDirectory()) logger.error("Can't add to jar {}", t); continue; } // Create jar entry String filename = t.getPath().replace(relativeToDir, "").replace("\\", "/"); // if (filename.endsWith("MANIFEST.MF")) { // skip to avoid duplicates // continue; // } JarEntry jarAdd = new JarEntry(filename); jarAdd.setTime(t.lastModified()); out.putNextEntry(jarAdd); // Write file to archive FileInputStream in = new FileInputStream(t); while (true) { int nRead = in.read(buffer, 0, buffer.length); if (nRead <= 0) break; out.write(buffer, 0, nRead); } in.close(); } out.close(); stream.close(); return stream.toByteArray(); }
From source file:org.meshpoint.anode.util.ModuleUtils.java
public static boolean copyFile(File src, File dest) { boolean result = true; if (src.isDirectory()) { result = copyDir(src, dest);//ww w . j av a 2 s .c o m } else { try { int count; byte[] buf = new byte[1024]; FileInputStream fis = new FileInputStream(src); FileOutputStream fos = new FileOutputStream(dest); while ((count = fis.read(buf, 0, 1024)) != -1) fos.write(buf, 0, count); } catch (IOException e) { Log.v(TAG, "moveFile exception: aborting; exception: " + e + "; src = " + src.toString() + "; dest = " + dest.toString()); return false; } } return result; }
From source file:de.xwic.appkit.core.util.ZipUtil.java
/** * Zips the files array into a file that has the name given as parameter. * /* w ww . ja va2 s . c o m*/ * @param files * the files array * @param zipFileName * the name for the zip file * @return the new zipped file * @throws IOException */ public static File zip(File[] files, String zipFileName) throws IOException { FileOutputStream stream = null; ZipOutputStream out = null; File archiveFile = null; try { if (!zipFileName.endsWith(".zip")) { zipFileName = zipFileName + ".zip"; } archiveFile = new File(zipFileName); byte buffer[] = new byte[BUFFER_SIZE]; // Open archive file stream = new FileOutputStream(archiveFile); out = new ZipOutputStream(stream); for (int i = 0; i < files.length; i++) { if (null == files[i] || !files[i].exists() || files[i].isDirectory()) { continue; } log.info("Zipping " + files[i].getName()); // Add archive entry ZipEntry zipAdd = new ZipEntry(files[i].getName()); zipAdd.setTime(files[i].lastModified()); out.putNextEntry(zipAdd); // Read input & write to output FileInputStream in = new FileInputStream(files[i]); while (true) { int nRead = in.read(buffer, 0, buffer.length); if (nRead <= 0) { break; } out.write(buffer, 0, nRead); } in.close(); } } catch (IOException e) { log.error("Error: " + e.getMessage(), e); throw e; } finally { try { if (null != out) { out.close(); } if (null != stream) { stream.close(); } } catch (IOException e) { log.error("Error: " + e.getMessage(), e); throw e; } } return archiveFile; }
From source file:com.feilong.controller.DownloadController.java
/** * ???/*from ww w . java 2s . co m*/ * * @param filePath * * @return ? */ public static String getFileHeader(String filePath) { FileInputStream is = null; String value = null; try { is = new FileInputStream(filePath); byte[] b = new byte[4]; is.read(b, 0, b.length); value = bytesToHexString(b); } catch (Exception e) { } finally { if (null != is) { try { is.close(); } catch (IOException e) { } } } return value; }
From source file:Main.java
static String readStringFromFile(File theFile) { String myReturnString;/* w w w. j a va 2 s .c o m*/ if (theFile == null) { myReturnString = "No File was opened"; } else { try { FileInputStream in = new FileInputStream(theFile); int size = (int) theFile.length(); byte[] data = new byte[size]; /* let's read in the file (which may not all arrive in one chomp...)*/; int bytes_read = 0; while (bytes_read < size) { bytes_read += in.read(data, bytes_read, size - bytes_read); } ; myReturnString = new String(data); // used to be new String( data, 0 ); } catch (Exception exc) { myReturnString = "Error opening file:" + exc; } ; } ; return myReturnString; }