List of usage examples for java.io PrintWriter PrintWriter
public PrintWriter(File file) throws FileNotFoundException
From source file:Main.java
/** * Like {@link #sprintFullStack(Throwable)}, renders a stacktrace as a String for logging, * but excludes stack trace elements common with the calling scope, which may * be considered irrelevant to application functionality testing * @param t an exception//from www. j a v a 2s. co m * @return a string representation of a shorter stack trace */ public static String sprintShortStack(final Throwable t) { if (t == null) { return ""; } StringWriter writer = new StringWriter(); Throwable local = new Throwable(); StackTraceElement[] localStack = local.getStackTrace(); StackTraceElement[] tStack = t.getStackTrace(); int m = tStack.length - 1, n = localStack.length - 1; while (m >= 0 && n >= 0 && tStack[m].equals(localStack[n])) { m--; n--; } int framesInCommon = tStack.length - 1 - (m + 1); t.setStackTrace(Arrays.copyOf(t.getStackTrace(), m + 1, StackTraceElement[].class)); t.printStackTrace(new PrintWriter(writer)); t.setStackTrace(tStack); StringBuilder sb = new StringBuilder(writer.toString()); if (framesInCommon != 0) { sb.append("\t... " + framesInCommon + " more"); } return sb.toString(); }
From source file:Main.java
private static String getCrashReport(Throwable e, Context application) { StringBuffer body = new StringBuffer(); body.append("Timestamp: " + new Date().toString()); body.append("\n** Crash Report **\n"); try {//from w ww . jav a 2 s. c o m PackageInfo pi = application.getPackageManager().getPackageInfo(application.getPackageName(), 0); body.append("Package Name: ").append(pi.packageName).append("\n"); body.append("Package Version: ").append(pi.versionCode).append("\n"); body.append("Phone Model: ").append(android.os.Build.MODEL).append("\n"); body.append("Phone Manufacturer: ").append(android.os.Build.MANUFACTURER).append("\n"); body.append("Android Version:").append(android.os.Build.VERSION.RELEASE).append("\n"); } catch (NameNotFoundException e1) { } StringWriter stack = new StringWriter(); PrintWriter writer = new PrintWriter(stack); e.printStackTrace(writer); body.append("\n\nStacktrace:\n\n"); body.append(stack.toString()).append("\n"); if (e.getCause() != null) { Throwable cause = e.getCause(); stack = new StringWriter(); writer = new PrintWriter(stack); cause.printStackTrace(writer); body.append("\n\nCause Stacktrace:\n\n"); body.append(stack.toString()).append("\n"); } body.append("** Crash Report **\n"); return body.toString(); }
From source file:edu.ucsd.ccdb.cil.xml2json.XML2JsonUtil.java
public void outputStringToFile(File f, String json) throws Exception { FileOutputStream fout = new FileOutputStream(f); PrintWriter pw = new PrintWriter(fout); pw.print(json);/*w w w .j av a2 s . c om*/ pw.close(); }
From source file:net.sf.firemox.tools.MSaveDeck.java
/** * Saves deck to ASCII file from specified staticTurnLists. This new file will * contain the card names sorted in alphabetical order with their quantity * with this format :<br>/*from w w w. j a v a2 s . com*/ * <i>card's name </i> <b>; </b> <i>qty </i> <b>\n </b> <br> * * @param fileName * Name of new file. * @param names * ListModel of card names. * @param parent * the parent * @return true if the current deck has been correctly saved. false otherwise. */ public static boolean saveDeck(String fileName, MListModel<MCardCompare> names, JFrame parent) { PrintWriter outStream = null; try { // create the deckfile. If it was already existing, it would be scrathed. outStream = new PrintWriter(new FileOutputStream(fileName)); } catch (FileNotFoundException ex) { JOptionPane.showMessageDialog(parent, "Cannot create/modify the specified deck file:" + fileName + "\n" + ex.getMessage(), "File creation problem", JOptionPane.ERROR_MESSAGE); return false; } Object[] namesArray = names.toArray(); MCardCompare[] cards = new MCardCompare[namesArray.length]; System.arraycopy(namesArray, 0, cards, 0, namesArray.length); // sorts names Arrays.sort(cards, new MCardCompare()); // writes lines corresponding to this format : "card;quantity\n" for (int i = 0; i < cards.length; i++) { outStream.println(cards[i].toString()); } IOUtils.closeQuietly(outStream); // successfull deck save JOptionPane.showMessageDialog(parent, "Saving file " + fileName.substring(fileName.lastIndexOf("/") + 1) + " was successfully completed.", "Save success", JOptionPane.INFORMATION_MESSAGE); return true; }
From source file:MyServlet.java
public void saveState() { FileWriter fileWriter = null; PrintWriter printWriter = null; try {/* www . j ava 2s . c o m*/ fileWriter = new FileWriter("InitDestroyCounter.initial"); printWriter = new PrintWriter(fileWriter); printWriter.println(count); printWriter.close(); return; } catch (IOException e) { } }
From source file:bammerbom.ultimatecore.spongeapi.resources.classes.ErrorLogger.java
public static void log(Throwable t, String s) { String time = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss-SSS").format(Calendar.getInstance().getTime()); File dir = new File(r.getUC().getDataFolder() + "/Errors"); if (!dir.exists()) { dir.mkdir();/*from w w w .ja v a 2s . c om*/ } File file = new File(r.getUC().getDataFolder() + "/Errors", time + ".txt"); if (!file.exists()) { try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } FileWriter outFile; try { outFile = new FileWriter(file); } catch (IOException e) { e.printStackTrace(); return; } PrintWriter out = new PrintWriter(outFile); out.println("======================================="); out.println("UltimateCore has run into an error "); out.println("Please report your error on dev.bukkit.org/bukkit-plugins/ultimate_core/create-ticket"); out.println("Spongeapi version: " + r.getGame().getMinecraftVersion() + " - " + r.getGame().getImplementationVersion()); out.println("UltimateCore version: " + r.getGame().getPluginManager().getPlugin("UltimateCore").get().getVersion()); out.println("Plugins loaded (" + r.getGame().getPluginManager().getPlugins().size() + "): " + Arrays.asList(r.getGame().getPluginManager().getPlugins())); out.println("Java version: " + System.getProperty("java.version")); out.println("OS info: " + System.getProperty("os.arch") + ", " + System.getProperty("os.name") + ", " + System.getProperty("os.version")); if (r.getGame().getServer().isPresent()) { out.println("Online mode: " + r.getGame().getServer().get().getOnlineMode()); } out.println("Time: " + time); out.println("Error message: " + t.getMessage()); out.println("UltimateCore message: " + s); out.println("======================================="); out.println("Stacktrace: \n" + ExceptionUtils.getStackTrace(t)); out.println("======================================="); out.close(); try { outFile.close(); } catch (IOException e) { e.printStackTrace(); } // r.log(" "); r.log(TextColors.DARK_RED + "========================================================="); r.log(TextColors.RED + "UltimateCore has run into an error "); r.log(TextColors.RED + "Please report your error on "); r.log(TextColors.YELLOW + "http://dev.bukkit.org/bukkit-plugins/ultimate_core/create-ticket"); r.log(TextColors.RED + "Include the file: "); r.log(TextColors.YELLOW + "plugins/UltimateCore/Errors/" + time + ".txt "); /*r.log(TextColors.RED + "Sponge version: " + Bukkit.getServer().getVersion()); r.log(TextColors.RED + "UltimateCore version: " + Bukkit.getPluginManager().getPlugin("UltimateCore").getDescription().getVersion()); r.log(TextColors.RED + "Plugins loaded (" + Bukkit.getPluginManager().getPlugins().length + "): " + Arrays.asList(Bukkit.getPluginManager().getPlugins())); r.log(TextColors.RED + "Java version: " + System.getProperty("java.version")); r.log(TextColors.RED + "OS info: " + System.getProperty("os.arch") + ", " + System.getProperty("os.name") + ", " + System.getProperty("os.version")); r.log(TextColors.RED + "Error message: " + t.getMessage()); r.log(TextColors.RED + "UltimateCore message: " + s);*/ r.log(TextColors.DARK_RED + "========================================================="); if (t instanceof Exception) { r.log(TextColors.RED + "Stacktrace: "); t.printStackTrace(); r.log(TextColors.DARK_RED + "========================================================="); } r.log(" "); }
From source file:avantssar.aslanpp.testing.HTMLHelper.java
public static File toHTML(File textFile, boolean lineNumbers) { if (textFile != null) { File htmlFile = new File(textFile.getAbsolutePath() + ".html"); try {// www .j a va 2s .c o m BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(textFile))); PrintWriter writer = new PrintWriter(htmlFile); String line; writer.println("<html>"); writer.println("<body>"); writer.println("<pre>"); int lineCount = 1; while ((line = reader.readLine()) != null) { if (lineNumbers) { line = String.format("%4d: %s", lineCount++, line); } writer.println(line); } writer.println("</pre>"); writer.println("</body>"); writer.println("</html>"); reader.close(); writer.close(); return htmlFile; } catch (IOException ex) { System.out.println( "Failed to convert to HTML file '" + textFile.getAbsolutePath() + "': " + ex.getMessage()); Debug.logger.error(ex); return null; } } else { return null; } }
From source file:net.erdfelt.android.sdkfido.project.FilteredFileUtil.java
public static void copyWithExpansion(String resourceId, File destFile, Map<String, String> props) { URL url = FilteredFileUtil.class.getResource(resourceId); if (url == null) { LOG.log(Level.WARNING, "Unable to find resourceID: " + resourceId); return;/*from w w w . j a v a 2s .co m*/ } InputStream in = null; InputStreamReader reader = null; BufferedReader buf = null; FileWriter writer = null; PrintWriter out = null; try { in = url.openStream(); reader = new InputStreamReader(in); buf = new BufferedReader(reader); writer = new FileWriter(destFile); out = new PrintWriter(writer); PropertyExpander expander = new PropertyExpander(props); String line; while ((line = buf.readLine()) != null) { out.println(expander.expand(line)); } } catch (IOException e) { LOG.log(Level.WARNING, "Unable to open input stream for url: " + url, e); } finally { IOUtils.closeQuietly(out); IOUtils.closeQuietly(writer); IOUtils.closeQuietly(buf); IOUtils.closeQuietly(reader); IOUtils.closeQuietly(in); } }