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

/**
 * This method get string and create local file with that string
 * @param st/* w  ww. ja v  a2 s . co m*/
 * @param fileName
 */
private static void writeFile(String st, String fileName) {
    FileOutputStream fos = null;
    PrintWriter pw = null;

    try {
        File rootDir = new File("/storage/emulated/0/dbSuperZol/");

        fos = new FileOutputStream(new File(rootDir, fileName));
        pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(fos)));

        pw.println(st);

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (pw != null) {
            pw.close();
        }
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

From source file:gda.device.detector.mythen.data.MythenDataFileUtils.java

/**
 * Saves the given processed data to the specified file.
 * /*from w  ww  . j  a  va2s  .co m*/
 * @param data
 *            the processed data - an array of (angle, count, error) arrays
 * @param file
 *            the file in which to save the data
 */
public static void saveProcessedDataFile(double[][] data, String file) throws IOException {
    PrintWriter pw = new PrintWriter(file);
    for (double[] point : data) {
        pw.printf("%f %f %f%n", point[0], point[1], point[2]);
    }
    pw.close();
}

From source file:com.alvermont.terraj.fracplanet.io.ColourFile.java

/**
 * Write a list of colours to a file that can be read by the
 * <code>readFile</code> method of this class.
 *
 * @param colours The list of colours to be written
 * @param target The file object indicating where the data is to be written
 * @throws java.io.IOException If there is an error opening or writing to
 * the file etc./*from   w  w w  .  ja  v a 2  s .co m*/
 */
public static void writeFile(List<FloatRGBA> colours, File target) throws IOException {
    final PrintWriter pw = new PrintWriter(new FileOutputStream(target));

    try {
        for (int c = 0; c < colours.size(); ++c) {
            final FloatRGBA col = colours.get(c);

            pw.println(col.getR() + "," + col.getG() + "," + col.getB());
        }
    } finally {
        pw.close();
    }
}

From source file:com.voa.weixin.utils.HttpUtils.java

public static void write(HttpServletResponse response, String message) throws IOException {
    PrintWriter writer = null;
    try {//from   w  w w.j av  a 2s.  c om
        response.setHeader("Charset", "UTF-8");
        response.setCharacterEncoding("UTF-8");
        writer = response.getWriter();

        writer.write(message);
    } finally {
        if (writer != null)
            writer.close();
    }
}

From source file:cpd3314.buildit12.CPD3314BuildIt12.java

/**
 * Build a sample method that saves a handful of car instances as Serialized
 * objects, and as JSON objects./*from  w  ww . j  a  va2  s.c  o  m*/
 */
public static void doBuildIt2Output() {
    Car[] cars = { new Car("Honda", "Civic", 2001), new Car("Buick", "LeSabre", 2003),
            new Car("Toyota", "Camry", 2005) };

    // Saves as Serialized Objects
    try {
        FileOutputStream objFile = new FileOutputStream("cars.obj");
        ObjectOutputStream objStream = new ObjectOutputStream(objFile);

        for (Car car : cars) {
            objStream.writeObject(car);
        }

        objStream.close();
        objFile.close();
    } catch (IOException ex) {
        Logger.getLogger(CPD3314BuildIt12.class.getName()).log(Level.SEVERE, null, ex);
    }

    // Saves as JSONObject
    try {
        PrintWriter jsonStream = new PrintWriter("cars.json");

        JSONArray arr = new JSONArray();
        for (Car car : cars) {
            arr.add(car.toJSON());
        }

        JSONObject json = new JSONObject();
        json.put("cars", arr);

        jsonStream.println(json.toJSONString());

        jsonStream.close();
    } catch (IOException ex) {
        Logger.getLogger(CPD3314BuildIt12.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:edu.vassar.cs.cmpu331.tvi.Main.java

private static void renumber(String filename) throws IOException {
    Path file = Paths.get(filename);
    if (Files.notExists(file)) {
        System.out.println("File not found: " + filename);
        return;//from  ww w  .  j a  v a 2  s  .  co m
    }
    System.out.println("Renumbering " + filename);
    Function<String, String> trim = s -> {
        int i = s.indexOf(':');
        if (i > 0) {
            return s.substring(i + 1).trim();
        }
        return s.trim();
    };
    List<String> list = Files.lines(file).filter(line -> !line.startsWith("CODE")).map(line -> trim.apply(line))
            .collect(Collectors.toList());
    int size = list.size();
    PrintWriter writer = new PrintWriter(Files.newBufferedWriter(file));
    //      PrintStream writer = System.out;
    writer.println("CODE");
    IntStream.range(0, size).forEach(index -> {
        String line = list.get(index);

        writer.println(index + ": " + line);
    });
    writer.close();
    System.out.println("Done.");
}

From source file:Main.java

public static String toString(Element element) {
    if (element == null)
        return "null";

    Source source = new DOMSource(element);

    StringWriter stringWriter = new StringWriter();
    PrintWriter printWriter = new PrintWriter(stringWriter);
    Result result = new StreamResult(printWriter);

    try {/*from w ww  .  j  av  a 2  s.  c  o m*/
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.transform(source, result);
    } catch (Exception e) {
        throw new RuntimeException("couldn't write element '" + element.getTagName() + "' to string", e);
    }

    printWriter.close();

    return stringWriter.toString();
}

From source file:it.tidalwave.imageio.test.ImageReaderTestSupport.java

/*******************************************************************************************************************
 *
 *
 ******************************************************************************************************************/
public static void dumpRasterAsText(final @Nonnull Raster raster, final @Nonnull File file)
        throws FileNotFoundException {
    file.getParentFile().mkdir();/*from w  w  w  . j a  v  a2  s . c  o  m*/
    logger.info("***************** Writing %s...", file.getAbsolutePath());
    final PrintWriter pw = new PrintWriter(file);
    dumpRasterAsText(raster, pw);
    pw.close();
}

From source file:mobile.tiis.appv2.helpers.Utils.java

public static void writeNetworkLogFileOnSD(String sBody) {
    try {/*from   w ww .  j a  v  a 2  s .  com*/
        File root = new File(Environment.getExternalStorageDirectory(), "TIIS");
        if (!root.exists()) {
            root.mkdirs();
        }
        File gpxfile = new File(root, "network_log_file.txt");

        PrintWriter fileStream = new PrintWriter(new FileOutputStream(gpxfile, true));
        fileStream.append(System.getProperty("line.separator"));
        fileStream.append(sBody);
        fileStream.flush();
        fileStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.vmware.identity.openidconnect.server.Shared.java

public static void writeJSONResponse(HttpServletResponse httpServletResponse, int statusCode,
        JSONObject jsonObject) throws IOException {
    Validate.notNull(httpServletResponse, "httpServletResponse");
    Validate.notNull(jsonObject, "jsonObject");

    PrintWriter writer = null;
    try {/*  w w  w .  ja  va 2s .  c o m*/
        httpServletResponse.setStatus(statusCode);
        httpServletResponse.setContentType("application/json");
        writer = httpServletResponse.getWriter();
        writer.print(jsonObject.toString());
    } finally {
        if (writer != null) {
            writer.close();
        }
    }
}