Here you can find the source of writeStringToTempFileNoExceptions(String contents, String path)
Parameter | Description |
---|---|
contents | The string to write |
path | The file path |
public static void writeStringToTempFileNoExceptions(String contents, String path)
//package com.java2s; import java.io.*; import java.util.zip.GZIPOutputStream; public class Main { /**/* w ww . ja va 2 s . com*/ * Writes a string to a temporary file, squashing exceptions * * @param contents The string to write * @param path The file path * @param encoding The encoding to encode in * @return The File that was written to */ public static File writeStringToTempFileNoExceptions(String contents, String path, String encoding) { OutputStream writer = null; File tmp = null; try { tmp = File.createTempFile(path, ".tmp"); if (path.endsWith(".gz")) { writer = new GZIPOutputStream(new FileOutputStream(tmp)); } else { writer = new BufferedOutputStream(new FileOutputStream(tmp)); } writer.write(contents.getBytes(encoding)); } catch (Exception e) { e.printStackTrace(); } finally { closeIgnoringExceptions(writer); } return tmp; } /** * Writes a string to a temporary file with UTF-8 encoding, squashing exceptions * * @param contents The string to write * @param path The file path */ public static void writeStringToTempFileNoExceptions(String contents, String path) { writeStringToTempFileNoExceptions(contents, path, "UTF-8"); } /** * Provides an implementation of closing a file for use in a finally block so * you can correctly close a file without even more exception handling stuff. * From a suggestion in a talk by Josh Bloch. * * @param c The IO resource to close (e.g., a Stream/Reader) */ public static void closeIgnoringExceptions(Closeable c) { if (c != null) { try { c.close(); } catch (IOException ioe) { // ignore } } } }