private String stacktrace(Exception e) { ByteArrayOutputStream os = new ByteArrayOutputStream(); PrintStream ps = new PrintStream(os, true); e.printStackTrace(ps); ps.close(); return os.toString(); } }
File file = new File("test.log"); PrintStream ps = new PrintStream(file); try { // something } catch (Exception ex) { ex.printStackTrace(ps); } ps.close();
final OutputStream os = new FileOutputStream("/tmp/out"); final PrintStream printStream = new PrintStream(os); printStream.print("String"); printStream.close();
optionSet = parser.parse(args); } catch (OptionException e) { System.err.println("unrecognized option(s): " + CustomHelpFormatter.OPTIONS_JOINER.join(e.options())); parser.printHelpOn(System.err); System.err.println("cannot specify both \"--brief\" and " + "\"--quiet\""); parser.printHelpOn(System.err); System.err.println("missing arguments"); parser.printHelpOn(System.err); System.exit(CMD_ERROR.get()); files.add(new File(target).getCanonicalFile()); System.out.close(); System.err.close(); reporter = Reporters.QUIET;
/** * Dumps the config as a set of HTML tables * * @param ConfigurationManager the manager * @param path where to output the HTML * @throws java.io.IOException if an error occurs */ public static void showConfigAsHTML(ConfigurationManager ConfigurationManager, String path) throws IOException { PrintStream out = new PrintStream(new FileOutputStream(path)); dumpHeader(out); for (String componentName : ConfigurationManager.getInstanceNames(Configurable.class)) { dumpComponentAsHTML(out, componentName, ConfigurationManager.getPropertySheet(componentName)); } dumpFooter(out); out.close(); }
@Test public void testFlushOnClose() throws IOException { MockHttpServletResponse mockResponse = new MockHttpServletResponse(); PartialBufferedOutputStream2 pbos = new PartialBufferedOutputStream2(mockResponse); PrintStream ps = new PrintStream(pbos); ps.print("Hello world!"); ps.close(); // check the in memory buffer has been flushed to the target output stream // close assertEquals("Hello world!", mockResponse.getContentAsString()); } }
@Override public void copyFromLocalFile(File srcFile, URI dstUri) throws Exception { OutputStream stream = _adlStoreClient.createFile(dstUri.getPath(), IfExists.OVERWRITE); PrintStream out = new PrintStream(stream); byte[] inputStream = IOUtils.toByteArray(new FileInputStream(srcFile)); out.write(inputStream); out.close(); }
/** * Creates the individual batch files * * @param dir the directory to place the input file in * @param name the name of the file * @param line the contents of the file */ private void createInputFile(File dir, String name, String line) throws IOException { File path = new File(dir, name); FileOutputStream fos = new FileOutputStream(path); PrintStream ps = new PrintStream(fos); ps.println(line); ps.close(); }
public static String exceptionStacktraceToString(Exception e) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintStream ps = new PrintStream(baos); e.printStackTrace(ps); ps.close(); return baos.toString(); }
private void caugh(Throwable t) { File f = new File(String.format("webcam-capture-hs-%s", System.currentTimeMillis())); PrintStream ps = null; try { t.printStackTrace(ps = new PrintStream(f)); } catch (FileNotFoundException e) { e.printStackTrace(); } finally { if (ps != null) { ps.close(); } } }
private int printHelpTextAndExit(String helpTextResource) throws IOException { String fullHelpTextResource = HELP_TEXT_RESOURCE_ROOT + helpTextResource; InputStream helpTextInputStream = CommandLineClient.class.getResourceAsStream(fullHelpTextResource); if (helpTextInputStream == null) { return showErrorAndExit("No detailed help text available for this command."); } for (String line : IOUtils.readLines(helpTextInputStream)) { line = replaceVariables(line); out.println(line.replaceAll("\\s$", "")); } out.close(); return 0; }
@Test public void testGetProperty() throws IOException { PrintStream o = System.out; File f = File.createTempFile("cfg", ".tmp"); PrintStream tmpOut = new PrintStream(new FileOutputStream(f), false, "UTF-8"); System.setOut(tmpOut); KylinConfigCLI.main(new String[] { "kylin.storage.url" }); String val = FileUtils.readFileToString(f, Charset.defaultCharset()).trim(); assertEquals("hbase", val); tmpOut.close(); FileUtils.forceDelete(f); System.setOut(o); }
/** * Execute the length task. */ @Override public void execute() { validate(); OutputStream out = property == null ? new LogOutputStream(this, Project.MSG_INFO) : new PropertyOutputStream(getProject(), property); PrintStream ps = new PrintStream(out); switch (mode) { case STRING: ps.print(getLength(string, getTrim())); ps.close(); break; case EACH: handleResources(new EachHandler(ps)); break; case ALL: handleResources(new AllHandler(ps)); break; } }
public static void main(String args[]) throws IOException { PrintStream ps = null; try { ps = new PrintStream(new FileOutputStream("myfile.txt")); ps.println("This data is written to a file:"); System.out.println("Write successfully"); } catch (IOException e) { System.err.println("Error in writing to file"); throw e; } finally { if (ps != null) ps.close(); } }
/** * Get long description as a string. */ @Restricted(NoExternalUse.class) public final String getLongDescription() { ByteArrayOutputStream out = new ByteArrayOutputStream(); PrintStream ps = new PrintStream(out); printUsageSummary(ps); ps.close(); return out.toString(); }
public static void dumpVmInfo( File directory ) throws IOException { File file = new File( directory, "main-vm-dump-" + System.currentTimeMillis() ); PrintStream out = null; try { out = new PrintStream( file ); dumpVmInfo( out ); } finally { if ( out != null ) { out.close(); } } }
tempFile.setWritable(true); PrintStream ps = new PrintStream(tempFile); ps.println(first); ps.println(second); ps.close(); System.err.println("test failed with exception: " + t.getMessage()); t.printStackTrace(System.err); fail("Test erroneous");