Example usage for java.io PrintWriter PrintWriter

List of usage examples for java.io PrintWriter PrintWriter

Introduction

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

Prototype

public PrintWriter(File file) throws FileNotFoundException 

Source Link

Document

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

Usage

From source file:Main.java

public static void writeToExternalFile(String data, String logTag, String fileName) {

    if (!isExternalStorageWritable()) {
        Log.e(logTag, "failed to find external storage");
    } else {/*from   w  w w  .  jav a2s. com*/
        File path = Environment.getExternalStorageDirectory();
        File dir = new File(path.getAbsolutePath() + "/SDNController");
        if (!dir.isDirectory()) {
            if (!dir.mkdirs()) {
                Log.e(logTag, "sdn directory can not be created");
                return;
            }
        }

        File file = new File(dir, fileName);
        try {
            FileOutputStream f = new FileOutputStream(file, true);
            PrintWriter pw = new PrintWriter(f);
            pw.println(data);
            pw.flush();
            pw.close();
            f.close();
        } catch (FileNotFoundException e) {
            Log.e(logTag, "can not find indicated file");
            e.printStackTrace();
        } catch (IOException e) {
            Log.e(logTag, "failed to write SDNController/result.txt");
            e.printStackTrace();
        }
    }
}

From source file:com.yolodata.tbana.testutils.FileTestUtils.java

public static boolean createFileWithContent(String filepath, String content) throws IOException {
    File f = new File(filepath);
    if (!f.createNewFile())
        return false;

    PrintWriter pw = new PrintWriter(f);
    pw.write(content);//from  ww  w .ja  va 2  s  .  c om
    pw.close();

    return true;
}

From source file:Utilitarios.Imprimir.java

public void imprimirGrafico(Image image) throws IOException {

    FileWriter arq = new FileWriter("D:\\Orcamentos\\Grafico.doc");//cria o arquivo
    PrintWriter gravarArq = new PrintWriter(arq);

    gravarArq.println("Oramento Banzos Instituto de Msica "); //linha de conteudo do txt
    gravarArq.println(" ");
    gravarArq.println(image);//from   www.  ja v a 2  s  .  c  o  m

    arq.close();

}

From source file:de.cgarbs.lib.json.JSONDataModel.java

/**
 * Converts the DataModel to JSON format and writes the JSON data to the
 * specified file.//from ww w .  jav  a 2  s  .c o  m
 *
 * @param model the DataModel to be converted to JSON
 * @param file the File to be written to
 * @throws DataException either IO errors or errors during the JSON conversion
 */
public static void writeToFile(final DataModel model, final File file) throws DataException {
    PrintWriter out = null;
    try {
        out = new PrintWriter(file);
        out.write(convertToJSON(model));
        out.close();
    } catch (FileNotFoundException e) {
        throw wrappedAsDataException(DataException.ERROR.IO_ERROR, e);
    } catch (JSONException e) {
        throw wrappedAsDataException(DataException.ERROR.JSON_CONVERSION_ERROR, e);
    } finally {
        if (out != null) {
            out.close();
        }
    }
    if (out.checkError()) {
        // FIXME: PrintWriter throws no IOExceptions?!  how to find out what happened?
        throw new DataException(DataException.ERROR.IO_ERROR, "error writing to file");
    }
}

From source file:mrdshinse.sql2xlsx.util.FileUtil.java

public static File create(File file, String str) {
    try (PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(file)))) {
        pw.print(str);/*from  www.ja va  2 s  . c o  m*/
    } catch (IOException ex) {
        return file;
    }
    return file;
}

From source file:com.sec.ose.osi.util.tools.FileOperator.java

public static boolean saveFile(String filePath, String contents) {

    File writeFile = new File(filePath);

    PrintWriter writer = null;// w w  w  .  j  a  va  2s  .  c om

    try {
        writer = new PrintWriter(new FileWriter(writeFile));
        writer.println(contents);

    } catch (IOException e) {
        // TODO Auto-generated catch block
        log.warn(e);
        return false;
    } finally {

        if (writer != null) {
            writer.flush();
            writer.close();
        }
        writer = null;
    }

    return true;
}

From source file:minij.codeemission.CodeEmitter.java

public static String emitCode(Configuration config, List<Fragment<List<Assem>>> assemFragments,
        MachineSpecifics machineSpecifics) throws CodeEmitterException {

    try {/*from  w w w . j  a v  a 2  s. c o  m*/
        String assembly = machineSpecifics.printAssembly(assemFragments);
        ;

        if (config.codeEmission) {
            new File(FilenameUtils.getPath(config.outputFile)).mkdirs();
            PrintWriter out = new PrintWriter(config.outputFile);
            out.print(assembly);
            out.close();
        }

        Logger.logVerbosely("Successfully generated assembly");

        if (config.printAssembly) {
            Logger.log(assembly);
        }

        return assembly;
    } catch (Exception e) {
        throw new CodeEmitterException("Failed to generate assembly", e);
    }
}

From source file:com.splunk.shuttl.testutil.TUtilsTestNG.java

private static String getStackTrace(Exception exception) {
    StringWriter stackTraceStringWriter = new StringWriter();
    PrintWriter printWriter = new PrintWriter(stackTraceStringWriter);
    exception.printStackTrace(printWriter);
    printWriter.flush();/*  w ww.  j  av  a2 s.c om*/
    return stackTraceStringWriter.toString();
}

From source file:StringUtils.java

/**
 * Convert an exception to a String with full stack trace
 * @param ex the exception// w ww.j a  v a 2  s . c om
 * @return a String with the full stacktrace error text
 */
public static String getStringFromStackTrace(Throwable ex) {
    if (ex == null) {
        return "";
    }
    StringWriter str = new StringWriter();
    PrintWriter writer = new PrintWriter(str);
    try {
        ex.printStackTrace(writer);
        return str.getBuffer().toString();
    } finally {
        try {
            str.close();
            writer.close();
        } catch (IOException e) {
            //ignore
        }
    }
}

From source file:buspathcontroller.JSONFileParser.java

public void generateAllRoutes() {
    JSONParser parser = new JSONParser();
    try {//from  w  ww  .  jav  a2  s .  com
        PrintWriter writer = new PrintWriter("/Users/Zhaowei/Desktop/BusPath/allRoutes.txt");
        Object obj = parser.parse(new FileReader("/Users/Zhaowei/Desktop/BusPath/RawJSON/RouteList.json"));
        JSONObject jsonObject = (JSONObject) obj;
        JSONArray routes = (JSONArray) jsonObject.get("routes");
        Iterator iterator = routes.iterator();
        while (iterator.hasNext()) {
            JSONObject route = (JSONObject) ((JSONObject) iterator.next()).get("route");
            writer.println(route.get("name") + ";" + route.get("tag") + ";" + route.get("agency"));
        }
        writer.close();
    } catch (Exception e) {
        System.out.println(e);
    }
}