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 CopyFile(String source, String destination) throws IOException {
    FileInputStream fin = new FileInputStream(source);
    FileOutputStream fout = new FileOutputStream(destination);

    byte[] b = new byte[1024 * 1024];
    int noOfBytes = 0;

    while ((noOfBytes = fin.read(b)) != -1) {
        fout.write(b, 0, noOfBytes);//from  w w w .j a  va  2s  . c o m
    }

    fin.close();
    fout.close();
}

From source file:gov.nih.nci.cabig.caaers.api.BlankFormGenerator.java

public static void testXMLGenaration() {
    Study s;/*from www. ja v  a 2 s  .c  om*/
    Epoch e;

    BlankFormGenerator g;
    g = new BlankFormGenerator();
    s = new LocalStudy();
    s.setShortTitle("ST");
    s.setLongTitle("LT");
    s.setId(55588);

    s.setIdentifiers(new ArrayList<Identifier>());
    s.getIdentifiers().add(new OrganizationAssignedIdentifier());
    s.getIdentifiers().get(0).setPrimaryIndicator(true);
    s.getIdentifiers().get(0).setValue("VALUE");

    e = new Epoch();
    e.setName("PT");
    e.setDescriptionText("DT");
    e.setId(88);

    SolicitedAdverseEvent sae = new SolicitedAdverseEvent();
    s.addEpoch(e);
    e.addArm(new Arm());
    List<SolicitedAdverseEvent> sael = new ArrayList<SolicitedAdverseEvent>();
    SolicitedAdverseEvent sae1 = new SolicitedAdverseEvent();
    sae1.setCtcterm(new CtcTerm());
    sae1.getCtcterm().setTerm("Nausea");
    sael.add(sae1);

    sae1 = new SolicitedAdverseEvent();
    sae1.setCtcterm(new CtcTerm());
    sae1.getCtcterm().setTerm("Bone Pain");
    sael.add(sae1);

    e.getArms().get(0).setSolicitedAdverseEvents(sael);
    try {
        String xml = g.serialize(s, e);
        FileOutputStream out = new FileOutputStream(XMLFile);
        out.write(xml.getBytes());
        out.close();
    } catch (Exception e1) {
        e1.printStackTrace();
    }
}

From source file:Main.java

public static void unZip(String zipFile, String outputFolder) {
    byte[] buffer = new byte[BUFFER_SIZE];

    try {//  w  w  w  . j a  va 2 s .  co m
        File folder = new File(outputFolder);
        if (!folder.exists()) {
            folder.mkdir();
        }

        ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
        ZipEntry ze = zis.getNextEntry();

        while (ze != null) {
            String fileName = ze.getName();
            File newFile = new File(outputFolder + File.separator + fileName);

            System.out.println("file unzip : " + newFile.getAbsoluteFile());
            if (ze.isDirectory()) {
                newFile.mkdirs();
            } else {
                FileOutputStream fos = new FileOutputStream(newFile);
                int len;
                while ((len = zis.read(buffer)) > 0) {
                    fos.write(buffer, 0, len);
                }
                fos.close();
            }
            ze = zis.getNextEntry();
        }
        zis.closeEntry();
        zis.close();
        System.out.println("Done");
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static String saveToInternalStorage(Context context, Bitmap bitmapImage) {
    ContextWrapper cw = new ContextWrapper(context);
    // path to /data/data/yourapp/app_data/imageDir
    File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
    // Create imageDir
    File mypath = new File(directory, "gravatar.jpg");

    FileOutputStream fos = null;
    try {/*from  w w w.  j  a  va 2  s  . c  om*/
        fos = new FileOutputStream(mypath);
        // Use the compress method on the BitMap object to write image to the OutputStream
        bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
        fos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return directory.getAbsolutePath();
}

From source file:Main.java

public static void unzip(final InputStream input, final File destFolder) {
    try {/*from   w  w  w .j  a v  a2s  .  c o  m*/
        byte[] buffer = new byte[4096];
        int read;
        ZipInputStream is = new ZipInputStream(input);
        ZipEntry entry;
        while ((entry = is.getNextEntry()) != null) {
            if (!entry.isDirectory()) {
                String fileName = entry.getName();
                File fileFolder = destFolder;
                int lastSep = entry.getName().lastIndexOf(File.separatorChar);
                if (lastSep != -1) {
                    String dirPath = fileName.substring(0, lastSep);
                    fileFolder = new File(fileFolder, dirPath);
                    fileName = fileName.substring(lastSep + 1);
                }
                fileFolder.mkdirs();
                File file = new File(fileFolder, fileName);
                FileOutputStream os = new FileOutputStream(file);
                while ((read = is.read(buffer)) != -1) {
                    os.write(buffer, 0, read);
                }
                os.flush();
                os.close();
            }
        }
        is.close();
    } catch (Exception ex) {
        throw new RuntimeException("Cannot unzip stream to " + destFolder.getAbsolutePath(), ex);
    }
}

From source file:core.HttpClient.java

/**
 * downloads the latest file//from w  w  w. ja va 2  s.  c  o  m
 */
public static String downloadUpdatedFile() {
    DefaultHttpClient httpclient = new DefaultHttpClient();
    try {
        HttpResponse response;
        HttpEntity entity;

        HttpPost httpost = new HttpPost(QUICKTODO_DOWNLOAD_LINK);
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("download_update", "true"));

        response = httpclient.execute(httpost);

        entity = response.getEntity();
        if (entity != null) {
            FileOutputStream fos = new FileOutputStream("QuickToDoUpdate.zip");
            entity.writeTo(fos);
            fos.close();
            return "QuickToDoUpdate.zip";
        }
        return null;

    } catch (ClientProtocolException e) {
        e.printStackTrace();
        return null;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    } finally {
        httpclient.getConnectionManager().shutdown();
    }
}

From source file:com.ikon.util.FileUtils.java

/**
 * Copy InputStream to File./*from  ww  w  . ja  v  a2  s.  co  m*/
 */
public static void copy(InputStream input, File output) throws IOException {
    FileOutputStream fos = new FileOutputStream(output);
    IOUtils.copy(input, fos);
    fos.flush();
    fos.close();
}

From source file:com.ikon.util.FileUtils.java

/**
 * Copy Reader to File.//from  w w w. j a  va 2s.c om
 */
public static void copy(Reader input, File output) throws IOException {
    FileOutputStream fos = new FileOutputStream(output);
    IOUtils.copy(input, fos);
    fos.flush();
    fos.close();
}

From source file:Main.java

private static void decompressGzipFile(String gzipFile) {
    try {// w  w w.  j a  v  a 2s .c  o m
        File newFile = new File(Environment.getExternalStorageDirectory() + File.separator + "FlockLoad");
        FileInputStream fis = new FileInputStream(gzipFile);
        GZIPInputStream gis = new GZIPInputStream(fis);
        FileOutputStream fos = new FileOutputStream(newFile);
        byte[] buffer = new byte[1024];
        int len;
        while ((len = gis.read(buffer)) != -1) {
            fos.write(buffer, 0, len);
        }
        //close resources
        fos.close();
        gis.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:org.radargun.reporting.BarPlotGenerator.java

/**
 * @param operation Name of the plotted operation
 * @param ranges ranges[0] = min, ranges[ranges.length - 1] = max
 * @param counts counts[i] is number of entries with value >= ranges[i - 1] and < ranges[i]
 * @param reportDir//  ww  w . ja va2  s  . co m
 * @param filename
 * @throws IOException
 */
public static void generate(String operation, long[] ranges, long[] counts, String reportDir, String filename)
        throws IOException {
    XYSeries series = new XYSeries(operation + " response times");
    long totalCount = 0;
    for (long count : counts) {
        totalCount += count;
    }
    double left = Math.log10(ranges[0]);
    double right = Math.log10(ranges[ranges.length - 1]);

    for (int i = 0; i < counts.length; i++) {
        series.add(Math.log10(ranges[i]), (double) counts[i] / totalCount);
    }
    series.add(right, 0d);
    XYDataset dataset = new XYSeriesCollection(series);
    JFreeChart chart = ChartFactory.createXYStepAreaChart(operation + " response time histogram",
            "Response time", "Percentage", dataset, PlotOrientation.VERTICAL, false, false, false);
    XYPlot plot = (XYPlot) chart.getPlot();
    NumberAxis d = (NumberAxis) plot.getDomainAxis();
    d.setRange(left, right);
    d.setStandardTickUnits(new HistoTickUnitSource());
    plot.setDomainAxis(d);
    FileOutputStream output = new FileOutputStream(new File(reportDir + File.separator + filename));
    ChartUtilities.writeChartAsPNG(output, chart, 1024, 768);
    output.close();
}