private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); private final ByteArrayOutputStream errContent = new ByteArrayOutputStream(); @Before public void setUpStreams() { System.setOut(new PrintStream(outContent)); System.setErr(new PrintStream(errContent)); } @After public void cleanUpStreams() { System.setOut(null); System.setErr(null); }
private String stacktrace(Exception e) { ByteArrayOutputStream os = new ByteArrayOutputStream(); PrintStream ps = new PrintStream(os, true); e.printStackTrace(ps); ps.close(); return os.toString(); } }
/** * Gets the stack trace from a Throwable as a String. * * @param cause the {@link Throwable} to be examined * @return the stack trace as generated by {@link Throwable#printStackTrace(java.io.PrintWriter)} method. */ public static String stackTraceToString(Throwable cause) { ByteArrayOutputStream out = new ByteArrayOutputStream(); PrintStream pout = new PrintStream(out); cause.printStackTrace(pout); pout.flush(); try { return new String(out.toByteArray()); } finally { try { out.close(); } catch (IOException ignore) { // ignore as should never happen } } }
public static void redirectOutput(String file) throws Exception { System.out.println("Redirecting output to " + file); FileOutputStream workerOut = new FileOutputStream(new File(file)); PrintStream ps = new PrintStream(new BufferedOutputStream(workerOut), true); System.setOut(ps); System.setErr(ps); LOG.info("Successfully redirect System.out to " + file); }
void assertClosed() { if ( !c.isClosed() ) { ByteArrayOutputStream out = new ByteArrayOutputStream(); PrintStream printStream = new PrintStream( out ); for ( StackTraceElement traceElement : stackTrace ) { printStream.println( "\tat " + traceElement ); } printStream.println(); throw new IllegalStateException( format( "Closeable %s was not closed!\n%s", c, out.toString() ) ); } } }
PrintStream fileStream = new PrintStream(new File("file.txt")); fileStream.println("your data"); // ^^^^^^^ will add OS line separator after data
public HTMLWriter(String filename, Multimap<String, String> curMap) throws FileNotFoundException { LOG.debug("Making file: {}", filename); File newFile = new File(outPath, filename +".html"); FileOutputStream fileOutputStream = new FileOutputStream(newFile); this.out = new PrintStream(fileOutputStream); writerAnnotations = curMap; annotationClassName = filename; }
/** * Transform path for Windows. */ private InputStream transformForWindows(InputStream src) throws IOException { BufferedReader r = new BufferedReader(new InputStreamReader(src)); ByteArrayOutputStream out = new ByteArrayOutputStream(); try (PrintStream p = new PrintStream(out)) { String line; while ((line = r.readLine()) != null) { if (!line.startsWith("#") && Functions.isWindows()) line = line.replace("/", "\\\\"); p.println(line); } } return new ByteArrayInputStream(out.toByteArray()); }
public static String print2String(byte minbinMsg[]) { ByteArrayOutputStream os = new ByteArrayOutputStream(); MBPrinter.printMessage(minbinMsg, new PrintStream(os)); try { return os.toString("UTF8"); } catch (UnsupportedEncodingException e) { return e.getMessage(); } }
import java.io.FileDescriptor; import java.io.FileOutputStream; import java.io.PrintStream; public static void main(String [] args) { System.err.println("error."); System.out.println("out."); System.setOut(System.err); System.out.println("error?"); System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out))); System.out.println("out?"); }
@Test public void shouldLoadAndBootstrapJarUsingAgentBootstrapCode_specifiedInAgentManifestFile() throws Exception { if (!OS_CHECKER.satisfy()) { PrintStream err = System.err; try { ByteArrayOutputStream os = new ByteArrayOutputStream(); System.setErr(new PrintStream(os)); File agentJar = new File("agent.jar"); agentJar.delete(); new AgentBootstrapper() { @Override void jvmExit(int returnValue) { } }.go(false, new AgentBootstrapperArgs(new URL("http://" + "localhost" + ":" + server.getPort() + "/go"), null, AgentBootstrapperArgs.SslMode.NONE)); agentJar.delete(); assertThat(new String(os.toByteArray()), containsString("Hello World Fellas!")); } finally { System.setErr(err); } } }
protected Process sudoWithPass(ArgumentListBuilder args) throws IOException { args.prepend(sudoExe(),"-S"); listener.getLogger().println("$ "+Util.join(args.toList()," ")); ProcessBuilder pb = new ProcessBuilder(args.toCommandArray()); Process p = pb.start(); // TODO: use -p to detect prompt // TODO: detect if the password didn't work PrintStream ps = new PrintStream(p.getOutputStream()); ps.println(rootPassword); ps.println(rootPassword); ps.println(rootPassword); return p; } }.start(listener,rootPassword);
private static void redirectStdOutAndErr() { try { PrintStream out = new PrintStream(new FileOutputStream(getOutFile(), true), true); System.setErr(out); System.setOut(out); } catch (FileNotFoundException ignore) { // cannot redirect out and err to file, so we don't log("Unable to redirect stdout/stderr to file " + getOutFile() + ". Will continue without redirecting stdout/stderr."); ignore.printStackTrace(); } }
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(); } } }
final OutputStream os = new FileOutputStream("/tmp/out"); final PrintStream printStream = new PrintStream(os); printStream.print("String"); printStream.close();
@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(); }
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(); } }
public void testVerbosePrintNullLabelAndMap() { final ByteArrayOutputStream out = new ByteArrayOutputStream(); final PrintStream outPrint = new PrintStream(out); outPrint.println("null"); final String EXPECTED_OUT = out.toString(); out.reset(); MapUtils.verbosePrint(outPrint, null, null); assertEquals(EXPECTED_OUT, out.toString()); }
public static void writeLinesTextFile(File dir) throws IOException { FileUtil.fullyDelete(dir); File fileLines = new File(dir, "lines.avro"); fileLines.getParentFile().mkdirs(); try(PrintStream out = new PrintStream(fileLines)) { for (String line : LINES) { out.println(line); } } }