List of usage examples for java.io FileOutputStream getChannel
public FileChannel getChannel()
From source file:net.librec.util.FileUtil.java
/** * fast file copy/* w w w .j a v a 2 s .c o m*/ * * @param source source file * @param target target file * @throws Exception if error occurs */ public static void copyFile(File source, File target) throws Exception { FileInputStream fis = new FileInputStream(source); FileOutputStream fos = new FileOutputStream(target); FileChannel inChannel = fis.getChannel(); FileChannel outChannel = fos.getChannel(); // inChannel.transferTo(0, inChannel.size(), outChannel); // original -- apparently has trouble copying large files on Windows // magic number for Windows, 64Mb - 32Kb int maxCount = (64 * 1024 * 1024) - (32 * 1024); long size = inChannel.size(); long position = 0; while (position < size) { position += inChannel.transferTo(position, maxCount, outChannel); } inChannel.close(); outChannel.close(); fis.close(); fos.close(); }
From source file:org.mule.util.FileUtils.java
public static boolean renameFileHard(File srcFile, File destFile) { boolean isRenamed = false; if (srcFile != null && destFile != null) { logger.debug("Moving file " + srcFile.getAbsolutePath() + " to " + destFile.getAbsolutePath()); if (!destFile.exists()) { try { if (srcFile.isFile()) { logger.debug("Trying to rename file"); FileInputStream in = null; FileOutputStream out = null; try { in = new FileInputStream(srcFile); out = new FileOutputStream(destFile); out.getChannel().transferFrom(in.getChannel(), 0, srcFile.length()); isRenamed = true; } catch (Exception e) { logger.debug(e); } finally { if (in != null) { try { in.close(); } catch (Exception inNotClosed) { logger.debug(inNotClosed); }/*from w w w . j a va 2 s . com*/ } if (out != null) { try { out.close(); } catch (Exception outNotClosed) { logger.debug(outNotClosed); } } } logger.debug("File renamed: " + isRenamed); if (isRenamed) { srcFile.delete(); } else { destFile.delete(); } } else { logger.debug(srcFile.getAbsolutePath() + " is not a valid file."); } } catch (Exception e) { logger.debug("Error renaming file from " + srcFile.getAbsolutePath() + " to " + destFile.getAbsolutePath()); } } else { logger.debug("Error renaming file " + srcFile.getAbsolutePath() + ". Destination file " + destFile.getAbsolutePath() + " already exists."); } } return isRenamed; }
From source file:com.dmbstream.android.util.Util.java
public static void atomicCopy(File from, File to) throws IOException { FileInputStream in = null;/*from ww w . j av a 2s . co m*/ FileOutputStream out = null; File tmp = null; try { tmp = new File(to.getPath() + ".tmp"); in = new FileInputStream(from); out = new FileOutputStream(tmp); in.getChannel().transferTo(0, from.length(), out.getChannel()); out.close(); if (!tmp.renameTo(to)) { throw new IOException("Failed to rename " + tmp + " to " + to); } Log.i(TAG, "Copied " + from + " to " + to); } catch (IOException x) { close(out); delete(to); throw x; } finally { close(in); close(out); delete(tmp); } }
From source file:syndeticlogic.catena.utility.FixedLengthArrayGenerator.java
public void generateFileArray() throws Exception { file = new File(filename); FileOutputStream out = new FileOutputStream(file); out.getChannel().truncate(0); out.flush();/*from w w w . j a v a 2 s . co m*/ Random localRandom = new Random(seed); for (int i = 0; i < elementLength; i++) { byte[] buffer = new byte[elementSize]; localRandom.nextBytes(buffer); out.write(buffer); out.flush(); if (log.isTraceEnabled()) { System.out.println("generate file " + i + " ==================================================================="); for (int k = 0; k < buffer.length; k++) { System.out.print((Integer.toHexString((int) buffer[k] & 0xff)) + ", "); } System.out.println(); } } out.close(); }
From source file:com.hazelcast.simulator.boot.SimulatorInstaller.java
void install() { File simulatorHome = new File(FileUtils.getUserHomePath(), "hazelcast-simulator-" + version); System.setProperty("SIMULATOR_HOME", simulatorHome.getAbsolutePath()); System.out.println("Installing Simulator: " + version); File userHome = getUserHome(); try {//from w w w . j a v a 2 s . c om if (version.endsWith("SNAPSHOT")) { File archive = new File( format("%s/.m2/repository/com/hazelcast/simulator/dist/%s/dist-%s-dist.tar.gz", userHome, version, version)); if (archive.exists()) { simulatorHome.delete(); decompress(archive); } else if (!simulatorHome.exists()) { throw new IllegalStateException( "Could not install simulator, archive: " + archive.getAbsolutePath() + " not found"); } } else if (!simulatorHome.exists()) { File archive = new File(getUserHome(), format("hazelcast-simulator-%s-dist.tar.gz", version)); URL url = new URL(format( "http://repo1.maven.org/maven2/" + "com/hazelcast/simulator/dist/%s/dist-%s-dist.tar.gz", version, version)); ReadableByteChannel rbc = Channels.newChannel(url.openStream()); System.out.printf("File [%s] doesn't exist; downloading\n", archive.getAbsolutePath()); FileOutputStream fos = new FileOutputStream(archive); fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); decompress(archive); archive.delete(); } } catch (Exception e) { throw new RuntimeException(e); } }
From source file:org.mule.util.FileUtils.java
/** * Try to move a file by renaming with backup attempt by copying/deleting via NIO. * Creates intermidiate directories as required. *///from w w w .j a va 2s. c om public static boolean moveFileWithCopyFallback(File sourceFile, File destinationFile) { // try fast file-system-level move/rename first boolean success = sourceFile.renameTo(destinationFile); if (!success) { // try again using NIO copy FileInputStream fis = null; FileOutputStream fos = null; try { fis = new FileInputStream(sourceFile); if (!destinationFile.exists()) { FileUtils.createFile(destinationFile.getPath()); } fos = new FileOutputStream(destinationFile); FileChannel srcChannel = fis.getChannel(); FileChannel dstChannel = fos.getChannel(); dstChannel.transferFrom(srcChannel, 0, srcChannel.size()); srcChannel.close(); dstChannel.close(); success = sourceFile.delete(); } catch (IOException ioex) { // grr! success = false; } finally { IOUtils.closeQuietly(fis); IOUtils.closeQuietly(fos); } } return success; }
From source file:org.jodconverter.job.SourceDocumentSpecsFromInputStream.java
@Override public File getFile() { // Write the InputStream to the temp file final File tempFile = Optional.ofNullable(getFormat()) .map(format -> fileMaker.makeTemporaryFile(format.getExtension())).orElse(super.getFile()); try {// w w w.ja va 2 s. c om final FileOutputStream outputStream = new FileOutputStream(tempFile); // NOSONAR outputStream.getChannel().lock(); try { IOUtils.copy(inputStream, outputStream); return tempFile; } finally { // Note: This will implicitly release the file lock. outputStream.close(); } } catch (IOException ex) { throw new DocumentSpecsIOException("Could not write stream to file " + tempFile, ex); } }
From source file:updater.Updater.java
@SuppressWarnings("resource") public Updater() { try {/*from w w w . j av a 2s.c o m*/ System.out.println("Checking for updates..."); URL url1 = new URL( "https://raw.githubusercontent.com/kvsjxd/Droid-PC-Suite/master/.release-version.txt"); ReadableByteChannel obj1 = Channels.newChannel(url1.openStream()); FileOutputStream outputstream1 = new FileOutputStream(".release-version.txt"); outputstream1.getChannel().transferFrom(obj1, 0, Long.MAX_VALUE); FileReader file = new FileReader(".release-version.txt"); BufferedReader reader = new BufferedReader(file); String DownloadedString = reader.readLine(); File file2 = new File(".release-version.txt"); if (file2.exists() && !file2.isDirectory()) { file2.delete(); } double AvailableUpdate = Double.parseDouble(DownloadedString); InputStreamReader reader2 = new InputStreamReader( getClass().getResourceAsStream("/others/app-version.txt")); String tmp = IOUtils.toString(reader2); double ApplicationVersion = Double.parseDouble(tmp); if (AvailableUpdate > ApplicationVersion) { System.out.println("Your Droid PC Suite version: V" + ApplicationVersion); System.out.println( "New update V" + AvailableUpdate + " is available! Please download latest version now!"); URL url2 = new URL( "https://raw.githubusercontent.com/kvsjxd/Droid-PC-Suite/master/.release-changelog.txt"); ReadableByteChannel obj2 = Channels.newChannel(url2.openStream()); FileOutputStream outputstream2 = new FileOutputStream(".release-changelog.txt"); outputstream2.getChannel().transferFrom(obj2, 0, Long.MAX_VALUE); UpdaterGUI obj = new UpdaterGUI(); obj.setVisible(true); } else { System.out.println("You are running latest Droid PC Suite..."); } } catch (Exception e) { e.printStackTrace(); } }
From source file:util.io.IOUtilities.java
/** * Copies a file./*from w w w . ja v a2 s.co m*/ * * @param src The file to read from * @param target The file to write to * @param onlyNew Overwrite only older files. * @throws IOException If copying failed * @since 2.2.2/2.5.1 */ public static void copy(File src, File target, boolean onlyNew) throws IOException { BufferedInputStream in = null; BufferedOutputStream out = null; try { FileOutputStream outFile = new FileOutputStream(target); in = new BufferedInputStream(new FileInputStream(src), 0x4000); out = new BufferedOutputStream(outFile, 0x4000); if (!onlyNew || target.length() < 1 || (src.lastModified() > target.lastModified())) { outFile.getChannel().truncate(0); pipeStreams(in, out); } in.close(); out.close(); } finally { if (in != null) { try { in.close(); } catch (IOException exc) { } } if (out != null) { try { out.close(); } catch (IOException exc) { } } } }
From source file:org.sonarsource.sonarlint.core.plugin.DefaultPluginJarExploder.java
private File unzipFile(File cachedFile) throws IOException { String filename = cachedFile.getName(); File destDir = new File(cachedFile.getParentFile(), filename + "_unzip"); if (!destDir.exists()) { File lockFile = new File(cachedFile.getParentFile(), filename + "_unzip.lock"); FileOutputStream out = new FileOutputStream(lockFile); try {//from w w w .jav a2 s. co m FileLock lock = out.getChannel().lock(); try { // Recheck in case of concurrent processes if (!destDir.exists()) { Path tempDir = fileCache.createTempDir(); ZipUtils.unzip(cachedFile, tempDir.toFile(), newLibFilter()); FileUtils.moveDirectory(tempDir.toFile(), destDir); } } finally { lock.release(); } } finally { out.close(); FileUtils.deleteQuietly(lockFile); } } return destDir; }