Example usage for java.io FileOutputStream write

List of usage examples for java.io FileOutputStream write

Introduction

In this page you can find the example usage for java.io FileOutputStream write.

Prototype

public void write(byte b[]) throws IOException 

Source Link

Document

Writes b.length bytes from the specified byte array to this file output stream.

Usage

From source file:com.afis.jx.ckfinder.connector.utils.ImageUtils.java

/**
 * writes unchanged file to disk.//from w  ww  . j  ava  2  s  .c  o  m
 *
 * @param stream - stream to read the file from
 *
 * @param destFile - file to write to
 *
 * @throws IOException when error occurs.
 */
private static void writeUntouchedImage(final InputStream stream, final File destFile) throws IOException {
    ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
    byte[] buffer = new byte[MAX_BUFF_SIZE];
    int readNum;
    while ((readNum = stream.read(buffer)) != -1) {
        byteArrayOS.write(buffer, 0, readNum);
    }
    byte[] bytes = byteArrayOS.toByteArray();
    byteArrayOS.close();
    FileOutputStream fileOS = new FileOutputStream(destFile);
    fileOS.write(bytes);
    fileOS.flush();
    fileOS.close();
}

From source file:com.qmetry.qaf.automation.util.FileUtil.java

public static String saveImageFile(String base64Str, String prefix, String dir) throws Exception {
    byte[] decodedScreenshot = Base64.decodeBase64(base64Str.getBytes());// new
    File file = generateFile(prefix, ".png", dir);
    FileOutputStream fos = new FileOutputStream(file);
    fos.write(decodedScreenshot);
    fos.close();//w  ww  .j a v a 2s. c  o m
    return file.getName();
}

From source file:at.asitplus.regkassen.demo.RKSVCashboxSimulator.java

/**
 * helper method for writing JSON rep of objects to files/stdout
 * @param object arbitrary object, that should be converted to JSON string
 * @param outputFile output file for storing JSON rep of object
 * @param stdout output to stdout?//from   ww  w .j  a v  a  2 s. com
 * @param stdoutHeadLine if output to stdout, use this header line
 */
public static void dumpJSONRepOfObject(Object object, File outputFile, boolean stdout, String stdoutHeadLine) {
    try {
        String jsonString = gson.toJson(object);
        if (stdout && VERBOSE) {
            System.out.println();
            System.out.println(stdoutHeadLine);
            System.out.println(jsonString);
            System.out.println();
        }
        FileOutputStream outputStream = new FileOutputStream(outputFile);
        outputStream.write(jsonString.getBytes());
        outputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static boolean writeParcelable(Context context, String fileName, Parcelable parcelObject) {
    boolean success = false;
    FileOutputStream fos = null;
    try {/*from w  ww . j a va2s . com*/
        fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
        Parcel parcel = Parcel.obtain();
        parcel.writeParcelable(parcelObject, Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
        byte[] data = parcel.marshall();
        fos.write(data);

        success = true;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }
    }

    return success;
}

From source file:Main.java

public static void copyBitmapToTempFile(Uri uri) {
    File picture = new File(Environment.getExternalStorageDirectory() + "/yourName", "temp");
    FileOutputStream out = null;
    InputStream is = null;//from ww  w .  j  a  va 2  s  .c om
    try {
        is = mActivity.getContentResolver().openInputStream(uri);
        out = new FileOutputStream(picture);
        byte[] buffer = new byte[1024];
        while (is.read(buffer) != -1) {
            out.write(buffer);
        }
        is.close();
        is = null;
        out.close();
        out = null;
    } catch (FileNotFoundException e) {
    } catch (IOException e) {
    } finally {
        try {
            if (is != null) {
                is.close();
                is = null;
            }
            if (out != null) {
                out.close();
                out = null;
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

From source file:com.spstudio.common.image.ImageUtils.java

/**
 * ,?byte// w  w  w.  ja va 2s .c  om
 *
 * @return
 */
public static void writeFile(byte[] bao) {
    File file = new File("d:/touxiang1.jpg");

    FileOutputStream fos = null;
    ByteArrayOutputStream baos = null;
    byte[] data = null;

    try {
        fos = new FileOutputStream(file);
        fos.write(bao);

        fos.flush();

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            fos.close();
        } catch (IOException ex) {
            Logger.getLogger(ImageUtils.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

From source file:Main.java

/**
 * Write a string value to the specified file.
 * /* w  w w.ja v a2s  . c om*/
 * @param filename The filename
 * @param value The value
 */
public static void writeValue(String filename, Boolean value) {
    FileOutputStream fos = null;
    String sEnvia;
    try {
        fos = new FileOutputStream(new File(filename), false);
        if (value)
            sEnvia = "1";
        else
            sEnvia = "0";
        fos.write(sEnvia.getBytes());
        fos.flush();
        // fos.getFD().sync();
    } catch (FileNotFoundException ex) {
        Log.w(TAG, "file " + filename + " not found: " + ex);
    } catch (SyncFailedException ex) {
        Log.w(TAG, "file " + filename + " sync failed: " + ex);
    } catch (IOException ex) {
        Log.w(TAG, "IOException trying to sync " + filename + ": " + ex);
    } catch (RuntimeException ex) {
        Log.w(TAG, "exception while syncing file: ", ex);
    } finally {
        if (fos != null) {
            try {
                Log.w(TAG_WRITE, "file " + filename + ": " + value);
                fos.close();
            } catch (IOException ex) {
                Log.w(TAG, "IOException while closing synced file: ", ex);
            } catch (RuntimeException ex) {
                Log.w(TAG, "exception while closing file: ", ex);
            }
        }
    }
}

From source file:com.cloudera.knittingboar.utils.Utils.java

/**
 * Ungzip an input file into an output file.
 * <p>/* www.j ava2 s  .c  om*/
 * The output file is created in the output folder, having the same name as
 * the input file, minus the '.gz' extension.
 * 
 * @param inputFile
 *          the input .gz file
 * @param outputDir
 *          the output directory file.
 * @throws IOException
 * @throws FileNotFoundException
 * 
 * @return The {@File} with the ungzipped content.
 */
private static File unGzip(final File inputFile, final File outputDir)
        throws FileNotFoundException, IOException {

    System.out.println(String.format("Ungzipping %s to dir %s.", inputFile.getAbsolutePath(),
            outputDir.getAbsolutePath()));

    final File outputFile = new File(outputDir,
            inputFile.getName().substring(0, inputFile.getName().length() - 3));

    final GZIPInputStream in = new GZIPInputStream(new FileInputStream(inputFile));
    final FileOutputStream out = new FileOutputStream(outputFile);

    for (int c = in.read(); c != -1; c = in.read()) {
        out.write(c);
    }

    in.close();
    out.close();

    return outputFile;
}

From source file:edu.ku.brc.util.AttachmentUtils.java

/**
 * @param attachmentLocation/*from   ww  w. jav a2s .com*/
 * @return
 */
public static boolean isAttachmentDirMounted(final File attachmentLocation) {
    String fullPath = "";
    String statsMsg = "The test to write to the AttachmentLocation [%s] %s.";
    try {
        fullPath = attachmentLocation.getCanonicalPath();

        if (attachmentLocation.exists()) {
            if (attachmentLocation.isDirectory()) {
                File tmpFile = new File(attachmentLocation.getAbsoluteFile() + File.separator
                        + System.currentTimeMillis() + System.getProperty("user.name"));
                //log.debug(String.format("Trying to write a file to AttachmentLocation [%s]", tmpFile.getCanonicalPath()));
                if (tmpFile.createNewFile()) {
                    // I don't think I need this anymore
                    FileOutputStream fos = FileUtils.openOutputStream(tmpFile);
                    fos.write(1);
                    fos.close();
                    tmpFile.delete();

                    //log.debug(String.format(statsMsg, fullPath, "succeeded"));

                    return true;

                } else {
                    log.error(String.format("The Attachment Location [%s] atachment file couldn't be created",
                            fullPath));
                }
            } else {
                log.error(String.format("The Attachment Location [%s] is not a directory.", fullPath));
            }
        } else {
            log.error(String.format("The Attachment Location [%s] doesn't exist.", fullPath));
        }

    } catch (Exception ex) {
        ex.printStackTrace();
    }

    log.debug(String.format(statsMsg, fullPath, "failed"));

    return false;
}

From source file:Main.java

public static void writeTo(Document document, File output) {
    try {//from www  .j a  v  a  2  s. c o  m
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(document);

        FileOutputStream outputstream = new FileOutputStream(output);
        StreamResult result = new StreamResult(outputstream);

        // Manually add xml declaration, to force a newline after it.
        String xmlDeclaration = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
        outputstream.write(xmlDeclaration.getBytes());

        // Remove whitespaces outside tags.
        // Essential to make sure the nodes are properly indented.
        XPath xPath = XPathFactory.newInstance().newXPath();
        NodeList nodeList = (NodeList) xPath.evaluate("//text()[normalize-space()='']", document,
                XPathConstants.NODESET);
        for (int i = 0; i < nodeList.getLength(); ++i) {
            Node node = nodeList.item(i);
            node.getParentNode().removeChild(node);
        }

        // Pretty-print options.
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        transformer.transform(source, result);

        outputstream.close();
    } catch (TransformerException | IOException | XPathExpressionException e) {
        System.out.println("Failed to write document file" + output.getPath() + ": " + e.toString());
    }
}