Example usage for java.io BufferedWriter BufferedWriter

List of usage examples for java.io BufferedWriter BufferedWriter

Introduction

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

Prototype

public BufferedWriter(Writer out) 

Source Link

Document

Creates a buffered character-output stream that uses a default-sized output buffer.

Usage

From source file:Main.java

public static void writeNewFile(String filePath, String fileContents) {
    File f = new File(filePath);
    if (f.exists()) {
        f.delete();/*w w  w  .j a va  2  s . c o m*/
    }

    try {
        // Create file 
        FileWriter fstream = new FileWriter(f);
        BufferedWriter out = new BufferedWriter(fstream);
        out.write(fileContents);
        //Close the output stream
        out.close();
    } catch (Exception e) {
        Log.d(TAG, "Failed to create " + filePath + " File contents: " + fileContents);
    }
}

From source file:Main.java

public static void writeListToFile(String filepath, List<String> data) {
    try {//from w ww .  ja  v a 2 s.c om
        File file = new File(filepath);
        if (!file.exists()) {
            file.createNewFile();
        }

        FileWriter fileWriter = new FileWriter(file);
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
        for (String line : data) {
            bufferedWriter.write(line);
            bufferedWriter.newLine();
        }

        bufferedWriter.flush();
        bufferedWriter.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void writeContentToFile(String fileName, String contents) throws IOException {
    Log.d("writeContentToFile", fileName);
    File f = new File(fileName);
    f.getParentFile().mkdirs();/*w ww.  j av  a 2s .co m*/
    File tempFile = new File(fileName + ".tmp");
    FileWriter fw = new FileWriter(tempFile);
    BufferedWriter bw = new BufferedWriter(fw);
    int length = contents.length();
    if (length > 0) {
        bw.write(contents);
        //         int apart =  Math.min(length, 65536);
        //         int times = length / apart;
        //         for (int i = 0; i < times; i++) {
        //            bw.write(contents, i * apart, apart);
        //         }
        //         if (length % apart != 0) {
        //            bw.write(contents, times * apart, length - times * apart);
        //         }
        bw.flush();
        fw.flush();
        bw.close();
        fw.close();
        f.delete();
        tempFile.renameTo(f);
    }
}

From source file:experiments.SimpleExample.java

/**
 * Starts the example./*from   w  w w . ja  v  a2 s . co m*/
 * 
 * @param args
 *            if optional first argument provided, it represents the number
 *            of bits to use, but no more than 32
 * 
 * @author Neil Rotstan
 * @author Klaus Meffert
 * @throws IOException 
 * @since 2.0
 */
public static void main(String[] args) throws IOException {

    SimpleExample se = new SimpleExample();

    try {
        File[] result = { new File("ga_x.txt"), new File("ga_cos.txt"), new File("ga_ackley.txt"),
                new File("ga_quar.txt"), new File("ga_step.txt"), new File("ga_rosen.txt"),
                new File("ga_sch.txt"), new File("ga_gri.txt"), new File("ga_pen1.txt"),
                new File("ga_pen2.txt"), new File("ga_wei.txt"), new File("ga_non.txt") };
        BufferedWriter[] output = new BufferedWriter[result.length];
        for (int i = 0; i <= result.length - 1; i++) {
            if (result[i].exists()) {
                result[i].delete();
                if (result[i].createNewFile()) {
                    System.out.println("result" + i + " file create success!");
                } else {
                    System.out.println("result" + i + " file create failed!");
                }
            } else {
                if (result[i].createNewFile()) {
                    System.out.println("result" + i + " file create success!");
                } else {
                    System.out.println("result" + i + " file create failed!");
                }

            }
            output[i] = new BufferedWriter(new FileWriter(result[i]));
        }

        for (int a = 0; a <= 0; a++) {
            //            se.runga(100, 30, 40, -100,  100, new MaxFunction(), output[0]);
            //            se.runga(200, 30, 40, -100,  100, new MaxFunction(), output[0]);
            //            se.runga(120, 30, 40, -5.12,  5.12, new CosMaxFunction(), output[1]);
            //            se.runga(200, 30, 40, -5.12,  5.12, new CosMaxFunction(), output[1]);
            //            se.runga(120, 30, 40, -32,  32, new AckleyMaxFunction(), output[2]);
            se.runga(2000, 30, 40, -32, 32, new AckleyMaxFunction(), output[2]);
            //            se.runga(120, 30, 40, -100,  100, new QuardircMaxFunction(), output[3]);
            //            se.runga(200, 30, 40, -100,  100, new QuardircMaxFunction(), output[3]);
            //            se.runga(120, 30, 40, -100,  100, new StepMaxFunction(), output[4]);
            //            se.runga(200, 30, 40, -100,  100, new StepMaxFunction(), output[4]);
            //            se.runga(120, 30, 40, -30,  30, new RosenbrockMaxFunction(), output[5]);
            //            se.runga(200, 30, 40, -30,  30, new RosenbrockMaxFunction(), output[5]);
            //            se.runga(120, 30, 40, -500,  500, new SchwefelMaxFunction(), output[6]);
            //            se.runga(200, 30, 40, -500,  500, new SchwefelMaxFunction(), output[6]);
            //            se.runga(120, 30, 40, -600,  600, new GriewankMaxFunction(), output[7]);
            //            se.runga(200, 30, 40, -600,  600, new GriewankMaxFunction(), output[7]);
            //            se.runga(120, 30, 40, -50,  50, new PenalizedMaxFunction(), output[8]);
            //            se.runga(200, 30, 40, -50,  50, new PenalizedMaxFunction(), output[8]);
            //            se.runga(120, 30, 40, -50,  50, new Penalized2MaxFunction(), output[9]);
            //            se.runga(200, 30, 40, -50,  50, new Penalized2MaxFunction(), output[9]);
            //            se.runga(120, 30, 40, -5.12,  5.12, new WeiMaxFunction(), output[10]);
            //            se.runga(200, 30, 40, -5.12,  5.12, new WeiMaxFunction(), output[10]);
            //            se.runga(120, 30, 40, -0.5,  0.5, new NonMaxFunction(), output[11]);
            //            se.runga(200, 30, 40, -0.5,  0.5, new NonMaxFunction(), output[11]);
            for (BufferedWriter op : output) {
                op.write("\n");
                op.flush();
            }
        }

        for (BufferedWriter op : output) {
            op.close();
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:Main.java

public static void writeFile(String path, String content, boolean append) throws FileNotFoundException {
    //File file = new File(path);
    FileOutputStream writerStream = new FileOutputStream(path, append);
    BufferedWriter writer = null;
    try {/*from w  ww. java  2s .c  om*/
        writer = new BufferedWriter(new OutputStreamWriter(writerStream, "UTF-8"));
        writer.write(content);
        writer.flush();
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (IOException e1) {
            }
        }
    }
}

From source file:Main.java

public static void toStream(final String str, final OutputStream stream, final Charset charset)
        throws IOException {
    final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stream, charset));
    try {/*from  w ww  . ja  v  a  2s.c  o m*/
        writer.write(str);
    } finally {
        writer.flush();
        writer.close();
    }
}

From source file:Main.java

public static String getURLContent(String urlStr) throws Exception {
    URL url = new URL(urlStr);
    URLConnection connection = url.openConnection();
    connection.setDoOutput(true);//from w w  w  . ja  v a 2  s .c om
    connection.connect();
    OutputStream ous = connection.getOutputStream();
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(ous));
    bw.write("index.htm");
    bw.flush();
    bw.close();

    printRequestHeaders(connection);
    InputStream ins = connection.getInputStream();

    BufferedReader br = new BufferedReader(new InputStreamReader(ins));
    StringBuffer sb = new StringBuffer();
    String msg = null;
    while ((msg = br.readLine()) != null) {
        sb.append(msg);
        sb.append("\n"); // Append a new line
    }
    br.close();
    return sb.toString();
}

From source file:MainClass.java

public static void Converter(String input, String output) {
    try {//from  ww  w .j  a  v  a 2s  .co  m
        FileInputStream fis = new FileInputStream(new File(input));
        BufferedReader in = new BufferedReader(new InputStreamReader(fis, "SJIS"));

        FileOutputStream fos = new FileOutputStream(new File(output));
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(fos, "UTF8"));

        int len = 80;
        char buf[] = new char[len];

        int numRead;
        while ((numRead = in.read(buf, 0, len)) != -1)
            out.write(buf, 0, numRead);

        out.close();
        in.close();
    } catch (IOException e) {
        System.out.println("An I/O Exception Occurred: " + e);
    }
}

From source file:cz.cuni.mff.peckam.ais.statistics.IonogramStatistics.java

/**
 * @param args 0 => Orbit base dir, 1 => min orbit, 2 => max orbit
 * @throws IOException On IO error./*from   w w  w.  j av  a 2s  . c om*/
 */
public static void main(String[] args) throws IOException {
    final File orbitDir = new File(args[0]);
    final int minOrbit = Integer.parseInt(args[1]);
    final int maxOrbit = Integer.parseInt(args[2]);

    try (final BufferedWriter meanWriter = new BufferedWriter(new FileWriter("mean"));
            final BufferedWriter sdWriter = new BufferedWriter(new FileWriter("sd"));
            final BufferedWriter maxWriter = new BufferedWriter(new FileWriter("max"));
            final BufferedWriter featuresWriter = new BufferedWriter(new FileWriter("features"));
            final BufferedWriter traceWriter = new BufferedWriter(new FileWriter("trace"))) {
        final IonogramStatistics stats = new IonogramStatistics(meanWriter, sdWriter, maxWriter, featuresWriter,
                traceWriter);

        for (int i = minOrbit; i <= maxOrbit; i++) {
            final File orbitFile = new File(orbitDir,
                    (i + "").replaceAll(".$", "X") + File.separator + "FRM_AIS_RDR_" + i + ".LBL");
            if (orbitFile.exists()) {
                stats.execute(orbitFile);
            }
        }
    }
    System.out.println("Minimal value: " + min);
}

From source file:Main.java

/**
 * Write in a text file/*from ww w .j a  v  a 2  s  . c  om*/
 * 
 * @param file
 *            in which we write
 * 
 * @param text
 *            that we write
 * 
 * @param append
 *            indicates whether or not to append to an existing file
 * 
 * @throws IOException
 *             Input/Output exceptions
 */
public static void writeTextFile(File file, String text, boolean append) throws IOException {
    BufferedWriter writer = new BufferedWriter(new FileWriter(file, append));
    writer.write(text);
    writer.close();
}