Example usage for java.io FileOutputStream close

List of usage examples for java.io FileOutputStream close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closes this file output stream and releases any system resources associated with this stream.

Usage

From source file:com.pentaho.repository.importexport.PDIImportUtil.java

/**
 * @return instance of {@link Document}, if xml is loaded successfully null in case any error occurred during loading
 *///from  w  w  w  .jav a  2  s .c  o  m
public static Document loadXMLFrom(InputStream is) {
    DocumentBuilderFactory factory;
    try {
        factory = XMLParserFactoryProducer.createSecureDocBuilderFactory();
    } catch (ParserConfigurationException e) {
        log.logError(e.getLocalizedMessage());
        factory = DocumentBuilderFactory.newInstance();
    }
    DocumentBuilder builder = null;
    Document doc = null;
    try {
        builder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException ex) {
        // ignore
    }
    try {
        File file = File.createTempFile("tempFile", "temp");
        file.deleteOnExit();
        FileOutputStream fous = new FileOutputStream(file);
        IOUtils.copy(is, fous);
        fous.flush();
        fous.close();
        doc = builder.parse(file);
    } catch (IOException | SAXException e) {
        log.logError(e.getLocalizedMessage());
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            // nothing to do here
        }
    }
    return doc;
}

From source file:id.co.nlp.MachineTranslation.Utils.Util.java

public static void serializing(String pathfile, Object object) throws FileNotFoundException, IOException {
    FileOutputStream fileOut = new FileOutputStream(pathfile);
    ObjectOutputStream out = new ObjectOutputStream(fileOut);
    out.writeObject(object);/* w  w  w  . j  av  a  2s  .c o  m*/
    out.close();
    fileOut.close();
}

From source file:Main.java

public static void writeFile(InputStream in, File file) throws IOException {
    if (!file.getParentFile().exists())
        file.getParentFile().mkdirs();//from w w  w . j  ava  2  s.  c o m

    if (file != null && file.exists())
        file.delete();

    FileOutputStream out = new FileOutputStream(file);
    byte[] buffer = new byte[1024 * 128];
    int len = -1;
    while ((len = in.read(buffer)) != -1) {
        out.write(buffer, 0, len);
    }
    out.flush();
    out.close();
    in.close();

}

From source file:Main.java

public static void writeBitmapFile(Context context, Bitmap bmp, String pathStr) {

    if (pathStr != null) {
        FileOutputStream fos = null;
        try {//from w w  w .  j a va 2  s.c  om
            fos = new FileOutputStream(pathStr);
            if (bmp != null && fos != null) {
                bmp.compress(CompressFormat.JPEG, 100, fos);
            }
            fos.close();
        } catch (Exception e) {

            e.printStackTrace();
        }
    }
}

From source file:com.oprisnik.semdroid.utils.FileUtils.java

public static void writeToFile(String filename, byte[] data) {
    FileOutputStream fos = null;
    try {//from  w  w  w. j a v a 2s .c  o m
        fos = new FileOutputStream(filename);
        fos.write(data);
        fos.close();

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(fos);
    }
}

From source file:Main.java

/**
 * Writes a simple bitmap to local storage. The image is a solid color with size 400x400
 *///from  w  ww.j  a  v a 2 s .  c  om
private static void writeSolidColorBitmapToFile(File file, int color) throws IOException {
    if (!file.exists()) {
        Bitmap bitmap = Bitmap.createBitmap(400, 400, Bitmap.Config.ARGB_8888);
        bitmap.eraseColor(color);

        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
        } finally {
            if (fos != null) {
                fos.close();
            }
        }
    }
}

From source file:Main.java

public static void copy(File src, File dst) throws IOException {
    FileInputStream inStream = new FileInputStream(src);
    FileOutputStream outStream = new FileOutputStream(dst);
    FileChannel inChannel = inStream.getChannel();
    FileChannel outChannel = outStream.getChannel();
    inChannel.transferTo(0, inChannel.size(), outChannel);
    inStream.close();//from ww w  .j  a  va2  s .co m
    outStream.close();
}

From source file:com.streamsets.pipeline.kafka.impl.TestSaslEnabledKafka.java

@BeforeClass
public static void beforeClass() throws Exception {
    testDir = new File("target", UUID.randomUUID().toString()).getAbsoluteFile();
    Assert.assertTrue(testDir.mkdirs());

    File kdcDir = new File(testDir, KDC);
    Assert.assertTrue(kdcDir.mkdirs());//  ww w.  j av  a  2  s . c o m
    keytabFile = new File(testDir, TEST_KEYTAB);

    miniKdc = new MiniKdc(MiniKdc.createConf(), kdcDir);
    miniKdc.start();
    miniKdc.createPrincipal(keytabFile, KAFKA_BROKER_PRINCIPAL, KAFKA_CLIENT_PRINCIPAL);

    jaasConfigFile = new File(testDir, KAFKA_JAAS_CONF);
    jaasConfigFile.createNewFile();
    jaasConfigFile.setReadable(true);
    String jaasConf = JAAS_CONF.replaceAll("keyTabFile", keytabFile.getAbsolutePath());
    FileOutputStream outputStream = new FileOutputStream(jaasConfigFile);
    IOUtils.write(jaasConf, outputStream);
    outputStream.close();

    plainTextPort = TestUtil.getFreePort();
    securePort = TestUtil.getFreePort();

    // reload configuration when getConfiguration is called next
    Configuration.setConfiguration(null);
    System.setProperty(JAVA_SECURITY_AUTH_LOGIN_CONFIG, jaasConfigFile.getAbsolutePath());

    TestSecureKafkaBase.beforeClass();
}

From source file:Main.java

public static void createFileFormInputStream(InputStream is, String path) {
    try {//  w  w  w  .ja  v  a  2s.  c o  m
        FileOutputStream fos = new FileOutputStream(path);
        byte[] buf = new byte[1376];
        while (is.read(buf) > 0) {
            fos.write(buf, 0, buf.length);
        }
        is.close();
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static boolean writeBmpToSDCard(Bitmap bmp, File file, int quality) {
    try {// w  ww .jav  a 2  s  . c  o m
        ByteArrayOutputStream baosm = new ByteArrayOutputStream();
        if (file.getPath().toLowerCase(Locale.getDefault()).endsWith(".png")) {
            bmp.compress(Bitmap.CompressFormat.PNG, quality, baosm);
        } else {
            bmp.compress(Bitmap.CompressFormat.JPEG, quality, baosm);
        }
        byte[] bts = baosm.toByteArray();

        if (file.exists()) {
            file.delete();
        }
        file.createNewFile();

        File tempFile = new File(file.getPath() + ".png");

        FileOutputStream fosm = new FileOutputStream(tempFile);
        BufferedOutputStream bos = new BufferedOutputStream(fosm);
        bos.write(bts);
        bos.flush();
        bos.close();
        fosm.close();

        tempFile.renameTo(file);

        return true;
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return false;
}