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.example.dlp.Redact.java

private static void redactImage(String filePath, Likelihood minLikelihood, List<InfoType> infoTypes,
        String outputPath) throws Exception {
    // [START dlp_redact_image]
    // Instantiate the DLP client
    try (DlpServiceClient dlpClient = DlpServiceClient.create()) {
        // The path to a local file to inspect. Can be a JPG or PNG image file.
        //  filePath = 'path/to/image.png'
        // detect file mime type, default to application/octet-stream
        String mimeType = URLConnection.guessContentTypeFromName(filePath);
        if (mimeType == null) {
            mimeType = MimetypesFileTypeMap.getDefaultFileTypeMap().getContentType(filePath);
        }//w w w .  j av  a 2  s .  c o  m
        if (mimeType == null) {
            mimeType = "application/octet-stream";
        }

        byte[] data = Files.readAllBytes(Paths.get(filePath));

        // The minimum likelihood required before redacting a match
        //  minLikelihood = 'LIKELIHOOD_UNSPECIFIED'

        // The infoTypes of information to redact
        // infoTypes = [{ name: 'EMAIL_ADDRESS' }, { name: 'PHONE_NUMBER' }]

        // The local path to save the resulting image to.
        // outputPath = 'result.png'

        InspectConfig inspectConfig = InspectConfig.newBuilder().addAllInfoTypes(infoTypes)
                .setMinLikelihood(minLikelihood).build();
        ContentItem contentItem = ContentItem.newBuilder().setType(mimeType).setData(ByteString.copyFrom(data))
                .build();

        List<ImageRedactionConfig> imageRedactionConfigs = new ArrayList<>();
        for (InfoType infoType : infoTypes) {
            // clear the specific info type if detected in the image
            // use .setRedactionColor to color detected info type without clearing
            ImageRedactionConfig imageRedactionConfig = ImageRedactionConfig.newBuilder().setInfoType(infoType)
                    .clearTarget().build();
            imageRedactionConfigs.add(imageRedactionConfig);
        }
        RedactContentRequest redactContentRequest = RedactContentRequest.newBuilder()
                .setInspectConfig(inspectConfig).addAllImageRedactionConfigs(imageRedactionConfigs)
                .addItems(contentItem).build();

        RedactContentResponse contentResponse = dlpClient.redactContent(redactContentRequest);
        for (ContentItem responseItem : contentResponse.getItemsList()) {
            // redacted image data
            ByteString redactedImageData = responseItem.getData();
            FileOutputStream outputStream = new FileOutputStream(outputPath);
            outputStream.write(redactedImageData.toByteArray());
            outputStream.close();
        }
        // [END dlp_redact_image]
    }
}

From source file:Main.java

/**
 * Copy file from oldPath to newPath/*  w  w w.  j a  va  2s .  c  o  m*/
 *
 * @param oldPath
 * @param newPath
 */
public static void copyFile(String oldPath, String newPath) {
    try {
        int byteread = 0;
        File oldfile = new File(oldPath);
        File newfile = new File(newPath);
        if (newfile.exists()) {
            newfile.delete();
        }
        newfile.createNewFile();
        if (oldfile.exists()) {
            FileInputStream inStream = new FileInputStream(oldPath);
            FileOutputStream outStream = new FileOutputStream(newPath);
            byte[] buffer = new byte[1024];
            while ((byteread = inStream.read(buffer)) > 0) {
                outStream.write(buffer, 0, byteread);
            }
            inStream.close();
            outStream.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void copyDB(Context context, String fileName) throws IOException {
    String filePath = context.getFilesDir().getAbsolutePath() + "/" + fileName;
    if (new File(filePath).exists()) {
        return;/*from   ww  w.  j  a  v  a  2s  .co  m*/
    }
    FileOutputStream fos = new FileOutputStream(new File(filePath));
    InputStream is = context.getResources().getAssets().open(fileName);
    byte[] buffer = new byte[1024 * 500];
    int count = 0;
    while ((count = is.read(buffer)) > 0) {
        fos.write(buffer, 0, count);
    }
    fos.close();
    is.close();
}

From source file:Main.java

public static boolean addFile(InputStream src, File dest) {
    try {//from w  w w.  j  a v  a2s .co m
        FileOutputStream out = new FileOutputStream(dest);

        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(src));
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            out.write((line + "\n").getBytes());
        }

        out.close();
        bufferedReader.close();

        return true;
    } catch (IOException e) {
    }
    return false;
}

From source file:in.co.sneh.model.CargaExcelRuralModel.java

public static boolean processFile(String path, FileItemStream item) {
    try {/*from   w w  w  . java  2 s .c o m*/
        File f = new File(path + File.separator + "exceles" + File.separator);
        if (!f.exists()) {
            f.mkdir();
        }
        File savedFile = new File(f.getAbsolutePath() + File.separator + item.getName());
        FileOutputStream fos = new FileOutputStream(savedFile);
        InputStream is = item.openStream();
        int x = 0;
        byte[] b = new byte[1024];
        while ((x = is.read(b)) != -1) {
            fos.write(b, 0, x);
        }
        fos.flush();
        fos.close();
        return true;
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
    return false;
}

From source file:Main.java

public static void writeBitmapToFile(String fileName, Bitmap bitmap) {
    FileOutputStream out = null;
    try {//from   w w  w.  j ava2 s .c  om
        out = new FileOutputStream(fileName);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (out != null) {
                out.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:main.java.com.aosa.util.AOSAReadProperties.java

/**
 * Description<code>Properties</code>      <br>
 * By mutou at 2012-1-12 ?05:58:16   <br>
 * boolean      <br>/*  www .  jav  a  2  s  . com*/
 * @param profileName
 *       Properties ??ClassPath
 * @param key
 *       ?? 
 * @param value
 *       ? value
 * @return
 * @throws
 */
public static boolean WriteValue(String profileName, String key, String value) {
    boolean isWrite = false;
    Properties properties = new Properties();
    InputStream input = AOSAReadProperties.class.getClassLoader().getResourceAsStream(profileName);
    try {
        properties.load(input);
        properties.setProperty(key, value);
        URL prourl = AOSAReadProperties.class.getClassLoader().getResource(profileName);
        String filepath = prourl.getFile();
        FileOutputStream fileOutputStream = new FileOutputStream(filepath);
        properties.store(fileOutputStream, "Custum shutDownTime config : conf.properties");
        fileOutputStream.flush();
        fileOutputStream.close();
        isWrite = true;
    } catch (FileNotFoundException e) {
        logger.debug("Properties");
        throw new AOSARuntimeException("Properties", e);
    } catch (IOException e) {
        logger.debug("PropertiesIO");
        throw new AOSARuntimeException("PropertiesIO", e);
    } finally {
        if (input != null) {
            try {
                input.close();
            } catch (IOException e) {
                logger.debug("IO?");
                throw new AOSARuntimeException("IO?", e);
            }
        }
    }
    return isWrite;
}

From source file:com.welocalize.dispatcherMW.client.Main.java

public static void downloadJob(String p_jobId, String p_securityCode) {
    File parent = new File("target/" + p_jobId);
    String url = getFunctinURL(TYPE_DOWNLOAD, p_securityCode, p_jobId);
    HttpClient httpClient = HttpClients.createDefault();
    HttpGet httpget = new HttpGet(url);
    try {/*w w w  .j a va  2 s.  c o m*/
        HttpResponse response = httpClient.execute(httpget);
        String fileName = response.getHeaders("Content-Disposition")[0].getValue().split("\"")[1];
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            if (!parent.exists())
                parent.mkdirs();
            FileOutputStream fos = new FileOutputStream(parent.getAbsolutePath() + "/" + fileName);
            entity.writeTo(fos);
            fos.close();
        }

        System.out.println("Download file:" + parent.getAbsolutePath() + "/" + fileName);
    } catch (Exception e) {
        System.out.println("Download XLF file error:\n" + e);
    } finally {
        httpget.releaseConnection();
    }
}

From source file:com.elit2.app.model.FileUpload.java

public static boolean proccessFile(String path, FileItemStream item) {
    try {//from w ww  .jav  a 2  s .  c o  m
        File f = new File(path + File.separator + "images");
        if (!f.exists()) {
            f.mkdir();
        }
        File savedFile = new File(f.getAbsolutePath() + File.separator + item.getName());
        FileOutputStream fos = new FileOutputStream(savedFile);
        InputStream is = item.openStream();

        int x = 0;
        byte[] b = new byte[1024];
        while ((x = is.read()) != -1) {
            fos.write(b, 0, x);
        }
        fos.flush();
        fos.close();
        return true;
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return false;
}