List of usage examples for java.io FileOutputStream getChannel
public FileChannel getChannel()
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();// w w w . ja v a 2s. c o m outStream.flush(); outStream.close(); }
From source file:Main.java
public static boolean fileCopy(File srcFile, File dstFile) { int length = 1048891; FileChannel inC = null;//w w w . jav a2 s .c o m 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(); } } }
From source file:Main.java
/** * Copies the contents of one file to the other using {@link FileChannel}s. * /*from w w w . j ava 2s.c o 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:z.tool.util.FileUtil.java
/** * ?// www . jav a 2s .co m */ public static void copy(FileInputStream fis, FileOutputStream fos) throws IOException { FileChannel channel = fis.getChannel(); channel.transferTo(0, channel.size(), fos.getChannel()); }
From source file:edu.ucla.cs.scai.swim.qa.ontology.dbpedia.tipicality.DbpediaCsvDownload.java
private static void download(Element e) throws MalformedURLException, IOException { for (Element c : e.children()) { String tagName = c.tag().getName(); if (tagName.equals("small")) { for (Element c1 : c.children()) { if (c1.tag().getName().equals("a") && c1.text().equalsIgnoreCase("csv")) { String href = c1.attr("href"); System.out.println("Downloading " + href); try { URL remoteFile = new URL(href); ReadableByteChannel rbc = Channels.newChannel(remoteFile.openStream()); String[] s = href.split("\\/"); FileOutputStream fos = new FileOutputStream( DBpediaOntology.DBPEDIA_CSV_FOLDER + s[s.length - 1]); fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); } catch (Exception ex) { ex.printStackTrace(); }//from w w w .j a v a 2s . c om } } } else if (tagName.equals("ul")) { for (Element c1 : c.children()) { if (c1.tagName().equals("li")) { download(c1); } } } } }
From source file:Main.java
/** Copia un fichero. * @param source Fichero origen con el contenido que queremos copiar. * @param dest Fichero destino de los datos. * @throws IOException SI ocurre algun problema durante la copia */ public static void copyFile(final File source, final File dest) throws IOException { if (source == null || dest == null) { throw new IllegalArgumentException("Ni origen ni destino de la copia pueden ser nulos"); //$NON-NLS-1$ }/* w ww . j ava 2s . c om*/ final FileInputStream is = new FileInputStream(source); final FileOutputStream os = new FileOutputStream(dest); final FileChannel in = is.getChannel(); final FileChannel out = os.getChannel(); final MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY, 0, in.size()); out.write(buf); in.close(); out.close(); is.close(); os.close(); }
From source file:Main.java
private static void copyFile(File src, File dst) throws IOException { FileInputStream in = new FileInputStream(src); FileOutputStream out = new FileOutputStream(dst); FileChannel fromChannel = null, toChannel = null; try {//from w ww . j a v a2 s. c om fromChannel = in.getChannel(); toChannel = out.getChannel(); fromChannel.transferTo(0, fromChannel.size(), toChannel); } finally { if (fromChannel != null) fromChannel.close(); if (toChannel != null) toChannel.close(); } }
From source file:com.bellman.bible.service.common.FileManager.java
public static boolean copyFile(File fromFile, File toFile) { boolean ok = false; try {//from w ww . j av a 2 s. c om // 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; }
From source file:Main.java
public static boolean copyFile(File sourceFile, File destFile) throws IOException { if (!destFile.exists()) { destFile.createNewFile();/*from w ww. j a va 2 s .c o m*/ } FileInputStream source = null; FileOutputStream destination = null; try { source = new FileInputStream(sourceFile); destination = new FileOutputStream(destFile); destination.getChannel().transferFrom(source.getChannel(), 0, source.getChannel().size()); } catch (Exception e) { // FileLog.e("tmessages", e); return false; } finally { if (source != null) { source.close(); } if (destination != null) { destination.close(); } } return true; }
From source file:examples.utils.CifarReader.java
public static void downloadAndExtract() { if (new File("data", TEST_DATA_FILE).exists() == false) { try {//w ww . jav a 2 s . c o m if (new File("data", ARCHIVE_BINARY_FILE).exists() == false) { URL website = new URL("http://www.cs.toronto.edu/~kriz/" + ARCHIVE_BINARY_FILE); FileOutputStream fos = new FileOutputStream("data/" + ARCHIVE_BINARY_FILE); fos.getChannel().transferFrom(Channels.newChannel(website.openStream()), 0, Long.MAX_VALUE); fos.close(); } TarArchiveInputStream tar = new TarArchiveInputStream( new GZIPInputStream(new FileInputStream("data/" + ARCHIVE_BINARY_FILE))); TarArchiveEntry entry = null; while ((entry = tar.getNextTarEntry()) != null) { if (entry.isDirectory()) { new File("data", entry.getName()).mkdirs(); } else { byte data[] = new byte[2048]; int count; BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream(new File("data/", entry.getName())), 2048); while ((count = tar.read(data, 0, 2048)) != -1) { bos.write(data, 0, count); } bos.close(); } } tar.close(); } catch (IOException e) { e.printStackTrace(); } } }