Example usage for java.io OutputStreamWriter OutputStreamWriter

List of usage examples for java.io OutputStreamWriter OutputStreamWriter

Introduction

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

Prototype

public OutputStreamWriter(OutputStream out) 

Source Link

Document

Creates an OutputStreamWriter that uses the default character encoding.

Usage

From source file:Main.java

public synchronized static void writeXmlToFile(Document document, File outputFile)
        throws TransformerFactoryConfigurationError, TransformerException, IOException {
    if (document != null && outputFile != null) {
        FileOutputStream fos = new FileOutputStream(outputFile);
        OutputStreamWriter writer = new OutputStreamWriter(fos);
        writeXml(document, writer, null);
        writer.close();//from   www . j  a  v a2  s. c om
    }
}

From source file:Main.java

/**
 * Method - saveMessage/*from w  w  w.  j av  a  2  s  .  c  o  m*/
 * 
 * Description - Saves messages to the file by either appending or rewriting, used by any activity that needs to save the messages
 * 
 * @param ctx - Context of the Application that called the function
 * @param message - The messages to save to the file, most often a long String of several files
 * @param mode - Very likely either Context.MODE_APPEND or Context.MODE_PRIVATE for appending or rewriting respectively
 */

public static void saveMessage(Context ctx, String message, int mode) {
    try {

        // Creating objects to write to the file
        FileOutputStream fos = ctx.openFileOutput("messages.dat", mode);
        OutputStreamWriter osw = new OutputStreamWriter(fos);

        osw.write(message); // Writing to the file
        osw.flush(); // Making sure all characters have been written

        // Closing objects after writing has finished
        osw.close();
        fos.close();

    } catch (FileNotFoundException e) {
        return;
    } catch (IOException e) {
        return;
    }
}

From source file:Main.java

public static Uri getSMSLogs(ContentResolver cr, Uri internal, Context context, String timeStamp) {
    String[] smsLogArray = new String[2];
    Uri uri = Uri.parse("content://sms/inbox");
    Cursor cur = cr.query(uri, null, null, null, null);
    //Cursor cur= cr.query(uri, smsLogArray, null ,null,null);
    FileOutputStream fOut = null;

    try {//w  w  w.  j a  va  2  s . c o  m
        fOut = context.openFileOutput("sms_logs_" + timeStamp + ".txt", Context.MODE_PRIVATE);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    OutputStreamWriter osw = new OutputStreamWriter(fOut);

    while (cur.moveToNext()) {
        smsLogArray[0] = cur.getString(cur.getColumnIndexOrThrow("address")).toString();
        smsLogArray[1] = cur.getString(cur.getColumnIndexOrThrow("body")).toString();

        writeToOutputStreamArray(smsLogArray, osw);
    }

    try {
        osw.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return internal;
}

From source file:Main.java

public static Uri getAllContacts(ContentResolver cr, Uri internal, Context context, String timeStamp) {

    String[] contactsArray = new String[2];
    Uri contactsUri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;

    Cursor cur = cr.query(contactsUri, null, null, null, null);

    FileOutputStream fOut = null;
    try {/* w  w w  . jav a  2s. c  o  m*/
        fOut = context.openFileOutput("contacts_" + timeStamp + ".txt", Context.MODE_PRIVATE);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    OutputStreamWriter osw = new OutputStreamWriter(fOut);

    while (cur.moveToNext()) {
        contactsArray[0] = cur
                .getString(cur.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME))
                .toString();
        contactsArray[1] = cur
                .getString(cur.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));

        writeToOutputStreamArray(contactsArray, osw);
    }

    try {
        osw.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return internal;
}

From source file:Main.java

public static File writeLog(String root, String filename) {
    StringBuilder log = new StringBuilder();
    try {// w ww  .  jav a  2s  . co m
        Process process = Runtime.getRuntime().exec("logcat -d");
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            log.append(line).append("\n");
        }
    } catch (IOException e) {
        log.append(e.toString());
    }

    File file = new File(root, filename);

    try {
        FileOutputStream fOut = new FileOutputStream(file);
        OutputStreamWriter osw = new OutputStreamWriter(fOut);

        // Write the string to the file
        osw.write(log.toString());
        osw.flush();
        osw.close();

    } catch (Exception e) {
        Log.e(TAG, String.format("Failed to write log to [%s]", file), e);
    }
    return file;
}

From source file:Main.java

/**
 * Use a temporary file to obtain the name of the default system encoding
 * @return name of default system encoding, or null if write failed
 *///  w ww  .  j ava 2 s . c  o m
private static String determineSystemEncoding() {
    File tempFile = null;
    String encoding = null;
    try {
        tempFile = File.createTempFile("gpsprune", null);
        OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(tempFile));
        encoding = getEncoding(writer);
        writer.close();
    } catch (IOException e) {
    } // value stays null
      // Delete temp file
    if (tempFile != null && tempFile.exists()) {
        if (!tempFile.delete()) {
            System.err.println("Cannot delete temp file: " + tempFile.getAbsolutePath());
        }
    }
    // If writing failed (eg permissions) then just ask system for default
    if (encoding == null)
        encoding = Charset.defaultCharset().name();
    return encoding;
}

From source file:Main.java

/**
 * Print a DOM tree to an output stream or if there is an exception while doing so, print the
 * stack trace./*from  w ww  .j a  va2s.co m*/
 *
 * @param dom
 * @param os
 */
public static void printDom(Node dom, OutputStream os) {
    Transformer trans;
    PrintWriter w = new PrintWriter(os);
    try {
        TransformerFactory fact = TransformerFactory.newInstance();
        trans = fact.newTransformer();
        trans.transform(new DOMSource(dom), new StreamResult(new OutputStreamWriter(os)));
    } catch (TransformerException e) {
        w.println("An error ocurred while transforming the given DOM:");
        e.printStackTrace(w);
    }
}

From source file:Main.java

public static void printXml(Document response, OutputStream out)
        throws UnsupportedEncodingException, TransformerException {
    printXml(response, new OutputStreamWriter(out));
}

From source file:Main.java

public static void handleClientRequest(Socket socket) {
    try {//from www.  j  a v  a 2  s .c om
        BufferedReader socketReader = null;
        BufferedWriter socketWriter = null;
        socketReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        socketWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));

        String inMsg = null;
        while ((inMsg = socketReader.readLine()) != null) {
            System.out.println("Received from  client: " + inMsg);

            String outMsg = inMsg;
            socketWriter.write(outMsg);
            socketWriter.write("\n");
            socketWriter.flush();
        }
        socket.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:Main.java

public static Uri getAllCallLogs(ContentResolver cr, Uri internal, Context context, String timeStamp) {
    SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yy HH:mm");
    String[] callLogArray = new String[3];
    String strOrder = android.provider.CallLog.Calls.DATE + " DESC";
    Uri callUri = Uri.parse("content://call_log/calls");
    Cursor cur = cr.query(callUri, null, null, null, strOrder);

    FileOutputStream fOut = null;
    try {//from ww w  . ja v  a 2 s .  c  o  m
        fOut = context.openFileOutput("call_logs_" + timeStamp + ".txt", Context.MODE_PRIVATE);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    OutputStreamWriter osw = new OutputStreamWriter(fOut);

    while (cur.moveToNext()) {
        callLogArray[0] = cur.getString(cur.getColumnIndex(android.provider.CallLog.Calls.NUMBER));
        callLogArray[1] = cur.getString(cur.getColumnIndex(android.provider.CallLog.Calls.CACHED_NAME));

        int thirdIndex = cur.getColumnIndex(android.provider.CallLog.Calls.DATE);
        long seconds = cur.getLong(thirdIndex);
        String dateString = formatter.format(new Date(seconds));
        callLogArray[2] = dateString;

        writeToOutputStreamArray(callLogArray, osw);
    }

    try {
        osw.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return internal;
}