List of usage examples for java.io FileInputStream close
public void close() throws IOException
From source file:Main.java
public static String readXmlAsString(File input) throws IOException { String xmlString = ""; if (input == null) { throw new IOException("The input stream object is null."); }//from w w w . j a va2 s.c o m FileInputStream fileInputStream = new FileInputStream(input); InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String line = bufferedReader.readLine(); while (line != null) { xmlString += line + "\n"; line = bufferedReader.readLine(); } fileInputStream.close(); fileInputStream.close(); bufferedReader.close(); return xmlString; }
From source file:Compress.java
/** Zip the contents of the directory, and save it in the zipfile */ public static void zipDirectory(String dir, String zipfile) throws IOException, IllegalArgumentException { // Check that the directory is a directory, and get its contents File d = new File(dir); if (!d.isDirectory()) throw new IllegalArgumentException("Compress: not a directory: " + dir); String[] entries = d.list();/*from w w w . ja va 2 s .co m*/ byte[] buffer = new byte[4096]; // Create a buffer for copying int bytes_read; // Create a stream to compress data and write it to the zipfile ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile)); // Loop through all entries in the directory for (int i = 0; i < entries.length; i++) { File f = new File(d, entries[i]); if (f.isDirectory()) continue; // Don't zip sub-directories FileInputStream in = new FileInputStream(f); // Stream to read file ZipEntry entry = new ZipEntry(f.getPath()); // Make a ZipEntry out.putNextEntry(entry); // Store entry while ((bytes_read = in.read(buffer)) != -1) // Copy bytes out.write(buffer, 0, bytes_read); in.close(); // Close input stream } // When we're done with the whole loop, close the output stream out.close(); }
From source file:Main.java
public static Bitmap getBitmapByPath(String filePath, BitmapFactory.Options opts) { FileInputStream fis = null; Bitmap bitmap = null;/*from w w w .j a v a 2s . c om*/ try { File file = new File(filePath); fis = new FileInputStream(file); bitmap = BitmapFactory.decodeStream(fis, null, opts); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (OutOfMemoryError e) { e.printStackTrace(); } finally { try { fis.close(); } catch (Exception e) { } } return bitmap; }
From source file:Main.java
public static String read(File file) throws IOException { FileInputStream in = null; try {//from w w w. j a v a 2 s . c o m in = new FileInputStream(file); return read(in); } finally { try { if (in != null) in.close(); } catch (Throwable t) { } } }
From source file:de.helmholtz_muenchen.ibis.utils.ngs.samsplitter.helpers.FileHelpers.java
/** * Returns md5 sum for a given file/*from ww w. ja va2 s .c o m*/ * @param file * @return */ public static String getmd5Sum(String file) { FileInputStream fis; try { fis = new FileInputStream(new File(file)); String md5 = org.apache.commons.codec.digest.DigestUtils.md5Hex(fis); fis.close(); return md5; } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return ""; }
From source file:org.openmrs.module.omodexport.util.OmodExportUtil.java
/** * Utility method for zipping a list of files * //from w w w. jav a2 s .co m * @param files -The list of files to zip * @param out * @throws FileNotFoundException * @throws IOException */ public static void zipFiles(List<File> files, ZipOutputStream out) throws FileNotFoundException, IOException { byte[] buffer = new byte[4096]; // Create a buffer for copying int bytesRead; for (File f : files) { if (f.isDirectory()) continue;// Ignore directory FileInputStream in = new FileInputStream(f); // Stream to read file out.putNextEntry(new ZipEntry(f.getName())); // Store entry while ((bytesRead = in.read(buffer)) != -1) out.write(buffer, 0, bytesRead); in.close(); } out.close(); }
From source file:fm.moe.android.util.JSONFileHelper.java
private static String readFile(final File file) throws IOException { final FileInputStream stream = new FileInputStream(file); try {/*w w w .j a v a2 s . c o m*/ final FileChannel fc = stream.getChannel(); final MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); /* Instead of using default, pass in a decoder. */ return Charset.defaultCharset().decode(bb).toString(); } finally { stream.close(); } }
From source file:it.geosolutions.imageio.plugins.nitronitf.NITFImageWriterSpi.java
public static boolean isNITF(File file) { FileInputStream fin = null; try {/* w w w. j a va 2 s .com*/ fin = new FileInputStream(file); byte[] firstFour = new byte[4]; fin.read(firstFour); return new String(firstFour).equals("NITF"); } catch (IOException e) { return false; } finally { try { if (fin != null) fin.close(); } catch (IOException e) { log.error(ExceptionUtils.getStackTrace(e)); } } }
From source file:Main.java
/** * Reads the given file, translating {@link IOException} to a * {@link RuntimeException} of some sort. * * @param file {@code non-null;} the file to read * @return {@code non-null;} contents of the file *///from w w w .j ava2 s . c o m public static byte[] readFile(File file) { if (!file.exists()) { throw new RuntimeException(file + ": file not found"); } if (!file.isFile()) { throw new RuntimeException(file + ": not a file"); } if (!file.canRead()) { throw new RuntimeException(file + ": file not readable"); } long longLength = file.length(); int length = (int) longLength; if (length != longLength) { throw new RuntimeException(file + ": file too long"); } byte[] result = new byte[length]; try { FileInputStream in = new FileInputStream(file); int at = 0; while (length > 0) { int amt = in.read(result, at, length); if (amt == -1) { throw new RuntimeException(file + ": unexpected EOF"); } at += amt; length -= amt; } in.close(); } catch (IOException ex) { throw new RuntimeException(file + ": trouble reading", ex); } return result; }
From source file:Main.java
public static void copyFile(File src, File dst) throws IOException { FileInputStream in = new FileInputStream(src); FileOutputStream out = new FileOutputStream(dst); FileChannel inChannel = in.getChannel(); FileChannel outChannel = out.getChannel(); try {//from w w w . j a v a 2 s. c o m inChannel.transferTo(0, inChannel.size(), outChannel); } finally { if (inChannel != null) { inChannel.close(); } outChannel.close(); } in.close(); out.close(); }