Example usage for java.io PrintStream print

List of usage examples for java.io PrintStream print

Introduction

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

Prototype

public void print(Object obj) 

Source Link

Document

Prints an object.

Usage

From source file:Main.java

public static void main(String[] args) {
    int c = 123;//from  www .  ja  v  a 2  s  . c om

    PrintStream ps = new PrintStream(System.out);

    // print an integer and change line
    ps.println(c);
    ps.print("from java2s.com");

    // flush the stream
    ps.flush();

}

From source file:Main.java

public static void main(String[] args) {
    long c = 1234567890L;
    try {//ww w  .j ava2s .c o m

        PrintStream ps = new PrintStream(System.out);

        // print a long and change line
        ps.println(c);
        ps.print("from java2s.com");

        // flush the stream
        ps.flush();

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) {
    char c = 'a';

    PrintStream ps = new PrintStream(System.out);

    // print a char and change line
    ps.println(c);/* w  ww. j a  va2 s  . c  o m*/
    ps.println('b');
    ps.print("from java2s.com");

    // flush the stream
    ps.flush();

}

From source file:Main.java

public static void main(String[] args) {
    String s = "from java2s.com.";

    // create a new PrintStream
    PrintStream ps = new PrintStream(System.out);

    // print a string
    ps.println(s);//from   w w w  .j  a v  a 2s.c o  m

    // check for errors and print
    ps.print(ps.checkError());
    ps.flush();
    ps.close();

}

From source file:whois.java

public static void main(String[] args) {

    Socket theSocket;/*from  w w w  .  ja v  a  2 s  .  c  o  m*/
    DataInputStream theWhoisStream;
    PrintStream ps;

    try {
        theSocket = new Socket(hostname, port, true);
        ps = new PrintStream(theSocket.getOutputStream());
        for (int i = 0; i < args.length; i++)
            ps.print(args[i] + " ");
        ps.print("\r\n");
        theWhoisStream = new DataInputStream(theSocket.getInputStream());
        String s;
        while ((s = theWhoisStream.readLine()) != null) {
            System.out.println(s);
        }
    } catch (IOException e) {
        System.err.println(e);
    }

}

From source file:finger.java

public static void main(String[] args) {

    String hostname;/*from w  w  w. ja  v a  2 s .  co m*/
    Socket theSocket;
    DataInputStream theFingerStream;
    PrintStream ps;

    try {
        hostname = args[0];
    } catch (Exception e) {
        hostname = "localhost";
    }

    try {
        theSocket = new Socket(hostname, port, true);
        ps = new PrintStream(theSocket.getOutputStream());
        for (int i = 1; i < args.length; i++)
            ps.print(args[i] + " ");
        ps.print("\r\n");
        theFingerStream = new DataInputStream(theSocket.getInputStream());
        String s;
        while ((s = theFingerStream.readLine()) != null) {
            System.out.println(s);
        }
    } catch (IOException e) {
        System.err.println(e);
    }

}

From source file:is.merkor.core.cli.Main.java

public static void main(String[] args) throws Exception {

    List<String> results = new ArrayList<String>();
    PrintStream out = new PrintStream(System.out, true, "UTF-8");

    CommandLineParser parser = new GnuParser();
    try {/*www  .  j  a v  a  2 s .com*/

        MerkorCoreCommandLineOptions.createOptions();
        results = processCommandLine(parser.parse(MerkorCoreCommandLineOptions.options, args));
        out.print("\n");
        for (String str : results) {
            if (!str.equals("no message"))
                out.println(str);
        }
        out.print("\n");
        if (results.isEmpty()) {
            out.println("nothing found for parameters: ");
            for (int i = 0; i < args.length; i++)
                out.println("\t" + args[i]);
            out.println("for help type: -help or see README.markdown");
            out.print("\n");
        }
    } catch (ParseException e) {
        System.err.println("Parsing failed.  Reason: " + e.getMessage());
    }
}

From source file:Messenger.TorLib.java

public static void main(String[] args) {
    String req = "-r";
    String targetHostname = "tor.eff.org";
    String targetDir = "index.html";
    int targetPort = 80;

    if (args.length > 0 && args[0].equals("-h")) {
        System.out.println("Tinfoil/TorLib - interface for using Tor from Java\n"
                + "By Joe Foley<foley@mit.edu>\n" + "Usage: java Tinfoil.TorLib <cmd> <args>\n"
                + "<cmd> can be: -h for help\n" + "              -r for resolve\n"
                + "              -w for wget\n" + "For -r, the arg is:\n"
                + "  <hostname> Hostname to DNS resolve\n" + "For -w, the args are:\n"
                + "   <host> <path> <optional port>\n"
                + " for example, http://tor.eff.org:80/index.html would be\n" + "   tor.eff.org index.html 80\n"
                + " Since this is a demo, the default is the tor website.\n");
        System.exit(2);//from   w  ww. ja v a2  s  . c  o  m
    }

    if (args.length >= 4)
        targetPort = new Integer(args[2]).intValue();
    if (args.length >= 3)
        targetDir = args[2];
    if (args.length >= 2)
        targetHostname = args[1];
    if (args.length >= 1)
        req = args[0];

    if (req.equals("-r")) {
        System.out.println(TorResolve(targetHostname));
    } else if (req.equals("-w")) {
        try {
            Socket s = TorSocket(targetHostname, targetPort);
            DataInputStream is = new DataInputStream(s.getInputStream());
            PrintStream out = new java.io.PrintStream(s.getOutputStream());

            //Construct an HTTP request
            out.print("GET  /" + targetDir + " HTTP/1.0\r\n");
            out.print("Host: " + targetHostname + ":" + targetPort + "\r\n");
            out.print("Accept: */*\r\n");
            out.print("Connection: Keep-Aliv\r\n");
            out.print("Pragma: no-cache\r\n");
            out.print("\r\n");
            out.flush();

            // this is from Java Examples In a Nutshell
            final InputStreamReader from_server = new InputStreamReader(is);
            char[] buffer = new char[1024];
            int chars_read;

            // read until stream closes
            while ((chars_read = from_server.read(buffer)) != -1) {
                // loop through array of chars
                // change \n to local platform terminator
                // this is a nieve implementation
                for (int j = 0; j < chars_read; j++) {
                    if (buffer[j] == '\n')
                        System.out.println();
                    else
                        System.out.print(buffer[j]);
                }
                System.out.flush();
            }
            s.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:org.apache.heron.scheduler.RuntimeManagerMain.java

public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException,
        InstantiationException, IOException, ParseException {

    Options options = constructOptions();
    Options helpOptions = constructHelpOptions();
    CommandLineParser parser = new DefaultParser();
    // parse the help options first.
    CommandLine cmd = parser.parse(helpOptions, args, true);

    if (cmd.hasOption("h")) {
        usage(options);//from   w  w w  .j ava 2  s .c  om
        return;
    }

    try {
        // Now parse the required options
        cmd = parser.parse(options, args);
    } catch (ParseException e) {
        usage(options);
        throw new RuntimeException("Error parsing command line options: ", e);
    }

    Boolean verbose = false;
    Level logLevel = Level.INFO;
    if (cmd.hasOption("v")) {
        logLevel = Level.ALL;
        verbose = true;
    }

    // init log
    LoggingHelper.loggerInit(logLevel, false);

    String cluster = cmd.getOptionValue("cluster");
    String role = cmd.getOptionValue("role");
    String environ = cmd.getOptionValue("environment");
    String submitUser = cmd.getOptionValue("submit_user");
    String heronHome = cmd.getOptionValue("heron_home");
    String configPath = cmd.getOptionValue("config_path");
    String overrideConfigFile = cmd.getOptionValue("override_config_file");
    String releaseFile = cmd.getOptionValue("release_file");
    String topologyName = cmd.getOptionValue("topology_name");
    String commandOption = cmd.getOptionValue("command");

    // Optional argument in the case of restart
    // TODO(karthik): convert into CLI
    String containerId = Integer.toString(-1);
    if (cmd.hasOption("container_id")) {
        containerId = cmd.getOptionValue("container_id");
    }

    Boolean dryRun = false;
    if (cmd.hasOption("u")) {
        dryRun = true;
    }

    // Default dry-run output format type
    DryRunFormatType dryRunFormat = DryRunFormatType.TABLE;
    if (dryRun && cmd.hasOption("t")) {
        String format = cmd.getOptionValue("dry_run_format");
        dryRunFormat = DryRunFormatType.getDryRunFormatType(format);
        LOG.fine(String.format("Running dry-run mode using format %s", format));
    }

    Command command = Command.makeCommand(commandOption);

    // add config parameters from the command line
    Config.Builder commandLineConfig = Config.newBuilder().put(Key.CLUSTER, cluster).put(Key.ROLE, role)
            .put(Key.ENVIRON, environ).put(Key.SUBMIT_USER, submitUser).put(Key.DRY_RUN, dryRun)
            .put(Key.DRY_RUN_FORMAT_TYPE, dryRunFormat).put(Key.VERBOSE, verbose)
            .put(Key.TOPOLOGY_CONTAINER_ID, containerId);

    // This is a command line option, but not a valid config key. Hence we don't use Keys
    translateCommandLineConfig(cmd, commandLineConfig);

    Config.Builder topologyConfig = Config.newBuilder().put(Key.TOPOLOGY_NAME, topologyName);

    // build the final config by expanding all the variables
    Config config = Config.toLocalMode(Config.newBuilder()
            .putAll(ConfigLoader.loadConfig(heronHome, configPath, releaseFile, overrideConfigFile))
            .putAll(commandLineConfig.build()).putAll(topologyConfig.build()).build());

    LOG.fine("Static config loaded successfully ");
    LOG.fine(config.toString());

    /* Meaning of exit status code:
      - status code = 0:
        program exits without error
      - 0 < status code < 100:
        program fails to execute before program execution. For example,
        JVM cannot find or load main class
      - 100 <= status code < 200:
        program fails to launch after program execution. For example,
        topology definition file fails to be loaded
      - status code == 200
        program sends out dry-run response */
    /* Since only stderr is used (by logging), we use stdout here to
       propagate any message back to Python's executor.py (invoke site). */
    // Create a new instance of RuntimeManagerMain
    RuntimeManagerMain runtimeManagerMain = new RuntimeManagerMain(config, command);
    try {
        runtimeManagerMain.manageTopology();
        // SUPPRESS CHECKSTYLE IllegalCatch
    } catch (UpdateDryRunResponse response) {
        LOG.log(Level.FINE, "Sending out dry-run response");
        // Output may contain UTF-8 characters, so we should print using UTF-8 encoding
        PrintStream out = new PrintStream(System.out, true, StandardCharsets.UTF_8.name());
        out.print(DryRunRenders.render(response, Context.dryRunFormatType(config)));
        // SUPPRESS CHECKSTYLE RegexpSinglelineJava
        // Exit with status code 200 to indicate dry-run response is sent out
        System.exit(200);
        // SUPPRESS CHECKSTYLE IllegalCatch
    } catch (Exception e) {
        LOG.log(Level.FINE, "Exception when executing command " + commandOption, e);
        System.out.println(e.getMessage());
        // Exit with status code 100 to indicate that error has happened on user-land
        // SUPPRESS CHECKSTYLE RegexpSinglelineJava
        System.exit(100);
    }
}

From source file:org.apache.heron.scheduler.SubmitterMain.java

public static void main(String[] args) throws Exception {
    Options options = constructOptions();
    Options helpOptions = constructHelpOptions();
    CommandLineParser parser = new DefaultParser();
    // parse the help options first.
    CommandLine cmd = parser.parse(helpOptions, args, true);

    if (cmd.hasOption("h")) {
        usage(options);/*from  w  ww  .  j ava  2  s  . c om*/
        return;
    }

    try {
        // Now parse the required options
        cmd = parser.parse(options, args);
    } catch (ParseException e) {
        usage(options);
        throw new RuntimeException("Error parsing command line options: ", e);
    }

    Level logLevel = Level.INFO;
    if (isVerbose(cmd)) {
        logLevel = Level.ALL;
    }

    // init log
    LoggingHelper.loggerInit(logLevel, false);

    // load the topology definition into topology proto
    TopologyAPI.Topology topology = TopologyUtils.getTopology(cmd.getOptionValue("topology_defn"));
    Config config = loadConfig(cmd, topology);

    LOG.fine("Static config loaded successfully");
    LOG.fine(config.toString());

    SubmitterMain submitterMain = new SubmitterMain(config, topology);
    /* Meaning of exit status code:
       - status code = 0:
         program exits without error
       - 0 < status code < 100:
         program fails to execute before program execution. For example,
         JVM cannot find or load main class
       - 100 <= status code < 200:
         program fails to launch after program execution. For example,
         topology definition file fails to be loaded
       - status code >= 200
         program sends out dry-run response */
    try {
        submitterMain.submitTopology();
    } catch (SubmitDryRunResponse response) {
        LOG.log(Level.FINE, "Sending out dry-run response");
        // Output may contain UTF-8 characters, so we should print using UTF-8 encoding
        PrintStream out = new PrintStream(System.out, true, StandardCharsets.UTF_8.name());
        out.print(DryRunRenders.render(response, Context.dryRunFormatType(config)));
        // Exit with status code 200 to indicate dry-run response is sent out
        // SUPPRESS CHECKSTYLE RegexpSinglelineJava
        System.exit(200);
        // SUPPRESS CHECKSTYLE IllegalCatch
    } catch (Exception e) {
        /* Since only stderr is used (by logging), we use stdout here to
           propagate error message back to Python's executor.py (invoke site). */
        LOG.log(Level.FINE, "Exception when submitting topology", e);
        System.out.println(e.getMessage());
        // Exit with status code 100 to indicate that error has happened on user-land
        // SUPPRESS CHECKSTYLE RegexpSinglelineJava
        System.exit(100);
    }
    LOG.log(Level.FINE, "Topology {0} submitted successfully", topology.getName());
}