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

public static void main(String[] argv) throws Exception {
    BufferedReader br = new BufferedReader(new FileReader("in.txt"));
    BufferedWriter bw = new BufferedWriter(new FileWriter("out.txt"));
    int i;//from  w  w  w . j  a va  2  s  .  c o  m
    do {
        i = br.read();
        if (i != -1) {
            if (Character.isLowerCase((char) i))
                bw.write(Character.toUpperCase((char) i));
            else if (Character.isUpperCase((char) i))
                bw.write(Character.toLowerCase((char) i));
            else
                bw.write((char) i);
        }
    } while (i != -1);
    br.close();
    bw.close();
}

From source file:TabFilter.java

public static void main(String args[]) throws Exception {
    FileReader fr = new FileReader(args[0]);
    BufferedReader br = new BufferedReader(fr);

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

    // Convert tab to space characters
    String s;/*from  w  w  w .j  av  a  2 s.  co m*/
    while ((s = br.readLine()) != null) {
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == '\t')
                c = ' ';
            bw.write(c);
        }
    }

    bw.flush();
    fr.close();
    fw.close();
}

From source file:AuthorizedFileWriter.java

public static void main(String[] args) {
    System.setSecurityManager(new SecurityManager());
    String file = "authorized.txt";
    String fileBody = "test";
    try {/*from w  w w  .  j av  a2  s.c o  m*/
        FileWriter fileWriter = new FileWriter(file);
        fileWriter.write(fileBody);
        fileWriter.close();
        System.exit(0);
    } catch (IOException ioException) {
        ioException.printStackTrace();
        System.exit(1);
    }
}

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();//from  w w w.  jav a  2  s.com

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

    Scanner src = new Scanner(fin);

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

public static void main(String[] argv) throws IOException {
    // java.util.Random methods are non-static, do need to construct Math
    Random r = new Random();
    PrintWriter file1 = new PrintWriter(new FileWriter("file1"));
    PrintWriter file2 = new PrintWriter(new FileWriter("file2"));
    for (int i = 0; i < 10000; i++) {
        file1.println(r.nextDouble());//from  w w w  . jav  a2  s  .  c  om
        file2.println(r.nextGaussian());
    }
    file1.close();
    file2.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    URL url = new URL("http://www.google.com");

    BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
    BufferedWriter writer = new BufferedWriter(new FileWriter("data.html"));

    String line;/*ww w.  j av  a2  s  .co  m*/
    while ((line = reader.readLine()) != null) {
        System.out.println(line);
        writer.write(line);
        writer.newLine();
    }

    reader.close();
    writer.close();
}

From source file:MainClass.java

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

    FileWriter fout = new FileWriter("test.txt");
    fout.write("string true false 1 2 3 4.12");
    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();
}

From source file:MainClass.java

License:asdf

public static void main(String[] args) throws IOException {
    try {//from w w w .ja  va2  s .com
        BufferedReader in4 = new BufferedReader(new StringReader("asdf"));
        PrintWriter out1 = new PrintWriter(new BufferedWriter(new FileWriter("IODemo.out")));
        int lineCount = 1;
        String s = null;
        while ((s = in4.readLine()) != null)
            out1.println(lineCount++ + ": " + s);
        out1.close();
    } catch (EOFException e) {
        System.err.println("End of stream");
    }

}

From source file:MainClass.java

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

    int i;//  w  w w .  j a va 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();
}

From source file:CursorWriter.java

public static void main(String[] args) throws Exception {
    String fileName = "yourXML.xml";
    XMLOutputFactory xof = XMLOutputFactory.newInstance();
    XMLStreamWriter xtw = null;//w  w w.  j  av a  2s  .  c o m
    xtw = xof.createXMLStreamWriter(new FileWriter(fileName));
    xtw.writeComment("all elements here are explicitly in the HTML namespace");
    xtw.writeStartDocument("utf-8", "1.0");
    xtw.setPrefix("html", "http://www.w3.org/TR/REC-html40");
    xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "html");
    xtw.writeNamespace("html", "http://www.w3.org/TR/REC-html40");
    xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "head");
    xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "title");
    xtw.writeCharacters("character");
    xtw.writeEndElement();
    xtw.writeEndElement();
    xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "body");
    xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "p");
    xtw.writeCharacters("another character");
    xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "a");
    xtw.writeAttribute("href", "http://www.java2s.com");
    xtw.writeCharacters("here");
    xtw.writeEndElement();
    xtw.writeEndElement();
    xtw.writeEndElement();
    xtw.writeEndElement();
    xtw.writeEndDocument();
    xtw.flush();
    xtw.close();
    System.out.println("Done");
}