/** * Saves bytes to a file to inside the sketch folder. * The filename can be a relative path, i.e. "poo/bytefun.txt" * would save to a file named "bytefun.txt" to a subfolder * called 'poo' inside the sketch folder. If the in-between * subfolders don't exist, they'll be created. */ public void saveBytes(String filename, byte buffer[]) { saveBytes(saveFile(filename), buffer); }
/** * Saves bytes to a specific File location specified by the user. */ static public void saveBytes(File file, byte buffer[]) { try { String filename = file.getAbsolutePath(); createPath(filename); OutputStream output = new FileOutputStream(file); if (file.getName().toLowerCase().endsWith(".gz")) { output = new GZIPOutputStream(output); } saveBytes(output, buffer); output.close(); } catch (IOException e) { System.err.println("error saving bytes to " + file); e.printStackTrace(); } }
/** * ( begin auto-generated from saveBytes.xml ) * * Opposite of <b>loadBytes()</b>, will write an entire array of bytes to a * file. The data is saved in binary format. This file is saved to the * sketch's folder, which is opened by selecting "Show sketch folder" from * the "Sketch" menu.<br /> * <br /> * It is not possible to use saveXxxxx() functions inside a web browser * unless the sketch is <a * href="http://wiki.processing.org/w/Sign_an_Applet">signed applet</A>. To * save a file back to a server, see the <a * href="http://wiki.processing.org/w/Saving_files_to_a_web-server">save to * web</A> code snippet on the Processing Wiki. * * ( end auto-generated ) * * @webref output:files * @param filename name of the file to write to * @param data array of bytes to be written * @see PApplet#loadStrings(String) * @see PApplet#loadBytes(String) * @see PApplet#saveStrings(String, String[]) */ public void saveBytes(String filename, byte[] data) { saveBytes(saveFile(filename), data); }
/** * @nowebref * Saves bytes to a specific File location specified by the user. */ static public void saveBytes(File file, byte[] data) { File tempFile = null; try { tempFile = createTempFile(file); OutputStream output = createOutput(tempFile); saveBytes(output, data); output.close(); output = null; if (file.exists()) { if (!file.delete()) { System.err.println("Could not replace " + file.getAbsolutePath()); } } if (!tempFile.renameTo(file)) { System.err.println("Could not rename temporary file " + tempFile.getAbsolutePath()); } } catch (IOException e) { System.err.println("error saving bytes to " + file); if (tempFile != null) { tempFile.delete(); } e.printStackTrace(); } }