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 append(File aFile, String content) {
    try {//from  ww  w .  ja va  2  s . com
        PrintStream p = new PrintStream(new BufferedOutputStream(new FileOutputStream(aFile, true)));
        p.println(content);
        p.close();

    } catch (Exception e) {
        e.printStackTrace();
        System.err.println(aFile);
    }
}

From source file:Main.java

public static void writeString(Context context, String content, String path) {
    try {//  ww w.  j  av  a2s .c  o  m
        FileOutputStream fos = context.openFileOutput(path, Context.MODE_PRIVATE);
        PrintStream ps = new PrintStream(fos);
        ps.print(content);
        ps.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Returns stacktrace of current thread represented as String
 * /*from w  ww .j a  v a  2s .c o m*/
 * @return stacktrace of current thread represented as String
 */
public static String getStackTrace() {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PrintStream printStream = new PrintStream(baos);
    // noinspection ThrowableInstanceNeverThrown
    new Exception("Stack trace").printStackTrace(printStream);
    printStream.close();
    return new String(baos.toByteArray());
}

From source file:com.opengamma.analytics.example.coupledfokkerplank.CoupledFokkerPlankExample.java

public static void runCoupledFokkerPlank(PrintStream out) throws FileNotFoundException, IOException {
    final ExtendedCoupledFiniteDifference solver = new ExtendedCoupledFiniteDifference(0.5);
    final int tNodes = 50;
    final int xNodes = 150;

    final MeshingFunction timeMesh = new ExponentialMeshing(0, T, tNodes, 5.0);
    final MeshingFunction spaceMesh = new HyperbolicMeshing(LOWER.getLevel(), UPPER.getLevel(), SPOT, xNodes,
            0.01);/*from   w  ww  .ja  v  a  2  s  . co  m*/

    final double[] timeGrid = new double[tNodes];
    for (int n = 0; n < tNodes; n++) {
        timeGrid[n] = timeMesh.evaluate(n);
    }

    final double[] spaceGrid = new double[xNodes];
    for (int i = 0; i < xNodes; i++) {
        spaceGrid[i] = spaceMesh.evaluate(i);
    }
    final PDEGrid1D grid = new PDEGrid1D(timeGrid, spaceGrid);

    final PDEResults1D[] res = solver.solve(DATA1, DATA2, grid, LOWER, UPPER, LOWER, UPPER, null);
    final PDEFullResults1D res1 = (PDEFullResults1D) res[0];
    final PDEFullResults1D res2 = (PDEFullResults1D) res[1];

    // output in JSON format without using a JSON library to save dependencies
    StrBuilder buf = new StrBuilder(2048).append('{');

    ByteArrayOutputStream state_1_stream = new ByteArrayOutputStream();
    PrintStream state_1_out = new PrintStream(state_1_stream, true);
    PDEUtilityTools.printSurface("State 1 density", res1, state_1_out);
    state_1_out.close();
    buf.append("\"state_1_data\":\"").append(state_1_stream.toString()).append("\",");

    ByteArrayOutputStream state_2_stream = new ByteArrayOutputStream();
    PrintStream state_2_out = new PrintStream(state_2_stream, true);
    PDEUtilityTools.printSurface("State 2 density", res2, state_2_out);
    state_2_out.close();
    buf.append("\"state_2_data\":\"").append(state_2_stream.toString()).append("\"}");

    buf.replaceAll("\t", "\\t").replaceAll("\r\n", "\\r\\n").replaceAll("\n", "\\n");

    out.print(buf.toString());
}

From source file:Main.java

public static void printNode(Node node, String fn) {
    try {//w  w  w  .  j av  a 2  s .  co m
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer;
        transformer = tFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        DOMSource source = new DOMSource(node);
        PrintStream out = System.out;
        if (fn != null)
            out = new PrintStream(new FileOutputStream(fn));
        StreamResult result = new StreamResult(out);
        transformer.transform(source, result);
        out.close();
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerException e) {

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

From source file:de.uni.bremen.monty.moco.Main.java

private static void writeOutput(String llvmCode, File outputFile) throws FileNotFoundException {
    PrintStream resultStream = new PrintStream(outputFile);
    resultStream.print(llvmCode);//from  ww w . j  av a 2  s  . c om
    resultStream.close();
}

From source file:edu.msu.cme.rdp.abundstats.cli.AbundMain.java

private static void processSamples(List<Sample> samples, List<AbundStatsCalculator> statCalcs, File resultDir,
        String prefix, RPlotter plotter) throws IOException {
    for (AbundStatsCalculator calc : statCalcs) {
        float[][] matrix = AbundStatUtils.computeSymMatrix(samples, calc);
        File matrixOut = new File(resultDir, prefix + calc.getCalcName() + ".txt");
        PrintStream out = new PrintStream(matrixOut);
        AbundStatUtils.writeMatrix(out, samples, matrix);
        out.close();

        if (plotter != null) {
            plotter.generateRPlot(resultDir.getParentFile(), matrixOut,
                    new File(resultDir, prefix + calc.getCalcName()), prefix + calc.getCalcName());
        }//w ww  .  j  a  va2  s  .  c  o m
    }

}

From source file:de.uni.bremen.monty.moco.Main.java

private static File buildExecutable(String outputFileName, String inputFileName, boolean compileOnly,
        String llvmCode) throws IOException, InterruptedException {
    File outputFile = null;//from w  ww  .java  2  s . c  om
    if (outputFileName != null) {
        outputFile = new File(outputFileName);
    } else if (inputFileName != null) {
        outputFile = new File(FilenameUtils.removeExtension(inputFileName));
    } else if (compileOnly) {
        outputFile = File.createTempFile("output", null, null);
        outputFile.deleteOnExit();
    } else {
        outputFile = new File("output");
    }

    ProcessBuilder llcProcessBuilder = new ProcessBuilder("llc", "-O=2");
    Process llcProcess = llcProcessBuilder.start();
    PrintStream llcInput = new PrintStream(llcProcess.getOutputStream());
    llcInput.print(llvmCode);
    llcInput.close();

    ProcessBuilder ccProcessBuilder = new ProcessBuilder("cc", "-x", "assembler", "-o",
            outputFile.getAbsolutePath(), "-");
    Process ccProcess = ccProcessBuilder.start();
    IOUtils.copy(llcProcess.getInputStream(), ccProcess.getOutputStream());
    ccProcess.getOutputStream().close();

    System.err.print(IOUtils.toString(llcProcess.getErrorStream()));
    System.err.print(IOUtils.toString(ccProcess.getErrorStream()));
    return outputFile;
}

From source file:com.twinsoft.convertigo.engine.util.FileUtils.java

public static void saveProperties(Map<String, String> map, File file, String encoding) throws IOException {
    PrintStream ps = new PrintStream(file, encoding);
    for (Entry<String, String> entry : map.entrySet()) {
        ps.println(entry.getKey());/*from  w  w w .  j  ava 2s  .co  m*/
        ps.println(entry.getValue());
        ps.println();
    }
    ps.close();
}

From source file:gaffer.accumulo.utils.IngestUtils.java

/**
 * Get the existing splits from a table in Accumulo and write a splits file.
 * The number of splits is returned./* w  w  w  . j a  v  a  2s . c  o m*/
 * 
 * @param conn  An existing connection to an Accumulo instance
 * @param table  The table name
 * @param fs  The FileSystem in which to create the splits file
 * @param splitsFile  A path for the splits file
 * @return The number of splits in the table
 * @throws TableNotFoundException
 * @throws IOException
 */
public static int createSplitsFile(Connector conn, String table, FileSystem fs, Path splitsFile)
        throws TableNotFoundException, IOException {
    // Get the splits from the table
    Collection<Text> splits = conn.tableOperations().getSplits(table);

    // Write the splits to file
    if (splits.isEmpty()) {
        return 0;
    }
    PrintStream out = new PrintStream(new BufferedOutputStream(fs.create(splitsFile, true)));
    for (Text split : splits) {
        out.println(new String(Base64.encodeBase64(split.getBytes())));
    }
    out.close();

    return splits.size();
}