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:ch.ethz.inf.vs.android.g54.a4.util.SnapshotCache.java

public static void storeSnapshot(List<WifiReading> readings, String fileName, Context c) {

    String state = Environment.getExternalStorageState();

    if (Environment.MEDIA_MOUNTED.equals(state)) {
        // We can read and write the external storage
        try {//from w  ww. j  a  va2  s .  c o  m
            JSONArray json = readingsToJson(readings);
            File root = c.getExternalCacheDir();
            File jsonFile = new File(root, constructFileName(fileName));
            FileOutputStream out;
            out = new FileOutputStream(jsonFile);
            out.write(json.toString().getBytes());
            out.close();
            U.showToast(constructFileName(fileName));
            Log.d(TAG, "Successfully stored snapshot to SDCard");
        } catch (Exception e) {
            U.showToast("Could not save the snapshot on the SDCard.");
            Log.e(TAG, "Could not save the snapshot on the SDCard.", e);
        }
    } else {
        U.showToast("Cannot write to external storage.");
    }
}

From source file:edu.cwru.jpdg.JPDG.java

public static void writeGraph(Graph g, String path) {
    byte[] graph = g.Serialize().getBytes(Charset.forName("UTF-8"));

    FileOutputStream s = null;
    try {/*w ww  .  j  a  v  a2  s  .com*/
        s = new FileOutputStream(path);
        s.write(graph);
    } catch (IOException ex) {
        System.err.println(ex);
    } finally {
        try {
            s.close();
        } catch (Exception ex) {
        }
    }
}

From source file:Main.java

private static synchronized void logToFile(String tag, String msg) {
    try {//  www  .  j  av a  2 s  .  c om
        if (LOG_FILE == null) {
            LOG_FILE = new File(LOG_FILE_PATH);
            if (LOG_FILE.exists()) {
                LOG_FILE.delete();
            }
            LOG_FILE.createNewFile();
        }

        FileOutputStream outputStream = new FileOutputStream(LOG_FILE, true);

        StringBuilder builder = new StringBuilder();
        builder.append(tag).append("  :  ").append(msg).append("\n");

        outputStream.write(builder.toString().getBytes());
        outputStream.flush();
        outputStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.tingtingapps.securesms.crypto.PreKeyUtil.java

private static void setNextPreKeyId(Context context, int id) {
    try {//from  w w w.j a v a  2  s  .  c o m
        File nextFile = new File(getPreKeysDirectory(context), PreKeyIndex.FILE_NAME);
        FileOutputStream fout = new FileOutputStream(nextFile);
        fout.write(JsonUtils.toJson(new PreKeyIndex(id)).getBytes());
        fout.close();
    } catch (IOException e) {
        Log.w("PreKeyUtil", e);
    }
}

From source file:com.tingtingapps.securesms.crypto.PreKeyUtil.java

private static void setNextSignedPreKeyId(Context context, int id) {
    try {//  w  ww  .j  av a  2s  .c om
        File nextFile = new File(getSignedPreKeysDirectory(context), SignedPreKeyIndex.FILE_NAME);
        FileOutputStream fout = new FileOutputStream(nextFile);
        fout.write(JsonUtils.toJson(new SignedPreKeyIndex(id)).getBytes());
        fout.close();
    } catch (IOException e) {
        Log.w("PreKeyUtil", e);
    }
}

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);
        }//from  w  w  w .j  a v a 2 s  .  c  om
        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

private static void writeLong(FileOutputStream stream, long value) throws IOException {
    byte[] b = new byte[4];
    b[0] = (byte) (value & 0xff);
    b[1] = (byte) (value >> 8 & 0xff);
    b[2] = (byte) (value >> 16 & 0xff);
    b[3] = (byte) (value >> 24 & 0xff);
    stream.write(b);
}

From source file:Main.java

private static void writeDword(FileOutputStream stream, long value) throws IOException {
    byte[] b = new byte[4];
    b[0] = (byte) (value & 0xff);
    b[1] = (byte) (value >> 8 & 0xff);
    b[2] = (byte) (value >> 16 & 0xff);
    b[3] = (byte) (value >> 24 & 0xff);
    stream.write(b);
}

From source file:Main.java

public static void writeToInternalStorage(Context context, String fileName, String json) {

    FileOutputStream outputStream = null;
    try {/*from   w  w w .  j av a 2  s  .c  o m*/
        outputStream = context.openFileOutput(fileName, Context.MODE_PRIVATE);
        for (byte s : json.getBytes(StandardCharsets.UTF_8)) {
            outputStream.write(s);
        }
    } catch (Exception e) {
        Log.e(TAG, e.getMessage(), e);
    } finally {
        if (outputStream != null) {
            try {
                outputStream.close();
            } catch (IOException e) {
                Log.e(TAG, e.getMessage(), e);
            }
        }
    }
}

From source file:com.project.utilities.Utilities.java

public static void writeToFileViolationListJSON() {
    String violationJSONList = getRequest(Constants.API_VIOLATIONS);
    File currentConfig = new File(Constants.EXTERNAL_DIRECTORY + "/" + Constants.FILENAME_VIOLATION_JSON);
    try {//from w ww . ja v a2 s . co  m
        currentConfig.createNewFile();
        FileOutputStream fos = new FileOutputStream(currentConfig);
        fos.write(violationJSONList.getBytes());
        fos.close();
    } catch (IOException e) {
        Log.e("ERROR CREATING JSON FILE", e.getMessage().toString());
    }
}