Example usage for java.io PrintWriter flush

List of usage examples for java.io PrintWriter flush

Introduction

In this page you can find the example usage for java.io PrintWriter flush.

Prototype

public void flush() 

Source Link

Document

Flushes the stream.

Usage

From source file:com.gsinnovations.howdah.CommandLineUtil.java

/**
 * Print the options supported by <code>GenericOptionsParser</code>.
 * In addition to the options supported by the job, passed in as the
 * group parameter.//from   w  ww . j ava2s .  c om
 *
 * @param group job-specific command-line options.
 */
public static void printHelpWithGenericOptions(Group group) {
    org.apache.commons.cli.Options ops = new org.apache.commons.cli.Options();
    new GenericOptionsParser(new Configuration(), ops, new String[0]);
    org.apache.commons.cli.HelpFormatter fmt = new org.apache.commons.cli.HelpFormatter();
    fmt.printHelp("<command> [Generic Options] [Job-Specific Options]", "Generic Options:", ops, "");

    PrintWriter pw = new PrintWriter(System.out);
    HelpFormatter formatter = new HelpFormatter();
    formatter.setGroup(group);
    formatter.setPrintWriter(pw);
    formatter.printHelp();
    pw.flush();
}

From source file:com.jredrain.base.utils.CommandUtils.java

public static File createShellFile(String command, String shellFileName) {
    String dirPath = IOUtils.getTempFolderPath();
    File dir = new File(dirPath);
    if (!dir.exists())
        dir.mkdirs();/*from  w ww  . ja va 2s. c  o m*/

    String tempShellFilePath = dirPath + File.separator + shellFileName + ".sh";
    File shellFile = new File(tempShellFilePath);
    try {
        if (!shellFile.exists()) {
            PrintWriter out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(tempShellFilePath)));
            out.write("#!/bin/bash\n" + command);
            out.flush();
            out.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        return shellFile;
    }
}

From source file:Main.java

public static void putString2XmlFile(String xml, String fileName) {
    PrintWriter out = null;
    try {//from  w ww . j  a v  a2 s .  c o m
        out = new PrintWriter(new BufferedWriter(new FileWriter(fileName)));
        out.write(xml, 0, xml.length());
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        out.flush();
        out.close();
    }
}

From source file:ch.ralscha.extdirectspring.util.ExtDirectSpringUtil.java

/**
 * Converts a stacktrace into a String//from   ww  w  .ja  v  a 2s.c om
 *
 * @param t a Throwable
 * @return the whole stacktrace in a String
 */
public static String getStackTrace(Throwable t) {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw, true);
    t.printStackTrace(pw);
    pw.flush();
    sw.flush();
    return sw.toString();
}

From source file:io.proleap.cobol.TestGenerator.java

public static void generateTreeFile(final File cobolInputFile, final File outputDirectory) throws IOException {
    final File outputFile = new File(outputDirectory + "/" + cobolInputFile.getName() + TREE_EXTENSION);

    final boolean createdNewFile = outputFile.createNewFile();

    if (createdNewFile) {
        LOG.info("Creating tree file {}.", outputFile);

        final File parentDirectory = cobolInputFile.getParentFile();
        final CobolSourceFormat format = getCobolSourceFormat(parentDirectory);

        final String preProcessedInput = CobolGrammarContext.getInstance().getCobolPreprocessor()
                .process(cobolInputFile, parentDirectory, format);

        final Cobol85Lexer lexer = new Cobol85Lexer(new ANTLRInputStream(preProcessedInput));
        final CommonTokenStream tokens = new CommonTokenStream(lexer);
        final Cobol85Parser parser = new Cobol85Parser(tokens);
        final StartRuleContext startRule = parser.startRule();
        final String inputFileTree = TreeUtils.toStringTree(startRule, parser);

        final PrintWriter pWriter = new PrintWriter(new FileWriter(outputFile));

        pWriter.write(inputFileTree);//ww  w .  j  a va 2  s .  c  o  m
        pWriter.flush();
        pWriter.close();
    }
}

From source file:gov.nist.appvet.tool.sigverifier.util.FileUtil.java

public static boolean saveReport(String reportContent, String reportFilePath) {
    PrintWriter out;
    try {//from w ww.  j  a  v a  2  s.c o m
        out = new PrintWriter(reportFilePath);
        out.println(reportContent);
        out.flush();
        out.close();
        log.debug("Saved " + reportFilePath);
        return true;
    } catch (FileNotFoundException e) {
        log.error(e.toString());
        return false;
    }
}

From source file:org.eclipse.jgit.lfs.server.fs.FileLfsServlet.java

/**
 * Send an error response./* www  .ja va2  s .  co  m*/
 *
 * @param rsp
 *            the servlet response
 * @param status
 *            HTTP status code
 * @param message
 *            error message
 * @throws IOException
 *             on failure to send the response
 * @since 4.6
 */
protected static void sendError(HttpServletResponse rsp, int status, String message) throws IOException {
    rsp.setStatus(status);
    PrintWriter writer = rsp.getWriter();
    gson.toJson(new Error(message), writer);
    writer.flush();
    writer.close();
    rsp.flushBuffer();
}

From source file:name.martingeisse.ecotools.simulator.ui.Main.java

/**
 * @param options the command-line option definitions
 *//*from   ww w.  j ava 2  s  .com*/
private static void showUsgeAndExit(Options options) {
    PrintWriter w = new PrintWriter(System.out);
    HelpFormatter formatter = new HelpFormatter();
    formatter.printUsage(w, 150, "ecosim.sh", options);
    formatter.printOptions(w, 150, options, 2, 3);
    w.flush();
    System.exit(1);
}

From source file:SimpleProxyServer.java

/**
 * runs a single-threaded proxy server on
 * the specified local port. It never returns.
 *//* www.jav  a 2s. c o  m*/
public static void runServer(String host, int remoteport, int localport) throws IOException {
    // Create a ServerSocket to listen for connections with
    ServerSocket ss = new ServerSocket(localport);

    final byte[] request = new byte[1024];
    byte[] reply = new byte[4096];

    while (true) {
        Socket client = null, server = null;
        try {
            // Wait for a connection on the local port
            client = ss.accept();

            final InputStream streamFromClient = client.getInputStream();
            final OutputStream streamToClient = client.getOutputStream();

            // Make a connection to the real server.
            // If we cannot connect to the server, send an error to the
            // client, disconnect, and continue waiting for connections.
            try {
                server = new Socket(host, remoteport);
            } catch (IOException e) {
                PrintWriter out = new PrintWriter(streamToClient);
                out.print("Proxy server cannot connect to " + host + ":" + remoteport + ":\n" + e + "\n");
                out.flush();
                client.close();
                continue;
            }

            // Get server streams.
            final InputStream streamFromServer = server.getInputStream();
            final OutputStream streamToServer = server.getOutputStream();

            // a thread to read the client's requests and pass them
            // to the server. A separate thread for asynchronous.
            Thread t = new Thread() {
                public void run() {
                    int bytesRead;
                    try {
                        while ((bytesRead = streamFromClient.read(request)) != -1) {
                            streamToServer.write(request, 0, bytesRead);
                            streamToServer.flush();
                        }
                    } catch (IOException e) {
                    }

                    // the client closed the connection to us, so close our
                    // connection to the server.
                    try {
                        streamToServer.close();
                    } catch (IOException e) {
                    }
                }
            };

            // Start the client-to-server request thread running
            t.start();

            // Read the server's responses
            // and pass them back to the client.
            int bytesRead;
            try {
                while ((bytesRead = streamFromServer.read(reply)) != -1) {
                    streamToClient.write(reply, 0, bytesRead);
                    streamToClient.flush();
                }
            } catch (IOException e) {
            }

            // The server closed its connection to us, so we close our
            // connection to our client.
            streamToClient.close();
        } catch (IOException e) {
            System.err.println(e);
        } finally {
            try {
                if (server != null)
                    server.close();
                if (client != null)
                    client.close();
            } catch (IOException e) {
            }
        }
    }
}

From source file:Main.java

public static String getStackTrace(Exception e) {
    if (e == null) {
        return "";
    }// w  w  w . jav a  2 s  .  c om
    StringWriter sw = null;
    PrintWriter pw = null;
    try {
        sw = new StringWriter();
        pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        pw.flush();
        sw.flush();
    } finally {
        if (sw != null) {
            try {
                sw.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
        if (pw != null) {
            pw.close();
        }
    }
    return sw.toString();
}