List of usage examples for java.io FileOutputStream write
public void write(byte b[]) throws IOException
b.length
bytes from the specified byte array to this file output stream. From source file:com.bluexml.side.util.documentation.LogSave.java
/** * Move file from this jar to the specified folder * // w ww. ja va 2 s.com * @param folderDest * @param fileName * @param folderSource * @throws Exception */ private static void moveFile(String folderDest, String fileName, String folderSource) throws Exception { /* * InputStream in = LogSave.class.getResourceAsStream(folderSource + * fileName); */ InputStream in = LogSave.class.getClassLoader().getResourceAsStream(folderSource + "/" + fileName); File dest = new File(folderDest); if (!dest.exists()) { dest.mkdirs(); } File file = new File(folderDest + fileName); FileOutputStream fos; fos = new FileOutputStream(file); int data; data = in.read(); while (data != -1) { fos.write(data); data = in.read(); } fos.close(); }
From source file:com.sinpo.xnfc.nfc.Util.java
public static void writeFileSdcardFile(String fileName, String write_str) throws IOException { try {//from w ww . j a v a 2s . com FileOutputStream fout = new FileOutputStream(fileName); byte[] bytes = write_str.getBytes(); fout.write(bytes); fout.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:com.cssweb.android.common.CssIniFile.java
public static boolean saveStockData(Context context, String filename, String filecontent) { FileOutputStream fileOut = null; try {//from w w w. ja va2 s. co m fileOut = context.openFileOutput(filename + ".dat", Context.MODE_PRIVATE); fileOut.write(filecontent.getBytes()); fileOut.close(); } catch (FileNotFoundException e) { return false; } catch (IOException e) { return false; } return true; }
From source file:com.heliosdecompiler.helios.bootloader.Bootloader.java
private static byte[] loadSWTLibrary() throws IOException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, ClassNotFoundException { String name = getOSName();/*from w w w. ja v a2 s. c o m*/ if (name == null) throw new IllegalArgumentException("Cannot determine OS"); String arch = getArch(); if (arch == null) throw new IllegalArgumentException("Cannot determine architecture"); String artifactId = "org.eclipse.swt." + name + "." + arch; String swtLocation = artifactId + "-" + SWT_VERSION + ".jar"; System.out.println("Loading SWT version " + swtLocation); byte[] data = null; File savedJar = new File(Constants.DATA_DIR, swtLocation); if (savedJar.isDirectory() && !savedJar.delete()) throw new IllegalArgumentException("Saved file is a directory and could not be deleted"); if (savedJar.exists() && savedJar.canRead()) { try { System.out.println("Loading from saved file"); InputStream inputStream = new FileInputStream(savedJar); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); copy(inputStream, outputStream); data = outputStream.toByteArray(); } catch (IOException exception) { System.out.println("Failed to load from saved file."); exception.printStackTrace(System.out); } } if (data == null) { InputStream fromJar = Bootloader.class.getResourceAsStream("/swt/" + swtLocation); if (fromJar != null) { try { System.out.println("Loading from within JAR"); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); copy(fromJar, outputStream); data = outputStream.toByteArray(); } catch (IOException exception) { System.out.println("Failed to load within JAR"); exception.printStackTrace(System.out); } } } if (data == null) { URL url = new URL("https://maven-eclipse.github.io/maven/org/eclipse/swt/" + artifactId + "/" + SWT_VERSION + "/" + swtLocation); try { System.out.println("Loading over the internet"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); if (connection.getResponseCode() == 200) { InputStream fromURL = connection.getInputStream(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); copy(fromURL, outputStream); data = outputStream.toByteArray(); } else { throw new IOException(connection.getResponseCode() + ": " + connection.getResponseMessage()); } } catch (IOException exception) { System.out.println("Failed to load over the internet"); exception.printStackTrace(System.out); } } if (data == null) { throw new IllegalArgumentException("Failed to load SWT"); } if (!savedJar.exists()) { try { System.out.println("Writing to saved file"); if (savedJar.createNewFile()) { FileOutputStream fileOutputStream = new FileOutputStream(savedJar); fileOutputStream.write(data); fileOutputStream.close(); } else { throw new IOException("Could not create new file"); } } catch (IOException exception) { System.out.println("Failed to write to saved file"); exception.printStackTrace(System.out); } } byte[] dd = data; URL.setURLStreamHandlerFactory(protocol -> { //JarInJar! if (protocol.equals("swt")) { return new URLStreamHandler() { protected URLConnection openConnection(URL u) { return new URLConnection(u) { public void connect() { } public InputStream getInputStream() { return new ByteArrayInputStream(dd); } }; } protected void parseURL(URL u, String spec, int start, int limit) { // Don't parse or it's too slow } }; } return null; }); ClassLoader classLoader = Bootloader.class.getClassLoader(); Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class); method.setAccessible(true); method.invoke(classLoader, new URL("swt://load")); return data; }
From source file:de.ingrid.portal.global.UtilsFileHelper.java
/** * Convert a byte array to file//from www. j a v a 2 s. c om * * @param byteFile * @param docid * @param title * @param mimetyp * @return HashMap with file details * @throws IOException */ public static HashMap<String, String> getByteAsFile(byte[] byteFile, String title, String mimetyp, RenderRequest request) throws IOException { HashMap<String, String> fileDetails = null; File directory = null; File file = null; String typ = null; String parentTyp = null; if (byteFile != null) { fileDetails = new HashMap<String, String>(); typ = UtilsMimeType.getFileExtensionOfMimeType(mimetyp); parentTyp = UtilsMimeType.getMimeTypeParent(mimetyp); if (typ != null) { directory = new File( System.getProperty("java.io.tmpdir") + "/ingrid-portal-apps/details/" + parentTyp + "/"); directory.mkdirs(); file = new File(directory.getAbsolutePath() + "/" + generateFilename(parentTyp, title, new String(byteFile).hashCode(), typ)); file.createNewFile(); FileOutputStream fos = new FileOutputStream(file); fos.write(byteFile); fos.close(); if (log.isDebugEnabled()) { log.debug("Create file: " + file.getAbsolutePath()); } fileDetails.put("mimetyp", mimetyp); fileDetails.put("filename", file.getName()); fileDetails.put("title", title); fileDetails.put("path", file.getAbsolutePath()); fileDetails.put("parenttyp", parentTyp); // Set file into servlet session request.getPortletSession().setAttribute(file.getName(), file.getAbsolutePath(), PortletSession.APPLICATION_SCOPE); } } return fileDetails; }
From source file:com.sinpo.xnfc.nfc.Util.java
public static void writeFileSdcardFile(String fileName, String write_str, boolean append) throws IOException { try {//from www .j a va 2 s . c o m FileOutputStream fout = new FileOutputStream(fileName, append); byte[] bytes = write_str.getBytes(); fout.write(bytes); fout.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:eu.liveandgov.ar.utilities.Download_Data.java
/** DownAndCopy * /*from www . j av a 2 s . c o m*/ * Download file from URL and Copy to Android file system folder * * @param fileUrl * @param StringAndroidPath */ public static boolean DownAndCopy(String fileUrlSTR, String StringAndroidPath, boolean preservefilename, String Caller) { if (compareRemoteWithLocal(fileUrlSTR, StringAndroidPath)) { //Log.e("fileUrlSTR", "SKIP WITH HASH"); return true; } else { Log.e("TRY TO DOWNLOAD BY " + Caller, fileUrlSTR); } SimpleDateFormat sdf = new SimpleDateFormat("mm"); // Check if downloaded at least just 2 minutes ago for (String[] mem : MemDown) if (fileUrlSTR.equals(mem[0])) { int diff = Integer.parseInt(sdf.format(new Date())) - Integer.parseInt(mem[1]); Log.e("diff", " " + diff); if (diff < 2) { Log.d("Download_Data", "I am not downloading " + fileUrlSTR + " because it was downloaded " + diff + " minutes ago"); return true; } } if (!OS_Utils.exists(fileUrlSTR)) { Log.e("Download_Data", "URL: " + fileUrlSTR + " called from " + Caller + " not exists to copy it to " + StringAndroidPath); return false; } int DEBUG_FLAG = 0; HttpURLConnection conn; URL fileUrl = null; try { fileUrl = new URL(fileUrlSTR); } catch (MalformedURLException e1) { return false; } try { conn = (HttpURLConnection) fileUrl.openConnection(); DEBUG_FLAG = 1; conn.setDoInput(true); conn.connect(); InputStream is = conn.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is); ByteArrayBuffer baf = new ByteArrayBuffer(100); DEBUG_FLAG = 2; int current = 0; byte[] buffer = new byte[10]; while ((current = bis.read(buffer)) != -1) baf.append(buffer, 0, current); DEBUG_FLAG = 3; /* Convert the Bytes read to a String. */ File fileAndroid; try { if (preservefilename) { int iSlash = fileUrlSTR.lastIndexOf("/"); fileAndroid = new File(StringAndroidPath + "/" + fileUrlSTR.substring(iSlash + 1)); } else fileAndroid = new File(StringAndroidPath); } catch (Exception e) { Log.e("Download_Data.DownAndCopy", "I can not create " + StringAndroidPath); bis.close(); conn.disconnect(); return false; } DEBUG_FLAG = 4; FileOutputStream fos = new FileOutputStream(fileAndroid); fos.write(baf.toByteArray()); DEBUG_FLAG = 5; bis.close(); fos.close(); conn.disconnect(); MemDown.add(new String[] { fileUrlSTR, sdf.format(new Date()) }); return true; //returns including the filename } catch (IOException e) { Log.e("Download_Data", "Download_Data: Error when trying to download: " + fileUrl.toString() + " to " + StringAndroidPath + " DEBUG_FLAG=" + DEBUG_FLAG); return false; } }
From source file:com.example.mediastock.util.Utilities.java
/** * Method to save a media file inside the directory typeDir. It writes the bytes of the stream into the file * @param type the directory type: music, video or image * @param context the context/* www .j av a2 s. c o m*/ * @param inputStream the stream * @param id the id of the media file * @return the path of the media file */ public static String saveMediaToInternalStorage(String type, Context context, InputStream inputStream, int id) { ContextWrapper cw = new ContextWrapper(context.getApplicationContext()); File dir = cw.getDir(type + "Dir", Context.MODE_PRIVATE); // Create media file File file; if (type.equals(Utilities.MUSIC_DIR)) file = new File(dir, type + id + ".mp3"); else file = new File(dir, type + id); FileOutputStream fos; try { fos = new FileOutputStream(file); fos.write(Utilities.covertStreamToByte(inputStream)); fos.flush(); fos.close(); inputStream.close(); } catch (IOException e) { e.printStackTrace(); } return file.getName(); }