Example usage for java.io PrintWriter close

List of usage examples for java.io PrintWriter close

Introduction

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

Prototype

public void close() 

Source Link

Document

Closes the stream and releases any system resources associated with it.

Usage

From source file:Main.java

private static void writeFile(String filePath, String content) throws IOException {
    FileWriter fw = new FileWriter(filePath);
    PrintWriter out = new PrintWriter(fw);
    out.write(content);//from   w w w . j  a  v a 2 s  .c om
    out.println();
    fw.close();
    out.close();
}

From source file:Main.java

public static void writeToFile(String fileName, String xml) throws IOException {
    PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(fileName, true), 1024));
    out.println(xml);//from  www  .  jav a  2 s  . c o m
    out.flush();
    out.close();
}

From source file:com.jmu.util.ResponseJsonUtils.java

public static void writeJSON(PrintWriter wirter, String json) {
    wirter.print(json);
    wirter.flush();
    wirter.close();
}

From source file:Main.java

/**
 * Print all running threads/*  w w w  .j av  a2 s . co m*/
 */
public static void printRunningThreads() {
    // Get the thread listing as a string using a StringWriter stream
    StringWriter stringWriter = new StringWriter();
    PrintWriter out = new PrintWriter(stringWriter);
    listAllThreads(out);
    out.close();
    String threadListing = stringWriter.toString();
    System.out.println(threadListing);
}

From source file:Main.java

/**
 * Returns a stack trace of the {@code Throwable} as a {@code String}.
 * //from   w  ww  . ja va 2  s. co  m
 * @param throwable
 *          The throwable for which to create the stack trace.
 * @param expectNull
 *          True if null should be returned when {@code throwable} is null or
 *          false to return "" when {@code throwable} is null
 * @return null if {@code throwable} is null and {@code expectNull} is true,
 *         "" if {@code throwable} is null and {@code expectNull} is false,
 *         otherwise the stack trace for {@code throwable}
 */
public static String stackTraceToString(final Throwable throwable, final boolean expectNull) {
    if (throwable == null) {
        if (expectNull == true) {
            return null;
        }

        return "";
    }

    StringWriter stringWriter = new StringWriter();
    PrintWriter printWriter = new PrintWriter(stringWriter);
    throwable.printStackTrace(printWriter);
    printWriter.close();
    return stringWriter.toString();
}

From source file:edu.usf.cutr.obascs.io.FileUtil.java

public static void writeToFile(String text, String path) throws FileNotFoundException {
    File file = new File(path);
    PrintWriter printWriter = new PrintWriter(file);
    printWriter.println(text);//from ww w .  j av a  2s  . co  m
    printWriter.close();
}

From source file:Main.java

public static Boolean writeToSDFile(String directory, String file_name, String text) {

    // Find the root of the external storage.
    // See/*from  w w w .ja v  a 2 s  .  c o m*/
    // http://developer.android.com/guide/topics/data/data-storage.html#filesExternal

    File root = Environment.getExternalStorageDirectory();

    // See
    // http://stackoverflow.com/questions/3551821/android-write-to-sd-card-folder

    File dir = new File(root.getAbsolutePath() + "/" + directory);
    dir.mkdirs();
    File file = new File(dir, file_name);

    try {
        FileOutputStream f = new FileOutputStream(file);
        PrintWriter pw = new PrintWriter(f);
        pw.println(text);
        pw.flush();
        pw.close();
        f.close();
        // Log.v(TAG, "file written to sd card");
        return true;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        // Log.i(TAG, "******* File not found. Did you" +
        // " add a WRITE_EXTERNAL_STORAGE permission to the manifest?");
        return false;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
}

From source file:minij.codeemission.CodeEmitter.java

public static String emitCode(Configuration config, List<Fragment<List<Assem>>> assemFragments,
        MachineSpecifics machineSpecifics) throws CodeEmitterException {

    try {//from ww  w . j a v a 2s .  com
        String assembly = machineSpecifics.printAssembly(assemFragments);
        ;

        if (config.codeEmission) {
            new File(FilenameUtils.getPath(config.outputFile)).mkdirs();
            PrintWriter out = new PrintWriter(config.outputFile);
            out.print(assembly);
            out.close();
        }

        Logger.logVerbosely("Successfully generated assembly");

        if (config.printAssembly) {
            Logger.log(assembly);
        }

        return assembly;
    } catch (Exception e) {
        throw new CodeEmitterException("Failed to generate assembly", e);
    }
}

From source file:com.jmu.util.ResponseJsonUtils.java

public static void writeJSON(PrintWriter wirter, AjaxResponse json) {
    wirter.print(JSON.toJSONString(json));
    wirter.flush();//  w  ww  . j  a va2 s. co m
    wirter.close();
}

From source file:at.uni_salzburg.cs.ckgroup.cscpp.mapper.registry.RegistryPersistence.java

@SuppressWarnings("unchecked")
public static void storeRegistry(File registryFile, Map<String, IRegistrationData> registrationData)
        throws IOException {
    synchronized (lock) {
        JSONArray obj = new JSONArray();
        for (Entry<String, IRegistrationData> entry : registrationData.entrySet()) {
            IRegistrationData rd = entry.getValue();
            obj.add(rd);/*from   w ww.j  ava  2 s  . c  o m*/
        }

        PrintWriter pw = new PrintWriter(registryFile);
        pw.print(obj.toString());
        pw.close();
        LOG.info("Registry successfully saved to file " + registryFile);
    }
}