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
public static void save(InputStream inputStream, File file) { FileOutputStream fos = null; if (inputStream != null) { try {//from w ww. j a v a 2s . co m fos = new FileOutputStream(file); byte[] buf = new byte[1024]; int slice; while ((slice = inputStream.read(buf)) != -1) { fos.write(buf, 0, slice); } } catch (IOException e) { e.printStackTrace(); } finally { try { inputStream.close(); if (fos != null) { fos.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
From source file:Main.java
public static void copyFile(String src, String dst) { FileInputStream in = null;/*ww w .j a va 2 s . c o m*/ FileOutputStream out = null; try { in = new FileInputStream(src); out = new FileOutputStream(dst); byte[] b = new byte[1024 * 5]; int len = 0; while ((len = in.read(b)) > 0) { out.write(b, 0, len); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (out != null) { out.close(); } if (in != null) { in.close(); } } catch (IOException e) { e.printStackTrace(); } } }
From source file:Main.java
/** * Show a level//from w ww . ja v a 2 s . c o m * * @param context The context * @param level The level */ public static void showLevel(Context context, int level) { String filename; switch (level) { case 1: { filename = "ground_floor.png"; break; } case 2: { filename = "talks_floor.png"; break; } default: { return; } } File f = new File(context.getFilesDir() + "/" + filename); try { if (f.exists() == false) { InputStream is = context.getAssets().open(filename); FileOutputStream fos = context.openFileOutput(filename, Context.MODE_WORLD_READABLE); byte[] buffer = new byte[is.available()]; is.read(buffer); // write the stream to file fos.write(buffer, 0, buffer.length); fos.close(); is.close(); } // Prepare the intent //TODO - create an activity for this instead. Internal viewers might be quite heavy Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(f), "image/png"); context.startActivity(intent); } catch (Exception ex) { ex.printStackTrace(); } }
From source file:Main.java
public static boolean WriteFileFromUrl(String url, String filename) { try {/*from w w w. j a v a 2 s. c om*/ URL fileUrl = new URL(url); URLConnection connection = fileUrl.openConnection(); InputStream inputStream = new BufferedInputStream(fileUrl.openStream(), 10240); File cacheFile = new File(filename); FileOutputStream outputStream = new FileOutputStream(cacheFile); byte buffer[] = new byte[1024]; int dataSize; int loadedSize = 0; while ((dataSize = inputStream.read(buffer)) != -1) { loadedSize += dataSize; outputStream.write(buffer, 0, dataSize); } outputStream.close(); return true; } catch (Exception e) { Log.d("#StorageHelper Error:", e.toString()); return false; } }
From source file:Main.java
public static void copyFolder(String oldPath, String newPath) { try {//from w ww. ja v a 2 s. c om (new File(newPath)).mkdirs(); File a = new File(oldPath); String[] file = a.list(); File temp = null; for (int i = 0; i < file.length; i++) { if (oldPath.endsWith(File.separator)) { temp = new File(oldPath + file[i]); } else { temp = new File(oldPath + File.separator + file[i]); } if (temp.isFile()) { FileInputStream input = new FileInputStream(temp); FileOutputStream output = new FileOutputStream(newPath + "/" + (temp.getName()).toString()); byte[] b = new byte[1024 * 5]; int len; while ((len = input.read(b)) != -1) { output.write(b, 0, len); } output.flush(); output.close(); input.close(); } if (temp.isDirectory()) { copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]); } } } catch (Exception e) { Log.e(TAG, "copyFolder() copy dir error", e); } }
From source file:Main.java
/** * Copy data from a source stream to destFile. Return true if succeed, * return false if failed./*from w ww .j a va 2 s. co m*/ */ public static boolean copyToFile(InputStream inputStream, File destFile) { try { if (destFile.exists()) { destFile.delete(); } FileOutputStream out = new FileOutputStream(destFile); try { byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = inputStream.read(buffer)) >= 0) { out.write(buffer, 0, bytesRead); } } finally { out.flush(); try { out.getFD().sync(); } catch (IOException e) { } out.close(); } return true; } catch (IOException e) { return false; } }
From source file:de.shadowhunt.subversion.internal.AbstractPrepare.java
private static boolean extractArchive(final File zip, final File prefix) throws Exception { final ZipFile zipFile = new ZipFile(zip); final Enumeration<? extends ZipEntry> enu = zipFile.entries(); while (enu.hasMoreElements()) { final ZipEntry zipEntry = enu.nextElement(); final String name = zipEntry.getName(); final File file = new File(prefix, name); if (name.charAt(name.length() - 1) == Resource.SEPARATOR_CHAR) { if (!file.isDirectory() && !file.mkdirs()) { throw new IOException("can not create directory structure: " + file); }/*from w w w . j a va2 s. c om*/ continue; } final File parent = file.getParentFile(); if (parent != null) { if (!parent.isDirectory() && !parent.mkdirs()) { throw new IOException("can not create directory structure: " + parent); } } final InputStream is = zipFile.getInputStream(zipEntry); final FileOutputStream fos = new FileOutputStream(file); final byte[] bytes = new byte[1024]; int length; while ((length = is.read(bytes)) >= 0) { fos.write(bytes, 0, length); } is.close(); fos.close(); } zipFile.close(); return true; }
From source file:Main.java
/** * * * @param context// w w w . ja v a2s . co m * @param assetFilename * @param out * @throws IOException */ public static void copy(Context context, String assetFilename, File out) throws IOException { if (context == null) { return; } else if (out == null) { return; } if (!out.getParentFile().exists()) { out.getParentFile().mkdirs(); } AssetManager assetManager = context.getAssets(); InputStream is = null; FileOutputStream fos = null; try { is = assetManager.open(assetFilename); fos = new FileOutputStream(out); byte[] buf = new byte[1024]; int len = -1; while ((len = is.read(buf, 0, 1024)) != -1) { fos.write(buf, 0, len); } } finally { if (fos != null) fos.close(); if (is != null) is.close(); } }
From source file:Main.java
public static File getFileFromCacheOrURL(File cacheDir, URL url) throws IOException { Log.i(TAG, "getFileFromCacheOrURL(): url = " + url.toExternalForm()); String filename = url.getFile(); int lastSlashPos = filename.lastIndexOf('/'); String fileNameNoPath = new String(lastSlashPos == -1 ? filename : filename.substring(lastSlashPos + 1)); File file = new File(cacheDir, fileNameNoPath); if (file.exists()) { if (file.length() > 0) { Log.i(TAG, "File exists in cache as: " + file.getAbsolutePath()); return file; } else {/*w ww.ja v a 2 s . c om*/ Log.i(TAG, "Deleting zero length file " + file.getAbsolutePath()); file.delete(); } } Log.i(TAG, "File " + file.getAbsolutePath() + " does not exists."); URLConnection ucon = url.openConnection(); ucon.setReadTimeout(5000); ucon.setConnectTimeout(30000); InputStream is = ucon.getInputStream(); BufferedInputStream inStream = new BufferedInputStream(is, 1024 * 5); FileOutputStream outStream = new FileOutputStream(file); byte[] buff = new byte[5 * 1024]; // Read bytes (and store them) until there is nothing more to read(-1) int len; while ((len = inStream.read(buff)) != -1) { outStream.write(buff, 0, len); } // Clean up outStream.flush(); outStream.close(); inStream.close(); return file; }
From source file:DBMS.UpdateFileUpload.java
public static boolean processFile(String path, FileItemStream item, int id) { try {/*from w w w. j av a 2 s . c o m*/ String check = item.getName(); if (check.endsWith(".jpg") || check.endsWith(".JPG")) { String imstring = "images/" + Integer.toString(id); File f = new File(path + File.separator + imstring); 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(); String dbimage = imstring + "/a.jpg"; //dc.enterImage(dbimage); //im =dbimage; //System.out.println("Resizing!"); //Resize rz = new Resize(); //rz.resize(dbimage); BufferedImage originalImage = ImageIO.read(savedFile); int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType(); BufferedImage resizeImageJpg = resizeImage(originalImage, type); ImageIO.write(resizeImageJpg, "jpg", savedFile); File rFile = new File(f.getAbsolutePath() + "/a.jpg"); savedFile.renameTo(rFile); ProfileEditDB dc = new ProfileEditDB(); dc.enterImage(id, dbimage); System.out.println("Link Entered to Database!"); return true; } } catch (Exception e) { e.printStackTrace(); } return false; }