List of usage examples for java.io FileInputStream getChannel
public FileChannel getChannel()
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 www. j a va 2 s .c o m inChannel.transferTo(0, inChannel.size(), outChannel); } finally { if (inChannel != null) { inChannel.close(); } outChannel.close(); } in.close(); out.close(); }
From source file:com.viddu.handlebars.HandlebarsFileUtil.java
/** * Utility method to get contents of the File. * /* w w w . j av a2 s. c o m*/ * @param inputFile * @return * @throws IOException */ public static String getFileContents(File inputFile) throws IOException { FileInputStream stream = new FileInputStream(inputFile); try { FileChannel fc = stream.getChannel(); MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); return Charset.defaultCharset().decode(bb).toString(); } finally { stream.close(); } }
From source file:edu.kit.cockpit.valuationserver.sfmpersistency.FileUtil.java
/** * @param file//www. ja v a 2 s. c om * @return string representation of File * @throws IOException */ public static String readFile(File file) throws IOException { FileInputStream stream = new FileInputStream(file); try { FileChannel fc = stream.getChannel(); 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:Main.java
public static void copy(File src, File dst) throws IOException { FileInputStream inStream = new FileInputStream(src); FileOutputStream outStream = new FileOutputStream(dst); FileChannel inChannel = inStream.getChannel(); FileChannel outChannel = outStream.getChannel(); inChannel.transferTo(0, inChannel.size(), outChannel); inStream.close();//from w ww . j a v a 2 s .c o m outStream.flush(); outStream.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 {//from w w w. ja va2s . 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:Main.java
/** * Copies the contents of one file to the other using {@link FileChannel}s. * //from w w w . j a v a 2 s. co m * @param src * source {@link File} * @param dst * destination {@link File} */ 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 { inChannel.transferTo(0, inChannel.size(), outChannel); } finally { if (inChannel != null) { inChannel.close(); } if (outChannel != null) { outChannel.close(); } } in.close(); out.close(); }
From source file:ja.lingo.engine.util.EngineFiles.java
private static void appendFile(String fileName, FileOutputStream fos, boolean prependWithLength) throws IOException { FileInputStream fis = new FileInputStream(fileName); FileChannel fic = fis.getChannel(); FileChannel foc = fos.getChannel(); ByteBuffer buffer = ByteBuffer.allocate(COPY_BUFFER_SIZE); // put header: length (1 int = 4 bytes) if (prependWithLength) { buffer.putInt((int) new File(fileName).length()); }//from www . ja va 2 s.c o m // put body do { buffer.flip(); foc.write(buffer); buffer.clear(); } while (fic.read(buffer) != -1); fic.close(); // NOTE: do not close 'foc' Files.delete(fileName); }
From source file:Main.java
public static void copyFile(File src, File dest) throws IOException { FileOutputStream fileOut = null; FileInputStream fileIn = null; try {/*ww w.j a va2 s . c om*/ fileOut = new FileOutputStream(dest); fileIn = new FileInputStream(src); FileChannel inChannel = fileIn.getChannel(); FileChannel outChannel = fileOut.getChannel(); inChannel.transferTo(0, inChannel.size(), outChannel); } finally { if (fileIn != null) { fileIn.close(); } if (fileOut != null) { fileOut.close(); } } }
From source file:Main.java
public static void fastFileCopy(File source, File target) { FileChannel in = null;//from ww w. j a va 2s .c om FileChannel out = null; long start = System.currentTimeMillis(); FileInputStream fis = null; FileOutputStream fos = null; try { fis = new FileInputStream(source); in = fis.getChannel(); fos = new FileOutputStream(target); out = fos.getChannel(); long size = in.size(); long transferred = in.transferTo(0, size, out); while (transferred != size) { transferred += in.transferTo(transferred, size - transferred, out); } } catch (IOException e) { e.printStackTrace(); } finally { close(fis); close(fos); close(in); close(out); } long end = System.currentTimeMillis(); Log.d(target.getAbsolutePath(), nf.format(source.length()) + "B: " + ((end - start > 0) ? nf.format(source.length() / (end - start)) : 0) + " KB/s"); }
From source file:Main.java
public static boolean fileCopy(File srcFile, File dstFile) { int length = 1048891; FileChannel inC = null;//w ww . j a v a 2s . c om FileChannel outC = null; try { FileInputStream in = new FileInputStream(srcFile); FileOutputStream out = new FileOutputStream(dstFile); inC = in.getChannel(); outC = out.getChannel(); ByteBuffer b = null; while (inC.position() < inC.size()) { if ((inC.size() - inC.position()) < length) { length = (int) (inC.size() - inC.position()); } else { length = 1048891; } b = ByteBuffer.allocateDirect(length); inC.read(b); b.flip(); outC.write(b); outC.force(false); } return true; } catch (IOException e) { e.printStackTrace(); return false; } finally { try { if (inC != null && inC.isOpen()) { inC.close(); } if (outC != null && outC.isOpen()) { outC.close(); } } catch (IOException e) { e.printStackTrace(); } } }