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:org.apache.hadoop.fs.TestCopyFiles.java

static String execCmd(FsShell shell, String... args) throws Exception {
    ByteArrayOutputStream baout = new ByteArrayOutputStream();
    PrintStream out = new PrintStream(baout, true);
    PrintStream old = System.out;
    System.setOut(out);/*from   ww w  .j  av  a  2 s  . c om*/
    shell.run(args);
    out.close();
    System.setOut(old);
    return baout.toString();
}

From source file:org.apache.hadoop.hive.common.JavaUtils.java

public static void closeClassLoader(ClassLoader loader) throws IOException {
    if (loader instanceof Closeable) {
        ((Closeable) loader).close();
    } else if (SUN_MISC_UTIL_RELEASE != null && loader instanceof URLClassLoader) {
        PrintStream outputStream = System.out;
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        PrintStream newOutputStream = new PrintStream(byteArrayOutputStream);
        try {//from w  w w. ja  va 2 s  .  c  o  m
            // SUN_MISC_UTIL_RELEASE.invoke prints to System.out
            // So we're changing the outputstream for that call,
            // and setting it back to original System.out when we're done
            System.setOut(newOutputStream);
            SUN_MISC_UTIL_RELEASE.invoke(null, loader);
            String output = byteArrayOutputStream.toString("UTF8");
            LOG.debug(output);
        } catch (InvocationTargetException e) {
            if (e.getTargetException() instanceof IOException) {
                throw (IOException) e.getTargetException();
            }
            throw new IOException(e.getTargetException());
        } catch (Exception e) {
            throw new IOException(e);
        } finally {
            System.setOut(outputStream);
            newOutputStream.close();
        }
    }
    LogFactory.release(loader);
}

From source file:net.sf.taverna.raven.helloworld.TestHelloWorld.java

@Test
public void runWithPrinter() throws Exception {
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    PrintStream printStream = new PrintStream(outStream);
    new HelloWorld().run(printStream);
    printStream.close();
    String printedString = new String(outStream.toByteArray());
    assertEquals("Did not print expected output", HelloWorld.TEST_DATA, printedString);
}

From source file:edu.msu.cme.rdp.framebot.stat.TaxonAbundance.java

public static void mapAbundance(File framebotResult, File lineagefile, String outfile,
        HashMap<String, Double> coveragetMap, double identity) throws IOException {
    HashMap<String, String> lineageMap = GetFrameBotStatMain.readDesc(lineagefile);
    HashMap<String, Double> rankMatchMap = new HashMap<String, Double>(); // taxon rank, 
    HashMap<String, Double> matchMap = new HashMap<String, Double>(); // match name

    double totalCount = 0.0;
    File[] files;//from   w  ww .j a  v a 2s  . c  om
    if (framebotResult.isDirectory()) {
        files = framebotResult.listFiles();
    } else {
        files = new File[1];
        files[0] = framebotResult;
    }
    String line;
    BufferedReader reader;
    for (File f : files) {
        reader = new BufferedReader(new FileReader(f));
        while ((line = reader.readLine()) != null) {
            if (!line.startsWith("STAT"))
                continue;
            String[] tokens = line.trim().split("\\t");
            double thisIdentity = Double.parseDouble(tokens[5]);
            if (thisIdentity < identity) {
                continue;
            }

            String match = tokens[1];

            String[] lineage = lineageMap.get(match).split(";");
            String taxonname = lineage[0].trim();
            if (lineage.length > 1) {
                taxonname = lineage[1];
                if (lineage[1].equalsIgnoreCase("Proteobacteria")) {// || lineage[1].equalsIgnoreCase("Bacteroidetes")){
                    taxonname = lineage[2].trim();
                }
            }
            if (taxonname.equals("")) {
                taxonname = "NA";
            }

            double count = 1.0;
            if (coveragetMap != null) {
                count = coveragetMap.get(tokens[2]);
            }
            Double prevCount = rankMatchMap.get(taxonname);
            if (prevCount != null) {
                rankMatchMap.put(taxonname, count + prevCount);
            } else {
                rankMatchMap.put(taxonname, count);
            }
            Double prevMatchCount = matchMap.get(match);
            if (prevMatchCount != null) {
                matchMap.put(match, count + prevMatchCount);
            } else {
                matchMap.put(match, count);
            }
            totalCount += count;
        }
        reader.close();
    }

    PrintStream out = new PrintStream(new File(outfile));
    // print out abundance group by phylum or class (within Proteobacteria)
    out.println("Taxon\tAbundance\tFraction Abundance");
    for (String taxonname : rankMatchMap.keySet()) {
        out.println(taxonname + "\t" + rankMatchMap.get(taxonname) + "\t"
                + String.format(dformat, rankMatchMap.get(taxonname) / totalCount));
    }

    // print out abundance by closest match   
    out.println("\n\nLineage\tMatchName\tAbundance\tFraction Abundance");
    for (String match : matchMap.keySet()) {
        String lineage = lineageMap.get(match);
        if (lineage.equals("")) {
            lineage = "NA";
        }
        out.println(lineage + "\t" + match + "\t" + matchMap.get(match) + "\t"
                + String.format(dformat, matchMap.get(match) / totalCount));
    }

    out.close();
}

From source file:Main.java

/**
 * Sends Gps data to emulator, and the start value has an offset.
 * /*from www  . j  a v a2 s.  c  om*/
 * @param number send times
 * @param offset is used to compute the start latitude and longitude
 * @param pause pause interval between each sending
 */
public static void sendGps(int number, int offset, int pause) {
    if (number < 1) {
        return;
    }

    int pauseInterval = TINY_WAIT_TIME;
    if (pause != -1) {
        pauseInterval = pause;
    }

    // If it's a real device, does not send simulated GPS signal.
    if (!isEmulator) {
        return;
    }

    PrintStream out = null;
    Socket socket = null;
    try {
        socket = new Socket(ANDROID_LOCAL_IP, emulatorPort);
        out = new PrintStream(socket.getOutputStream());
        double longitude = START_LONGITUDE + offset * DELTA_LONGITUDE;
        double latitude = START_LATITUDE + offset * DELTA_LADITUDE;
        for (int i = 0; i < number; i++) {
            out.println("geo fix " + longitude + " " + latitude);
            longitude += DELTA_LONGITUDE;
            latitude += DELTA_LADITUDE;
            Thread.sleep(pauseInterval);
        }
        // Wait the GPS signal can be obtained by MyTracks.
        Thread.sleep(SHORT_WAIT_TIME);
    } catch (UnknownHostException e) {
        System.exit(-1);
    } catch (IOException e) {
        System.exit(-1);
    } catch (InterruptedException e) {
        System.exit(-1);
    } finally {
        if (out != null) {
            out.close();
        }
    }
}

From source file:Main.java

/**
 * Sends Gps data to emulator, and the start value has an offset.
 * //from  w  w  w  .  j a  va  2 s. c  om
 * @param number send times
 * @param offset is used to compute the start latitude and longitude 
 * @param pause pause interval between each sending
 */
public static void sendGps(int number, int offset, int pause) {
    if (number < 1) {
        return;
    }

    int pauseInterval = PAUSE_DEFAULT;
    if (pause != -1) {
        pauseInterval = pause;
    }

    // If it's a real device, does not send simulated GPS signal.
    if (!isEmulator) {
        return;
    }

    PrintStream out = null;
    Socket socket = null;
    try {
        socket = new Socket(ANDROID_LOCAL_IP, emulatorPort);
        out = new PrintStream(socket.getOutputStream());
        double longitude = START_LONGITUDE + offset * DELTA_LONGITUDE;
        double latitude = START_LATITUDE + offset * DELTA_LADITUDE;
        for (int i = 0; i < number; i++) {
            out.println("geo fix " + longitude + " " + latitude);
            longitude += DELTA_LONGITUDE;
            latitude += DELTA_LADITUDE;
            Thread.sleep(pauseInterval);
        }
        // Wait the GPS signal can be obtained by MyTracks.  
        Thread.sleep(SHORT_WAIT_TIME);
    } catch (UnknownHostException e) {
        System.exit(-1);
    } catch (IOException e) {
        System.exit(-1);
    } catch (InterruptedException e) {
        System.exit(-1);
    } finally {
        if (out != null) {
            out.close();
        }
    }
}

From source file:net.sf.taverna.raven.helloworld.HelloWorld.java

public int launch(String[] args) throws IOException {
    if (args.length == 0) {
        run(System.out);//from ww  w  . j ava 2  s  .  c  om
    } else {
        PrintStream outStream = new PrintStream(args[0]);
        try {
            run(outStream);
        } finally {
            outStream.close();
        }
    }
    return 0;
}

From source file:Main.java

public void run() {
    try {/*from www.j  a  v a 2s. c om*/
        PrintStream pstream = new PrintStream(csocket.getOutputStream());
        for (int i = 10; i >= 0; i--) {
            pstream.println(i);
        }
        pstream.close();
        csocket.close();
    } catch (IOException e) {
        System.out.println(e);
    }
}

From source file:de.citec.sc.index.ProcessAnchorFile.java

public static void run(String filePath) {

    try {/*from   ww w.j  av  a  2  s. co m*/
        File file = new File("new.ttl");

        // if file doesnt exists, then create it
        if (file.exists()) {
            file.delete();
            file.createNewFile();
        }
        if (!file.exists()) {
            file.createNewFile();
        }
        BufferedReader wpkg = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "UTF-8"));
        String line = "";

        PrintStream ps = new PrintStream("new.ttl", "UTF-8");

        while ((line = wpkg.readLine()) != null) {

            String[] data = line.split("\t");

            if (data.length == 3) {
                String label = data[0];

                label = StringEscapeUtils.unescapeJava(label);

                try {
                    label = URLDecoder.decode(label, "UTF-8");
                } catch (Exception e) {
                }

                String uri = data[1].replace("http://dbpedia.org/resource/", "");
                uri = StringEscapeUtils.unescapeJava(uri);

                try {
                    uri = URLDecoder.decode(uri, "UTF-8");
                } catch (Exception e) {
                }

                String f = data[2];

                label = label.toLowerCase();
                ps.println(label + "\t" + uri + "\t" + f);

            }

        }

        wpkg.close();
        ps.close();

        File oldFile = new File(filePath);
        oldFile.delete();
        oldFile.createNewFile();

        file.renameTo(oldFile);

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

From source file:com.ibm.research.rdf.store.cmd.DumpRdfStore.java

@Override
public void doWork(Connection conn) {

    // create the store.
    try {//from  w  w w  .ja v a 2 s.  c  o m

        Store store = StoreManager.connectStore(conn, Backend.valueOf(params.get("-backend")),
                params.get("-schema"), storeName, Context.defaultContext);
        Dataset ds = RdfStoreFactory.connectDataset(store, conn, Backend.valueOf(params.get("-backend")));

        PrintStream ps = new PrintStream(new BufferedOutputStream(System.out, 1000000));
        RiotWriter.writeNQuads(ps, ds.asDatasetGraph());
        ps.close();
    } catch (RdfStoreException e) {
        log.error(e);
        System.out.println(e.getLocalizedMessage());
    } catch (Exception e) {
        log.error(e);
        System.out.println(e.getLocalizedMessage());
    }
}