Example usage for java.io PrintStream PrintStream

List of usage examples for java.io PrintStream PrintStream

Introduction

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

Prototype

public PrintStream(File file) throws FileNotFoundException 

Source Link

Document

Creates a new print stream, without automatic line flushing, with the specified file.

Usage

From source file:com.izforge.izpack.event.AntActionLogBuildListener.java

public AntActionLogBuildListener(File logFile, boolean append, int level) {
    this.setMessageOutputLevel(level);
    if (logFile != null) {
        PrintStream printStream;/*from   w w w.j av a  2s .com*/
        try {
            final File canonicalLogFile = logFile.getCanonicalFile();
            FileUtils.forceMkdir(canonicalLogFile.getParentFile());
            FileUtils.touch(canonicalLogFile);
            printStream = new PrintStream(new FileOutputStream(canonicalLogFile, append));
            this.setOutputPrintStream(printStream);
            this.setErrorPrintStream(printStream);
        } catch (IOException e) {
            logger.warning("Cannot log to file '" + logFile + "': " + e.getMessage());
            this.setOutputPrintStream(System.out);
            this.setErrorPrintStream(System.err);
        }
    } else {
        this.setOutputPrintStream(System.out);
        this.setErrorPrintStream(System.err);
    }
}

From source file:bamons.process.monitoring.service.listener.EmailMonitoringNotifier.java

private String formatExceptionMessage(Throwable exception) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    exception.printStackTrace(new PrintStream(baos));
    return baos.toString();
}

From source file:key.access.manager.HttpHandler.java

public String connect(String url, ArrayList data) throws IOException {
    try {//from  w  ww .j ava  2s.  c  o  m
        // open a connection to the site
        URL connectionUrl = new URL(url);
        URLConnection con = connectionUrl.openConnection();
        // activate the output
        con.setDoOutput(true);
        PrintStream ps = new PrintStream(con.getOutputStream());
        // send your parameters to your site
        for (int i = 0; i < data.size(); i++) {
            ps.print(data.get(i));
            //System.out.println(data.get(i));
            if (i != data.size() - 1) {
                ps.print("&");
            }
        }

        // we have to get the input stream in order to actually send the request
        InputStream inStream = con.getInputStream();
        Scanner s = new Scanner(inStream).useDelimiter("\\A");
        String result = s.hasNext() ? s.next() : "";
        System.out.println(result);

        // close the print stream
        ps.close();
        return result;
    } catch (MalformedURLException e) {
        e.printStackTrace();
        return "error";
    } catch (IOException e) {
        e.printStackTrace();
        return "error";
    }
}

From source file:client.communication.SocketClient.java

/**
 * Envia a mensagem para o servidor e retorna a resposta
 * //from   www  .j av  a 2s .  com
 * @param message
 * @return 
 */
public String sendMessage(String message) {
    Socket socket = null;

    PrintStream stream = null;

    try {
        socket = new Socket(serverAddress, serverPort);

        stream = new PrintStream(socket.getOutputStream());

        // Envia requisiao
        stream.println(message);

        // L resposta
        socket.getInputStream().read(response);
    } catch (IOException e) {
        System.out.println("Problem connecting server!");
    } finally {
        try {
            // Fecha stream
            if (stream != null)
                stream.close();

            if (socket != null)
                socket.close();
        } catch (IOException e) {
            System.err.println("Problem closing socket: " + e.getMessage());
        }
    }

    // Decodifica resposta em base 64
    String _reply = new String(Base64.decodeBase64(response));

    // Remove o espao nas extremidades da string de resposta
    return _reply.trim();
}

From source file:com.ikon.util.ExecutionUtils.java

/**
 * Execute script from file//from w ww .  j  a v a2 s  .co m
 * 
 * @return 0 - Return
 *         1 - StdOut
 *         2 - StdErr
 */
public static Object[] runScript(File script) throws EvalError {
    Object[] ret = new Object[3];
    FileReader fr = null;

    try {
        if (script.exists() && script.canRead()) {
            ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
            PrintStream out = new PrintStream(baosOut);
            ByteArrayOutputStream baosErr = new ByteArrayOutputStream();
            PrintStream err = new PrintStream(baosErr);
            Interpreter i = new Interpreter(null, out, err, false);
            fr = new FileReader(script);

            ret[0] = i.eval(fr);

            out.flush();
            ret[1] = baosOut.toString();
            err.flush();
            ret[2] = baosErr.toString();
        } else {
            log.warn("Unable to read script: {}", script.getPath());
        }
    } catch (IOException e) {
        log.warn(e.getMessage(), e);
    } finally {
        IOUtils.closeQuietly(fr);
    }

    log.debug("runScript: {}", Arrays.toString(ret));
    return ret;
}

From source file:com.leosys.core.telnet.Telent.java

public Telent(String hostname, int hostport, String user, String password) throws SocketException, IOException {
    super();/*from  www .  j  a v  a 2 s  .  co  m*/
    this.hostname = hostname;
    this.hostport = hostport;
    this.user = user;
    this.password = password;

    telnet = new TelnetClient("VT100");// VT100 VT52 VT220 VTNT ANSI  
    telnet.connect(hostname, hostport);
    in = telnet.getInputStream();
    out = new PrintStream(telnet.getOutputStream());

    readUntil("login: ");
    write(user);
    write("\n");
    readUntil("Password: ");
    write(password);
    write("\n");
}

From source file:com.sap.prd.mobile.ios.mios.XCodeSaveBuildSettingsMojo.java

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    try {// ww w.ja va2s .c  o m
        for (String configuration : getConfigurations()) {
            for (String sdk : getSDKs()) {
                XCodeContext ctx = getXCodeContext();
                CommandLineBuilder cmdLineBuilder = new CommandLineBuilder(configuration, sdk, ctx);
                PrintStream out = null;
                try {
                    out = new PrintStream(
                            EffectiveBuildSettings.getBuildSettingsFile(this.project, configuration, sdk));
                    final int returnValue = Forker.forkProcess(out, ctx.getProjectRootDirectory(),
                            cmdLineBuilder.createShowBuildSettingsCall());
                    if (returnValue != 0) {
                        throw new XCodeException(
                                "Could not execute xcodebuild -showBuildSettings command for configuration "
                                        + configuration);
                    }
                } finally {
                    IOUtils.closeQuietly(out);
                }
            }
        }
    } catch (Exception e) {
        throw new MojoExecutionException("Could not save build settings", e);
    }
}

From source file:com.anrisoftware.mongoose.buildins.echobuildin.EchoBuildin.java

@Override
protected void doCall() {
    PrintStream stream = new PrintStream(getOutput());
    output.output(stream, text);
    stream.flush();
}

From source file:au.com.jwatmuff.eventmanager.export.CSVExporter.java

public static void generateFromArray(Object[][] table, OutputStream os) {
    PrintStream ps = new PrintStream(os);
    for (Object[] row : table)
        outputRow(row, ps);//  w w w  .  j av a2 s .c  om
}

From source file:commands.AbstractCommand.java

@Override
public void outputToFile(File outputFile, boolean append) {
    try {//from   ww  w .  ja va2 s .c o m
        this.out = new PrintStream(new FileOutputStream(outputFile, append));
        this.outputFile = outputFile;
        this.append = append;
    } catch (FileNotFoundException e) {
        throw new CommandException("Could not find output file");
    }
}