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:Main.java

public static void logScanReport(Context c, String[] logs) {
    String dir = c.getExternalFilesDir(null).getParent();
    try {//from  w w w  .j a v  a2  s.  c  o  m
        File file = new File(dir + "/last_scan.log");
        boolean append = false;
        // if file doesnt exists, then create it
        if (!file.exists()) {
            file.createNewFile();
        } else {
            /*
            if(file.length() >= 2097152){
               // set log file max size to 2mb
               append = false;
            }
             */
        }
        FileOutputStream fos = new FileOutputStream(file, append);

        for (String log : logs) {
            fos.write((log + "\n\n").getBytes());
        }
        fos.close();
    } catch (IOException ioe) {
        Log.e("AndroidUtils", "An exception has occured in logScanReport!");
        ioe.printStackTrace();
    }
}

From source file:Main.java

public static <T> void writeSerializedObject(Context context, String fileName, T objectToWrite)
        throws IOException {
    FileOutputStream fileOut = context.openFileOutput(fileName, Context.MODE_PRIVATE);
    ObjectOutputStream out = new ObjectOutputStream(fileOut);
    out.writeObject(objectToWrite);/*from   w w  w .j a v a  2 s  .  c om*/
    out.close();
    fileOut.close();
}

From source file:Main.java

public static void logProgressReport(Context c, String[] logs) {
    String dir = c.getExternalFilesDir(null).getParent();
    try {//from  ww  w.  ja va  2 s  .co  m
        File file = new File(dir + "/sf_reports.log");
        boolean append = false;
        // if file doesnt exists, then create it
        if (!file.exists()) {
            file.createNewFile();
        } else {
            /*
            if(file.length() >= 2097152){
               // set log file max size to 2mb
               append = false;
            }
             */

        }
        FileOutputStream fos = new FileOutputStream(file, append);

        for (String log : logs) {
            fos.write((log + "\n\n").getBytes());
        }
        fos.close();
    } catch (IOException ioe) {
        Log.e("AndroidUtils", "An exception has occured in logProgressReport!");
        ioe.printStackTrace();
    }
}

From source file:com.shmsoft.dmass.print.Html2Pdf.java

/**
 * Bad rendering, perhaps used only for Windows
 *///from   w w  w . j  ava 2 s  .  c  om
@SuppressWarnings({ "rawtypes", "unchecked" })
private static void convertHtml2Pdf(Reader htmlReader, String outputFile) throws Exception {
    Document pdfDocument = new Document();

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PdfWriter.getInstance(pdfDocument, baos);
    pdfDocument.open();
    StyleSheet styles = new StyleSheet();
    styles.loadTagStyle("body", "font", "Times New Roman");

    ImageProvider imageProvider = new ImageProvider() {

        @Override
        public Image getImage(String src, HashMap arg1, ChainedProperties arg2, DocListener arg3) {

            try {
                Image image = Image.getInstance(IOUtils.toByteArray(
                        getClass().getClassLoader().getResourceAsStream(ParameterProcessing.NO_IMAGE_FILE)));
                return image;
            } catch (Exception e) {
                System.out.println("Problem with html->pdf imaging, image provider fault");
            }

            return null;
        }

    };

    HashMap interfaceProps = new HashMap();
    interfaceProps.put("img_provider", imageProvider);

    ArrayList arrayElementList = HTMLWorker.parseToList(htmlReader, styles, interfaceProps);
    for (int i = 0; i < arrayElementList.size(); ++i) {
        Element e = (Element) arrayElementList.get(i);
        pdfDocument.add(e);
    }
    pdfDocument.close();
    byte[] bs = baos.toByteArray();
    File pdfFile = new File(outputFile);
    FileOutputStream out = new FileOutputStream(pdfFile);
    out.write(bs);
    out.close();
}

From source file:Main.java

public static void putImageInCache(File file, Bitmap bmp) {
    try {//from  w  w w.j  ava 2s. c om
        file.createNewFile();
        FileOutputStream out = new FileOutputStream(file);
        bmp.compress(Bitmap.CompressFormat.PNG, 80, out);
        Log.d("CACHE", "FILE : " + out.toString());
        out.flush();
        out.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Saves a vcard string to an internal new vcf file.
 * @param vcard the string to save// w  w w.  j ava  2s .  c  o  m
 * @param filename the filename of the vcf
 * @param context the context used to open streams.
 */
public static void saveToDisk(String vcard, String filename, Context context) {
    if (TextUtils.isEmpty(vcard) || TextUtils.isEmpty(filename) || context == null) {
        return;
    }
    String path = context.getFilesDir().getAbsolutePath() + File.separator + "peer_profiles";
    File peerProfilesFile = new File(path);
    if (!peerProfilesFile.exists())
        peerProfilesFile.mkdirs();
    FileOutputStream outputStream;
    try {
        outputStream = new FileOutputStream(path + "/" + filename);
        outputStream.write(vcard.getBytes());
        outputStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.axelor.apps.tool.net.MyFtp.java

public static void getDataFiles(String server, String username, String password, String folder,
        String destinationFolder, Calendar start, Calendar end) {

    try {/*  w ww  . j  ava  2 s .  c o m*/

        // Connect and logon to FTP Server
        FTPClient ftp = new FTPClient();
        ftp.connect(server);
        ftp.login(username, password);

        // List the files in the directory
        ftp.changeWorkingDirectory(folder);
        FTPFile[] files = ftp.listFiles();

        for (int i = 0; i < files.length; i++) {

            Date fileDate = files[i].getTimestamp().getTime();
            if (fileDate.compareTo(start.getTime()) >= 0 && fileDate.compareTo(end.getTime()) <= 0) {

                // Download a file from the FTP Server
                File file = new File(destinationFolder + File.separator + files[i].getName());
                FileOutputStream fos = new FileOutputStream(file);
                ftp.retrieveFile(files[i].getName(), fos);
                fos.close();
                file.setLastModified(fileDate.getTime());
            }
        }

        // Logout from the FTP Server and disconnect
        ftp.logout();
        ftp.disconnect();

    } catch (Exception e) {

        LOG.error(e.getMessage());

    }
}

From source file:common.Util.java

public static boolean writeFile(File file, byte[] data) {
    try {//from ww w.jav  a  2  s  .c om
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(data, 0, data.length);
        fos.flush();
        fos.close();
        return true;
    } catch (Exception e) {
        System.out.println("writeFile failed with " + e);
        return false;
    }
}

From source file:Main.java

public static void logToFile(String filename, String log) {
    File myFile = new File("/sdcard/SAGETablet/" + filename + ".txt");
    try {//from  www  .  j a va 2 s.  c  o  m
        if (!myFile.exists()) {
            myFile.createNewFile();
        }
        FileOutputStream fOut = new FileOutputStream(myFile);
        OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
        myOutWriter.append(log);
        myOutWriter.close();
        fOut.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void saveFile(File f, byte[] content) throws IOException {
    FileOutputStream fos = new FileOutputStream(f);
    try {/*from   ww  w .ja v  a2 s .  c  o  m*/
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        bos.write(content);
        bos.flush();
    } finally {
        fos.close();
    }
}