Example usage for java.io PrintStream close

List of usage examples for java.io PrintStream close

Introduction

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

Prototype

public void close() 

Source Link

Document

Closes the stream.

Usage

From source file:Main.java

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

    PrintStream ps = new PrintStream(System.out);

    // append our characters
    ps.append(c);//from  w w  w . j  a v a 2 s. c o m
    ps.append('y');
    ps.append('m');

    // print the result
    ps.flush();
    ps.close();

}

From source file:Main.java

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

    PrintStream ps = new PrintStream(System.out);

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

    // closing stream
    System.out.println("Closing Stream...");

    // close the stream
    ps.close();

}

From source file:common.ReverseWordsCount.java

public static void main(String[] args) throws IOException {
    List<String> readLines = FileUtils.readLines(new File("G:\\\\LTNMT\\LTNMT\\sougou\\sougou2500.txt"));
    Map<String, Integer> words = new HashMap<>();

    for (String line : readLines) {
        String[] split = line.split(" ");
        for (String wprd : split) {
            Integer get = words.get(wprd);
            if (get == null) {
                words.put(wprd, 1);/*  w w  w .j  av  a2 s  .  c om*/
            } else {
                words.put(wprd, get + 1);
            }
        }
    }
    Set<Map.Entry<String, Integer>> entrySet = words.entrySet();
    List<Map.Entry<String, Integer>> reverseLists = new ArrayList<>(entrySet);
    Collections.sort(reverseLists, new Comparator<Map.Entry<String, Integer>>() {
        @Override
        public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
            return o2.getValue().compareTo(o1.getValue());
        }
    });
    PrintStream ps = new PrintStream("c:/reverseWords.txt");
    for (Map.Entry<String, Integer> teEntry : reverseLists) {
        ps.println(teEntry.getKey() + " " + teEntry.getValue());
    }
    ps.close();
}

From source file:MainClass.java

public static void main(String[] args) {

    try {/*w  ww .j a v a2s  .  com*/
        String FILE = "c:\\systemin.txt";
        FileOutputStream outStr = new FileOutputStream(FILE, true);
        PrintStream printStream = new PrintStream(outStr);

        System.setErr(printStream);

        Timestamp now = new Timestamp(System.currentTimeMillis());
        System.out.println(now.toString() + ": This is text that should go to the file");

        outStr.close();
        printStream.close();
    } catch (FileNotFoundException ex) {
        ex.printStackTrace();
        System.exit(-1);
    } catch (IOException ex) {
        ex.printStackTrace();
        System.exit(-1);
    }
}

From source file:cc.wikitools.lucene.FindWikipediaArticleId.java

@SuppressWarnings("static-access")
public static void main(String[] args) throws Exception {
    Options options = new Options();
    options.addOption(/*from  w  w  w .jav a 2 s . c om*/
            OptionBuilder.withArgName("path").hasArg().withDescription("index location").create(INDEX_OPTION));
    options.addOption(
            OptionBuilder.withArgName("string").hasArg().withDescription("article title").create(TITLE_OPTION));

    CommandLine cmdline = null;
    CommandLineParser parser = new GnuParser();
    try {
        cmdline = parser.parse(options, args);
    } catch (ParseException exp) {
        System.err.println("Error parsing command line: " + exp.getMessage());
        System.exit(-1);
    }

    if (!cmdline.hasOption(TITLE_OPTION) || !cmdline.hasOption(INDEX_OPTION)) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp(FindWikipediaArticleId.class.getName(), options);
        System.exit(-1);
    }

    File indexLocation = new File(cmdline.getOptionValue(INDEX_OPTION));
    if (!indexLocation.exists()) {
        System.err.println("Error: " + indexLocation + " does not exist!");
        System.exit(-1);
    }

    String title = cmdline.getOptionValue(TITLE_OPTION);

    PrintStream out = new PrintStream(System.out, true, "UTF-8");

    WikipediaSearcher searcher = new WikipediaSearcher(indexLocation);
    int id = searcher.getArticleId(title);

    out.println(title + ": id = " + id);

    searcher.close();
    out.close();
}

From source file:MainClass.java

public static void main(String[] args) throws IOException {
    PrintStream console = System.out;
    BufferedInputStream in = new BufferedInputStream(new FileInputStream("Redirecting.java"));
    PrintStream out = new PrintStream(new BufferedOutputStream(new FileOutputStream("test.out")));
    System.setIn(in);/* www . ja v  a2  s.  co  m*/
    System.setOut(out);
    System.setErr(out);
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String s;
    while ((s = br.readLine()) != null)
        System.out.println(s);
    out.close();
    System.setOut(console);
}

From source file:org.apache.accumulo.core.util.CreateToken.java

public static void main(String[] args) {
    Opts opts = new Opts();
    opts.parseArgs(CreateToken.class.getName(), args);

    Password pass = opts.password;//from w w w . ja va2  s  .c o  m
    if (pass == null && opts.securePassword != null) {
        pass = opts.securePassword;
    }

    try {
        String principal = opts.principal;
        if (principal == null) {
            principal = getConsoleReader().readLine("Username (aka principal): ");
        }

        AuthenticationToken token = Class.forName(opts.tokenClassName).asSubclass(AuthenticationToken.class)
                .newInstance();
        Properties props = new Properties();
        for (TokenProperty tp : token.getProperties()) {
            String input;
            if (pass != null && tp.getKey().equals("password")) {
                input = pass.toString();
            } else {
                if (tp.getMask()) {
                    input = getConsoleReader().readLine(tp.getDescription() + ": ", '*');
                } else {
                    input = getConsoleReader().readLine(tp.getDescription() + ": ");
                }
            }
            props.put(tp.getKey(), input);
            token.init(props);
        }
        String tokenBase64 = Base64.encodeBase64String(AuthenticationTokenSerializer.serialize(token));

        String tokenFile = opts.tokenFile;
        if (tokenFile == null) {
            tokenFile = getConsoleReader().readLine("File to save auth token to: ");
        }
        File tf = new File(tokenFile);
        if (!tf.exists()) {
            tf.createNewFile();
        }
        PrintStream out = new PrintStream(new FileOutputStream(tf, true), true, Constants.UTF8.name());
        String outString = principal + ":" + opts.tokenClassName + ":" + tokenBase64;
        out.println(outString);
        out.close();
        System.out.println("Token written to " + tokenFile + ". Remember to upload it to hdfs.");
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Redirecting.java

public static void main(String[] args) throws IOException {
    PrintStream console = System.out;
    BufferedInputStream in = new BufferedInputStream(new FileInputStream("Redirecting.java"));
    PrintStream out = new PrintStream(new BufferedOutputStream(new FileOutputStream("test.out")));
    System.setIn(in);/*from www.  j  a v a2  s.  c om*/
    System.setOut(out);
    System.setErr(out);
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String s;
    while ((s = br.readLine()) != null)
        System.out.println(s);
    out.close(); // Remember this!
    System.setOut(console);
}

From source file:MainClass.java

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

    System.setProperty("javax.net.ssl.trustStore", "clienttrust");
    SSLSocketFactory ssf = (SSLSocketFactory) SSLSocketFactory.getDefault();
    Socket s = ssf.createSocket("127.0.0.1", 8888);

    OutputStream outs = s.getOutputStream();
    PrintStream out = new PrintStream(outs);
    InputStream ins = s.getInputStream();
    BufferedReader in = new BufferedReader(new InputStreamReader(ins));

    out.println("Hi,How are u!");
    out.println("");
    String line = null;/*ww w .  ja v  a  2  s  .c  om*/
    while ((line = in.readLine()) != null) {
        System.out.println(line);
    }

    in.close();
    out.close();
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    ServerSocket ssock = new ServerSocket(1234);
    Socket sock = ssock.accept();
    ssock.close();//from   w  ww .j av  a 2  s  . c  om

    PrintStream pstream = new PrintStream(sock.getOutputStream());
    pstream.print("count? ");
    BufferedReader input = new BufferedReader(new InputStreamReader(sock.getInputStream()));
    String line = input.readLine();
    pstream.println("");
    int count = Integer.parseInt(line);
    for (int i = count; i >= 0; i--) {
        pstream.println(i);
    }
    pstream.close();
    sock.close();
}