List of usage examples for java.io FileInputStream getChannel
public FileChannel getChannel()
From source file:com.project.utilities.Utilities.java
public static ArrayList<ViolationDataModel> readViolationJSONFileToArray() { ArrayList<ViolationDataModel> violation = new ArrayList<ViolationDataModel>(); Gson gson = new Gson(); String jsonStr = ""; try {/*ww w .jav a2 s . c o m*/ File jsonFile = new File(Constants.EXTERNAL_DIRECTORY + "/" + Constants.FILENAME_VIOLATION_JSON); if (!jsonFile.exists()) { Log.e("VIOLATION_JSON_LIST", "Error reading file"); } else { Log.e("readJsonFile", "File Found"); } FileInputStream stream = new FileInputStream(jsonFile); try { FileChannel fc = stream.getChannel(); MappedByteBuffer byteBuffer = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); jsonStr = Charset.defaultCharset().decode(byteBuffer).toString(); } finally { stream.close(); } } catch (Exception e) { Log.e("readJsonFile", e.getMessage()); } try { JSONObject jsonContent = new JSONObject(jsonStr); violation = gson.fromJson(jsonContent.getString("data"), new TypeToken<ArrayList<ViolationDataModel>>() { }.getType()); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); Log.e("Error GSON", e.getMessage().toString()); } return violation; }
From source file:FileHelper.java
private static void copyFile(File srcFile, File destFile, long chunkSize) throws IOException { FileInputStream is = null; FileOutputStream os = null;// w w w. ja va2 s .co m try { is = new FileInputStream(srcFile); FileChannel iChannel = is.getChannel(); os = new FileOutputStream(destFile, false); FileChannel oChannel = os.getChannel(); long doneBytes = 0L; long todoBytes = srcFile.length(); while (todoBytes != 0L) { long iterationBytes = Math.min(todoBytes, chunkSize); long transferredLength = oChannel.transferFrom(iChannel, doneBytes, iterationBytes); if (iterationBytes != transferredLength) { throw new IOException("Error during file transfer: expected " + iterationBytes + " bytes, only " + transferredLength + " bytes copied."); } doneBytes += transferredLength; todoBytes -= transferredLength; } } finally { if (is != null) { is.close(); } if (os != null) { os.close(); } } boolean successTimestampOp = destFile.setLastModified(srcFile.lastModified()); if (!successTimestampOp) { System.out.println("Could not change timestamp for {}. Index synchronization may be slow. " + destFile); } }
From source file:Main.java
public static File copyFile(File destinationFile, File sourceFile) throws IOException { FileInputStream inputStream = null; FileChannel inputChannel = null; FileOutputStream outputStream = null; FileChannel outputChannel = null; try {/*from w ww. j a v a 2 s . c o m*/ inputStream = new FileInputStream(sourceFile); inputChannel = inputStream.getChannel(); outputStream = new FileOutputStream(destinationFile); outputChannel = outputStream.getChannel(); inputChannel.transferTo(0, inputChannel.size(), outputChannel); return destinationFile; } catch (IOException exception) { throw exception; } finally { if (inputChannel != null) { inputChannel.close(); } if (inputStream != null) { inputStream.close(); } if (outputChannel != null) { outputChannel.close(); } if (outputStream != null) { outputStream.close(); } } }
From source file:org.sleuthkit.autopsy.recentactivity.Util.java
public static String readFile(String path) throws IOException { FileInputStream stream = new FileInputStream(new File(path)); try {/* ww w . j a va 2 s .co m*/ 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:org.geomajas.plugin.deskmanager.utility.FileUtils.java
/** * Zip all files in a directory, only relative filenames are used, does not recurse. * /*from www . j av a2s .c o m*/ * @param sourceFolder * @param targetFileName * @return * @throws IOException */ public static File zipDirectory(File sourceFolder, String targetFileName) throws IOException { File target = new File(sourceFolder, targetFileName + (targetFileName.endsWith(".zip") ? "" : ".zip")); if (target.isFile()) { throw new IOException("Bestand bestaat reeds! " + target.getAbsolutePath()); } File[] files = sourceFolder.listFiles(); ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(target)); WritableByteChannel out = Channels.newChannel(zos); FileInputStream is = null; try { for (File file : files) { zos.putNextEntry(new ZipEntry(file.getName())); is = new FileInputStream(file); FileChannel in = is.getChannel(); in.transferTo(0, in.size(), out); is.close(); } } catch (IOException e) { throw e; } finally { if (is != null) { is.close(); } if (zos != null) { zos.close(); } } return target; }
From source file:com.alibaba.otter.shared.common.utils.NioUtilsPerformance.java
public static void sendFileTest(File source, File target) throws Exception { FileInputStream fis = null; FileOutputStream fos = null;//from w ww. j a v a2 s. co m try { fis = new FileInputStream(source); fos = new FileOutputStream(target); FileChannel sChannel = fis.getChannel(); FileChannel tChannel = fos.getChannel(); target.createNewFile(); sChannel.transferTo(0, source.length(), tChannel); tChannel.close(); sChannel.close(); } finally { IOUtils.closeQuietly(fis); IOUtils.closeQuietly(fos); } }
From source file:com.bazaarvoice.seo.sdk.util.BVUtility.java
public static String readFile(String path) throws IOException { FileInputStream stream = new FileInputStream(new File(path)); try {//from w w w .ja v a2 s . c om FileChannel fc = stream.getChannel(); MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); return Charset.forName("UTF-8").decode(bb).toString(); } finally { stream.close(); } }
From source file:com.alibaba.otter.shared.common.utils.NioUtilsPerformance.java
public static void channelTest(File source, File target) throws Exception { FileInputStream fis = null; FileOutputStream fos = null;// ww w . jav a 2 s.c om try { fis = new FileInputStream(source); fos = new FileOutputStream(target); FileChannel sChannel = fis.getChannel(); FileChannel tChannel = fos.getChannel(); target.createNewFile(); ByteBuffer buffer = ByteBuffer.allocate(16 * 1024); while (sChannel.read(buffer) > 0) { buffer.flip(); tChannel.write(buffer); buffer.clear(); } tChannel.close(); sChannel.close(); } finally { IOUtils.closeQuietly(fis); IOUtils.closeQuietly(fos); } }
From source file:VASSAL.tools.io.IOUtils.java
/** * Copies bytes from a large (over 2GB) <code>FileInputStream</code> to a * <code>FileOutputStream</code>. * * This method uses channels. The input file should not be written * to during the copy.//from w w w . ja v a 2s. c o m * * @param in the source * @param out the destination * @throws IOException if one occurs while reading or writing */ public static long copyLarge(FileInputStream in, FileOutputStream out) throws IOException { final FileChannel inc = in.getChannel(); return inc.transferTo(0L, inc.size(), out.getChannel()); }
From source file:yui.classes.utils.IOUtils.java
public static void fastCopy(File source, File dest) throws IOException { FileInputStream fi = new FileInputStream(source); FileChannel fic = fi.getChannel(); MappedByteBuffer mbuf = fic.map(FileChannel.MapMode.READ_ONLY, 0, source.length()); fic.close();// w w w. ja v a 2 s .c om fi.close(); FileOutputStream fo = new FileOutputStream(dest); FileChannel foc = fo.getChannel(); foc.write(mbuf); foc.close(); fo.close(); }