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:InputOutputDemo.java

public static void main(String[] a) throws Exception {
    PrintWriter pwr = new PrintWriter(new FileWriter("java2s.txt"));
    pwr.print(4711);/* w w w  . j a  v  a  2 s  .  c o m*/
    pwr.print(' ');
    pwr.print("Java Source and Support at www.java2s.com");
    pwr.close();

    StreamTokenizer stok = new StreamTokenizer(new FileReader("java2s.txt"));
    int tok = stok.nextToken();
    while (tok != StreamTokenizer.TT_EOF) {
        System.out.println(stok.sval);
        tok = stok.nextToken();
    }
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    String s = "<xmltag atr=value>tag data</xmltag>";
    FileWriter fr = new FileWriter(new File("a.txt"));
    Writer br = new BufferedWriter(fr);
    br.write(s);//w ww  .j a va2  s  .  c  om
    br.close();
}

From source file:FileReaderWriterExample.java

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

    Reader r = new FileReader("in.txt");
    Writer w = new FileWriter("out.txt");
    int c;//from  w  ww  .jav  a 2s.c  o m
    while ((c = r.read()) != -1) {
        System.out.print((char) c);
        w.write(c);
    }
    r.close();
    w.close();
}

From source file:MainClass.java

public static void main(String args[]) throws IOException {
    // Write output to a file.
    FileWriter fout = new FileWriter("test.txt");
    fout.write("2 3.4 5 6 7.4 9.1 10.5 done");
    fout.close();/*www  . j av a 2s . co  m*/

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

    Scanner src = new Scanner(fin);

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

From source file:MainClass.java

public static void main(String args[]) {

    try {//from   w ww  .  ja v a2s  . com

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

        FileWriter fw = new FileWriter(args[1]);

        int i;
        while ((i = fr.read()) != -1) {
            fw.write(i);
        }
        fw.close();

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

From source file:MainClass.java

public static void main(String args[]) throws IOException {
    // Write output to a file.
    FileWriter fout = new FileWriter("test.txt");
    fout.write("int: 1  double 1.0  boolean true");
    fout.close();// w w w  . ja  va  2  s. c om

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

    Scanner src = new Scanner(fin);

    while (src.hasNext()) {
        if (src.hasNextInt()) {
            System.out.println("int: " + src.nextInt());
        } else if (src.hasNextDouble()) {
            System.out.println("double: " + src.nextDouble());
        } else if (src.hasNextBoolean()) {
            System.out.println("boolean: " + src.nextBoolean());
        } else {
            System.out.println(src.next());
        }
    }
    fin.close();
}

From source file:Main.java

License:asdf

public static void main(String[] argv) throws Exception {
    File temp = File.createTempFile("pattern", ".dat");

    temp.deleteOnExit();//from www. j  ava2 s  . c om

    BufferedWriter out = new BufferedWriter(new FileWriter(temp));
    out.write("asdf");
    out.close();
}

From source file:Main.java

public static void main(String args[]) throws IOException {
    String source = "test";
    char buffer[] = new char[source.length()];
    source.getChars(0, source.length(), buffer, 0);

    FileWriter f0 = new FileWriter("file1.txt");
    for (int i = 0; i < buffer.length; i += 2) {
        f0.write(buffer[i]);/*from  ww w. j a  v  a 2  s . co m*/
    }
    f0.close();

    FileWriter f1 = new FileWriter("file2.txt");
    f1.write(buffer);
    f1.close();

    FileWriter f2 = new FileWriter("file3.txt");

    f2.write(buffer, buffer.length - buffer.length / 4, buffer.length / 4);
    f2.close();
}

From source file:MainClass.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();/*w  w  w .j a  va 2 s . com*/

    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:ScanMixed.java

public static void main(String args[]) throws IOException {
    int i;//from w  w  w. j  av  a 2 s .com
    double d;
    boolean b;
    String str;

    FileWriter fout = new FileWriter("test.txt");
    fout.write("Testing Scanner 10 12.2 one true two false");
    fout.close();

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

    Scanner src = new Scanner(fin);

    while (src.hasNext()) {
        if (src.hasNextInt()) {
            i = src.nextInt();
            System.out.println("int: " + i);
        } else if (src.hasNextDouble()) {
            d = src.nextDouble();
            System.out.println("double: " + d);
        } else if (src.hasNextBoolean()) {
            b = src.nextBoolean();
            System.out.println("boolean: " + b);
        } else {
            str = src.next();
            System.out.println("String: " + str);
        }
    }

    fin.close();
}