Example usage for java.io FileWriter FileWriter

List of usage examples for java.io FileWriter FileWriter

Introduction

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

Prototype

public FileWriter(FileDescriptor fd) 

Source Link

Document

Constructs a FileWriter given a file descriptor, using the platform's java.nio.charset.Charset#defaultCharset() default charset .

Usage

From source file:MainClass.java

public static void main(String args[]) {
    try {//from   w w  w.  j  a  v  a  2 s . c o m
        FileWriter fw = new FileWriter(args[0]);

        BufferedWriter bw = new BufferedWriter(fw);

        for (int i = 0; i < 12; i++) {
            bw.write("Line " + i + "\n");
        }

        bw.close();
    } catch (Exception e) {
        System.out.println("Exception: " + e);
    }
}

From source file:MainClass.java

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

    FileWriter fw = new FileWriter(args[0]);
    BufferedWriter bw = new BufferedWriter(fw);
    PrintWriter pw = new PrintWriter(bw, false);

    pw.println(true);/*from   ww w. j  ava2s  .c  o m*/
    pw.println('A');
    pw.println(500);
    pw.println(40000L);
    pw.println(45.67f);
    pw.println(45.67);
    pw.println("Hello");
    pw.println(new Integer("99"));

    pw.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    BufferedWriter bufferedWriter = null;
    bufferedWriter = new BufferedWriter(new FileWriter("yourFile.txt"));
    bufferedWriter.write("string");
    bufferedWriter.newLine();/*  w ww . j  a va  2 s  . com*/
    bufferedWriter.write("string");
}

From source file:MainClass.java

public static void main(String[] args) {
    try {//from   w  w w.  j  a  va 2 s.co  m
        FileWriter fw = new FileWriter("LPT1:");

        PrintWriter pw = new PrintWriter(fw);
        String s = "www.java2s.com";

        int i, len = s.length();

        for (i = 0; len > 80; i += 80) {
            pw.print(s.substring(i, i + 80));
            pw.print("\r\n");
            len -= 80;
        }

        if (len > 0) {
            pw.print(s.substring(i));
            pw.print("\r\n");
        }

        pw.close();
    } catch (IOException e) {
        System.out.println(e);
    }
}

From source file:Main.java

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

    int count = 0;
    double sum = 0.0;

    FileWriter fout = new FileWriter("test.txt");

    fout.write("2, 3.4,    5,6, 7.4, 9.1, 10.5, done");
    fout.close();//ww w.  j a  v  a  2s .  c  om

    FileReader fin = new FileReader("Test.txt");

    Scanner src = new Scanner(fin);

    src.useDelimiter(", *");

    while (src.hasNext()) {
        if (src.hasNextDouble()) {
            sum += src.nextDouble();
            count++;
        } else {
            String str = src.next();
            if (str.equals("done"))
                break;
            else {
                System.out.println("File format error.");
                return;
            }
        }
    }

    fin.close();
    System.out.println("Average is " + sum / count);
}

From source file:HTMLDemo.java

public static void main(String[] a) throws IOException {
    PrintWriter pw = new PrintWriter(new FileWriter("test.html"));
    DecimalFormat ff = new DecimalFormat("#0"), cf = new DecimalFormat("0.0");
    pw.println("<TABLE BORDER><TR><TH>Fahrenheit<TH>Celsius</TR>");
    for (double f = 100; f <= 400; f += 10) {
        double c = 5 * (f - 32) / 9;
        pw.println("<TR ALIGN=RIGHT><TD>" + ff.format(f) + "<TD>" + cf.format(c));
    }//w w  w  .  j  a  v  a  2s .  c om
    pw.println("</TABLE>");
    pw.close(); // Without this, the output file may be empty
}

From source file:Main.java

public static void main(String[] args) {
    String destFile = "test.txt";

    try (BufferedWriter bw = new BufferedWriter(new FileWriter(destFile))) {
        bw.append("test");
        bw.newLine();/*  w  ww . j a v  a 2  s . co m*/
        bw.append("test1");
        bw.newLine();
        bw.append("test2");
        bw.newLine();
        bw.append("test3");

        bw.flush();
    } catch (Exception e2) {
        e2.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    InputStream in = System.in;
    BufferedWriter bw = new BufferedWriter(new FileWriter("output.txt"));

    int letter;//  w w  w.  j a v a2s. co m

    while ((letter = in.read()) != -1) {
        bw.write((char) letter);
        bw.flush();
    }
}

From source file:MainClass.java

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

    FileWriter fout = new FileWriter("test.txt");
    fout.write("2, 3.4,    5,6, 7.4, 9.1, 10.5, done");
    fout.close();//ww w.  j a v  a  2s.  c  o  m

    FileReader fin = new FileReader("Test.txt");
    Scanner src = new Scanner(fin);
    // Set delimiters to space and comma.
    // ", *" tells Scanner to match a comma and zero or more spaces as
    // delimiters.

    src.useDelimiter(", *");

    // Read and sum numbers.
    while (src.hasNext()) {
        if (src.hasNextDouble()) {
            System.out.println(src.nextDouble());
        } else {
            break;
        }
    }
    fin.close();
}

From source file:FileCopy.java

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

    FileReader fr = new FileReader(args[0]);

    // Create a file writer
    FileWriter fw = new FileWriter(args[1]);

    // Read and copy characters
    int i;//from  w  ww .  j av a2  s  .  com
    while ((i = fr.read()) != -1) {
        fw.write(i);
    }
    fw.close();

    fr.close();
}