List of usage examples for java.io FileOutputStream write
public void write(byte b[], int off, int len) throws IOException
len
bytes from the specified byte array starting at offset off
to this file output stream. From source file:Main.java
/** * Copy the file from the assets to the map tiles directory if it was * shipped with the APK.//from ww w. j ava2s .c o m */ public static boolean copyTileAsset(Context context, String filename) { if (!hasTileAsset(context, filename)) { // file does not exist as asset return false; } // copy file from asset to internal storage try { InputStream is = context.getAssets().open(TILE_PATH + File.separator + filename); File f = getTileFile(context, filename); FileOutputStream os = new FileOutputStream(f); byte[] buffer = new byte[1024]; int dataSize; while ((dataSize = is.read(buffer)) > 0) { os.write(buffer, 0, dataSize); } os.close(); } catch (IOException e) { return false; } return true; }
From source file:Main.java
private static void copyAssetFolder(AssetManager am, String src, String dest) throws IOException { Log.i("Copy ", src); InputStream srcIS = null;//from ww w. j a va 2 s . c om File destfh; // this is the only way we can tell if this is a file or a // folder - we have to open the asset, and if the open fails, // it's a folder... boolean isDir = false; try { srcIS = am.open(src); } catch (FileNotFoundException e) { isDir = true; } // either way, we'll use the dest as a File destfh = new File(dest); // and now, depending on .. if (isDir) { // If the directory doesn't yet exist, create it if (!destfh.exists()) { destfh.mkdir(); } // list the assets in the directory... String assets[] = am.list(src); // and copy them all using same. for (String asset : assets) { copyAssetFolder(am, src + "/" + asset, dest + "/" + asset); } } else { int count, buffer_len = 2048; byte[] data = new byte[buffer_len]; // copy the file from the assets subsystem to the filesystem FileOutputStream destOS = new FileOutputStream(destfh); //copy the file content in bytes while ((count = srcIS.read(data, 0, buffer_len)) != -1) { destOS.write(data, 0, count); } // and close the two files srcIS.close(); destOS.close(); } }
From source file:Main.java
public static File getRobotCacheFile(Context context, String assetname) throws IOException { File cacheFile = new File(context.getCacheDir(), assetname); try {/*from w w w. ja v a 2 s . c o m*/ InputStream inputStream = context.getAssets().open(assetname); try { FileOutputStream outputStream = new FileOutputStream(cacheFile); try { byte[] buf = new byte[1024]; int len; while ((len = inputStream.read(buf)) > 0) { outputStream.write(buf, 0, len); } } finally { outputStream.close(); } } finally { inputStream.close(); } } catch (IOException e) { throw new IOException("Could not open " + assetname, e); } return cacheFile; }
From source file:Main.java
public static void copyFile(File in, File out) throws IOException { FileInputStream fis = new FileInputStream(in); if (!out.exists()) { if (in.isDirectory()) { out.mkdirs();/* ww w .ja v a 2s . com*/ } else { File parentDir = new File(out.getParent()); if (!parentDir.exists()) { parentDir.mkdirs(); } out.createNewFile(); } } FileOutputStream fos = new FileOutputStream(out); try { byte[] buf = new byte[8192]; int i = 0; while ((i = fis.read(buf)) != -1) fos.write(buf, 0, i); } catch (IOException e) { throw e; } finally { fis.close(); fos.close(); } }
From source file:Main.java
public static void copyDB(Context context, String fileName) throws IOException { String filePath = context.getFilesDir().getAbsolutePath() + "/" + fileName; if (new File(filePath).exists()) { return;// w w w. j av a 2 s .c o m } FileOutputStream fos = new FileOutputStream(new File(filePath)); InputStream is = context.getResources().getAssets().open(fileName); byte[] buffer = new byte[1024 * 500]; int count = 0; while ((count = is.read(buffer)) > 0) { fos.write(buffer, 0, count); } fos.close(); is.close(); }
From source file:Main.java
public static File extractAssetToFile(Context context, String file) { File cacheFile = new File(context.getCacheDir(), file); try {/*from www. java2 s . com*/ InputStream inputStream = context.getAssets().open(file); try { FileOutputStream outputStream = new FileOutputStream(cacheFile); try { byte[] buf = new byte[1024]; int len; while ((len = inputStream.read(buf)) > 0) { outputStream.write(buf, 0, len); } } finally { outputStream.close(); } } finally { inputStream.close(); } } catch (IOException e) { e.printStackTrace(); return null; } return cacheFile; }
From source file:Main.java
public static boolean copyFile(String oldPath, String newPath) { boolean bRet = false; try {/*w ww.ja va2 s. co m*/ int bytesum = 0; int byteread = 0; File oldfile = new File(oldPath); if (oldfile.exists()) { InputStream inStream = new FileInputStream(oldPath); FileOutputStream fs = new FileOutputStream(newPath); byte[] buffer = new byte[1444]; int length; while ((byteread = inStream.read(buffer)) != -1) { bytesum += byteread; fs.write(buffer, 0, byteread); } inStream.close(); } bRet = true; } catch (Exception e) { bRet = false; } return bRet; }
From source file:Main.java
/** * Downloads a file via HTTP(S) GET to the given path. This function cannot be called from the * UI thread. Android does not allow it. * * @param urlString Url to the ressource to download. * @param file file to be written to. * @param overwrite if file exists, overwrite? * @return flase if download was not successful. If successful, true. *//*from w w w . ja v a2 s .c om*/ private static Boolean fileDownloadHttp(String urlString, File file, Boolean overwrite) { HashMap<String, String> result = null; URL url = null; // File temp; try { url = new URL(urlString); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod("GET"); urlConnection.setReadTimeout(200000); urlConnection.connect(); InputStream in = new BufferedInputStream(urlConnection.getInputStream()); FileOutputStream outputStream = new FileOutputStream(file); int read = 0; byte[] bytes = new byte[1024]; while ((read = in.read(bytes)) != -1) { outputStream.write(bytes, 0, read); } in.close(); outputStream.close(); urlConnection.disconnect(); } catch (MalformedURLException e) { e.printStackTrace(); return false; } catch (IOException e) { e.printStackTrace(); return false; } Log.d(TAG, "File download: " + file.getAbsolutePath() + url.getFile() + "overwrite " + overwrite + "exists? " + file.exists()); return true; }
From source file:jBittorrentAPI.utils.IOManager.java
/** * Save the byte array into a file with the give filename * @param data byte[]/*from w w w . j av a2s . c om*/ * @param filename String * @return boolean */ public static boolean save(byte[] data, String filename) { try { FileOutputStream fos = new FileOutputStream(filename); fos.write(data, 0, data.length); fos.flush(); fos.close(); return true; } catch (IOException ioe) { } return false; }
From source file:helpers.FileUpload.java
public static boolean processFile(String path, FileItemStream item) { try {//from ww w. j a v a 2s.co m File f = new File(path + File.separator + "assets" + File.separator + "uploads"); if (!f.exists()) { f.mkdir(); } File savedFile = new File(f.getAbsolutePath() + File.separator + item.getName()); FileOutputStream fos = new FileOutputStream(savedFile); InputStream is = item.openStream(); int x = 0; byte[] b = new byte[1024]; while ((x = is.read(b)) != -1) { fos.write(b, 0, x); } fos.flush(); fos.close(); return true; } catch (Exception e) { System.out.println("FileUpload 35: " + e.getMessage()); } return false; }