URL website = new URL("http://www.website.com/information.asp"); ReadableByteChannel rbc = Channels.newChannel(website.openStream()); FileOutputStream fos = new FileOutputStream("information.html"); fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
public static void copyFile( File from, File to ) throws IOException { if ( !to.exists() ) { to.createNewFile(); } try ( FileChannel in = new FileInputStream( from ).getChannel(); FileChannel out = new FileOutputStream( to ).getChannel() ) { out.transferFrom( in, 0, in.size() ); } }
public static void copyFile(File source, File dest) throws IOException { try (FileChannel in = new FileInputStream(source).getChannel(); FileChannel out = new FileOutputStream(dest).getChannel()) { out.transferFrom(in, 0, in.size()); } }
/** * Downloads resource to a file, potentially very efficiently. */ public static void downloadFile(String url, File file) throws IOException { InputStream inputStream = new URL(url).openStream(); ReadableByteChannel rbc = Channels.newChannel(inputStream); FileOutputStream fos = new FileOutputStream(file); fos.getChannel().transferFrom(rbc, 0, 1 << 24); } }
public static void copyFile(File source, File dest) throws IOException { try (FileChannel in = new FileInputStream(source).getChannel(); FileChannel out = new FileOutputStream(dest).getChannel()) { out.transferFrom(in, 0, in.size()); } }
public static void writeToFile(byte[] data, File target) throws IOException { FileOutputStream fo = null; ReadableByteChannel src = null; FileChannel out = null; try { src = Channels.newChannel(new ByteArrayInputStream(data)); fo = new FileOutputStream(target); out = fo.getChannel(); out.transferFrom(src, 0, data.length); } finally { if (fo != null) { fo.close(); } if (src != null) { src.close(); } if (out != null) { out.close(); } } }
public static void copyFileUsingFileChannels(File source, File dest) throws IOException { FileChannel inputChannel = null; FileChannel outputChannel = null; try { inputChannel = new FileInputStream(source).getChannel(); outputChannel = new FileOutputStream(dest).getChannel(); outputChannel.transferFrom(inputChannel, 0, inputChannel.size()); } finally { assert inputChannel != null; inputChannel.close(); assert outputChannel != null; outputChannel.close(); } }
private File downloadZipFile() throws IOException { // Check if we already have a local copy of the emulator and download it if not. File zipFile = new File(System.getProperty("java.io.tmpdir"), fileName); if (!zipFile.exists() || (md5CheckSum != null && !md5CheckSum.equals(md5(zipFile)))) { if (log.isLoggable(Level.FINE)) { log.fine("Fetching emulator"); } ReadableByteChannel rbc = Channels.newChannel(downloadUrl.openStream()); try (FileOutputStream fos = new FileOutputStream(zipFile)) { fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); } } else { if (log.isLoggable(Level.FINE)) { log.fine("Using cached emulator"); } } return zipFile; }
FileChannel src = new FileInputStream(file1).getChannel(); FileChannel dest = new FileOutputStream(file2).getChannel(); dest.transferFrom(src, 0, src.size());
private void downloadFile(String fileUrl, Path destination) { if (destination.toFile().exists()) { Log.info(String.format("Skipping download for %s at %s because destination already exists", fileUrl, destination.toString())); return; } try { URL archiveUrl = new URL(fileUrl); ReadableByteChannel rbc = Channels.newChannel(archiveUrl.openStream()); FileOutputStream fos = new FileOutputStream(String.valueOf(destination)); Log.info(String.format("Downloading %s at %s", fileUrl, destination.toString())); fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); Log.info(String.format("Download complete for %s at %s", fileUrl, destination.toString())); } catch (IOException e) { e.printStackTrace(); } } }
public static void copyFile(File sourceFile, File destFile) throws IOException { if(!destFile.exists()) { destFile.createNewFile(); } FileChannel source = null; FileChannel destination = null; try { source = new FileInputStream(sourceFile).getChannel(); destination = new FileOutputStream(destFile).getChannel(); destination.transferFrom(source, 0, source.size()); } finally { if(source != null) { source.close(); } if(destination != null) { destination.close(); } } }
/** * @param igniteUrl Url to ignite. * @return Ignite file name. */ private String downloadIgnite(String igniteUrl) { try { URL url = new URL(igniteUrl); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); int code = conn.getResponseCode(); if (code == 200) { String fileName = fileName(url.toString()); String filePath = props.igniteLocalWorkDir() + File.separator + fileName; if (new File(filePath).exists()) return fileName; FileOutputStream outFile = new FileOutputStream(filePath); outFile.getChannel().transferFrom(Channels.newChannel(conn.getInputStream()), 0, Long.MAX_VALUE); outFile.close(); return fileName; } else throw new RuntimeException("Got unexpected response code. Response code: " + code); } catch (IOException e) { throw new RuntimeException("Failed update ignite.", e); } }
public static void copyFile(File src, File dest) throws IOException { if (!dest.exists()) { dest.createNewFile(); } FileChannel source = null; FileChannel destination = null; try { source = new FileInputStream(src).getChannel(); destination = new FileOutputStream(dest).getChannel(); destination.transferFrom(source, 0, source.size()); } finally { if (source != null) { source.close(); } if (destination != null) { destination.close(); } } }
/** * Downloads ignite by URL if this version wasn't downloaded before. * * @param url URL to Ignite. * @return File name. */ private String downloadIgnite(URL url) { assert url != null; try { HttpURLConnection conn = (HttpURLConnection)url.openConnection(); int code = conn.getResponseCode(); if (code == 200) { checkDownloadFolder(); String fileName = fileName(url.toString()); if (fileExist(fileName)) return fileName; log.log(Level.INFO, "Downloading from {0}", url.toString()); FileOutputStream outFile = new FileOutputStream(Paths.get(downloadFolder, fileName).toFile()); outFile.getChannel().transferFrom(Channels.newChannel(conn.getInputStream()), 0, Long.MAX_VALUE); outFile.close(); return fileName; } else throw new RuntimeException("Got unexpected response code. Response code: " + code + " from " + url); } catch (IOException e) { throw new RuntimeException("Failed to download Ignite.", e); } }
try { File sd = Environment.getExternalStorageDirectory(); File data = Environment.getDataDirectory(); if (sd.canWrite()) { String currentDBPath = "//data//{package name}//databases//{database name}"; String backupDBPath = "{database name}"; File currentDB = new File(data, currentDBPath); File backupDB = new File(sd, backupDBPath); if (currentDB.exists()) { FileChannel src = new FileInputStream(currentDB).getChannel(); FileChannel dst = new FileOutputStream(backupDB).getChannel(); dst.transferFrom(src, 0, src.size()); src.close(); dst.close(); } } } catch (Exception e) { }
@Override public void download(String downloadUrl, String destination, String userName, String password) throws DownloadException { // force tls to 1.2 since github removed weak cryptographic standards // https://blog.github.com/2018-02-02-weak-cryptographic-standards-removal-notice/ System.setProperty("https.protocols", "TLSv1.2"); String fixedDownloadUrl = downloadUrl; try { fixedDownloadUrl = FilenameUtils.separatorsToUnix(fixedDownloadUrl); URI downloadURI = new URI(fixedDownloadUrl); if ("file".equalsIgnoreCase(downloadURI.getScheme())) { FileUtils.copyFile(new File(downloadURI), new File(destination)); } else { CloseableHttpResponse response = execute(fixedDownloadUrl, userName, password); int statusCode = response.getStatusLine().getStatusCode(); if(statusCode != 200){ throw new DownloadException("Got error code "+ statusCode +" from the server."); } new File(FilenameUtils.getFullPathNoEndSeparator(destination)).mkdirs(); ReadableByteChannel rbc = Channels.newChannel(response.getEntity().getContent()); FileOutputStream fos = new FileOutputStream(destination); fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); fos.close(); } } catch (IOException | URISyntaxException e) { throw new DownloadException("Could not download " + fixedDownloadUrl, e); } }
try (FileInputStream fis = new FileInputStream(srcFile); FileChannel input = fis.getChannel(); FileOutputStream fos = new FileOutputStream(destFile); FileChannel output = fos.getChannel()) { final long size = input.size(); // TODO See IO-386 long pos = 0; long count = 0; final long remain = size - pos; count = remain > FILE_COPY_BUFFER_SIZE ? FILE_COPY_BUFFER_SIZE : remain; final long bytesCopied = output.transferFrom(input, pos, count); if (bytesCopied == 0) { // IO-385 - can happen if file is truncated after caching the size break; // ensure we don't loop forever
FileOutputStream outFile = new FileOutputStream(props.igniteLocalWorkDir() + File.separator + fileName(redirectUrl)); outFile.getChannel().transferFrom(Channels.newChannel(conn.getInputStream()), 0, Long.MAX_VALUE);
public static void copy(File inputFile, File outputFile) throws IOException { if (inputFile != null && outputFile != null) { FileChannel sourceChannel = null; FileChannel destChannel = null; try { sourceChannel = new FileInputStream(inputFile).getChannel(); destChannel = new FileOutputStream(outputFile).getChannel(); destChannel.transferFrom(sourceChannel, 0, sourceChannel.size()); } finally { if (sourceChannel != null) { sourceChannel.close(); } if (destChannel != null) { destChannel.close(); } } } } }
URL website = new URL(url); ReadableByteChannel rbc = Channels.newChannel(website.openStream()); FileOutputStream fos = new FileOutputStream(destination); fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);