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[] args) {
    String text = "JFileChooser, you're my only friend.";

    JFileChooser chooser = new JFileChooser();
    int result = chooser.showSaveDialog(null);

    if (result == JFileChooser.APPROVE_OPTION) {
        try {/*from w w w .  j  av a 2 s.  c o  m*/
            File file = chooser.getSelectedFile();
            FileWriter writer = new FileWriter(file);
            writer.write(text);
            writer.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTextField nameTextField = new JTextField();
    frame.add(nameTextField, BorderLayout.NORTH);

    FileWriter writer = null;//from  w ww. j av a  2 s . c  o  m
    try {
        writer = new FileWriter("filename.txt");
        nameTextField.write(writer);
    } catch (IOException exception) {
        System.err.println("Save oops");
        exception.printStackTrace();
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (IOException exception) {
                System.err.println("Error closing writer");
                exception.printStackTrace();
            }
        }
    }

    frame.setSize(250, 100);
    frame.setVisible(true);
}

From source file:AllCapsDemo.java

License:asdf

public static void main(String[] arguments) {
    String sourceName = "asdf";
    try {/*from w  w w.  j a  va2s  . co m*/
        File source = new File(sourceName);
        File temp = new File("cap" + sourceName + ".tmp");

        FileReader fr = new FileReader(source);
        BufferedReader in = new BufferedReader(fr);

        FileWriter fw = new FileWriter(temp);
        BufferedWriter out = new BufferedWriter(fw);

        boolean eof = false;
        int inChar = 0;
        do {
            inChar = in.read();
            if (inChar != -1) {
                char outChar = Character.toUpperCase((char) inChar);
                out.write(outChar);
            } else
                eof = true;
        } while (!eof);
        in.close();
        out.close();

        boolean deleted = source.delete();
        if (deleted)
            temp.renameTo(source);
    } catch (Exception se) {
        System.out.println("Error - " + se.toString());
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("JavaScript");

    File outputFile = new File("jsoutput.txt");
    System.out.println("Script output will be written to " + outputFile.getAbsolutePath());

    FileWriter writer = new FileWriter(outputFile);
    ScriptContext defaultCtx = engine.getContext();
    defaultCtx.setWriter(writer);//from w  w  w.ja  v  a2 s.c om
    String script = "print('Hello custom output writer')";
    engine.eval(script);
    writer.close();

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    ScriptContext ctx = new SimpleScriptContext();

    // Get the reader and writers from the script context
    Reader inputReader = ctx.getReader();
    Writer outputWriter = ctx.getWriter();
    Writer errWriter = ctx.getErrorWriter();

    // Write all script outputs to an out.txt file
    Writer fileWriter = new FileWriter("out.txt");
    ctx.setWriter(fileWriter);/*from   www . j  a v  a  2  s  . c  om*/
}

From source file:CharArrayWriterDemo.java

public static void main(String args[]) throws IOException {
    CharArrayWriter f = new CharArrayWriter();
    String s = "This should end up in the array";
    char buf[] = new char[s.length()];
    s.getChars(0, s.length(), buf, 0);/*from  ww  w. jav a  2 s .c  om*/
    f.write(buf);
    System.out.println(f.toString());

    char c[] = f.toCharArray();
    for (int i = 0; i < c.length; i++) {
        System.out.print(c[i]);
    }

    FileWriter f2 = new FileWriter("test.txt");
    f.writeTo(f2);
    f2.close();
    f.reset();
    for (int i = 0; i < 3; i++)
        f.write('X');
}

From source file:DataFileTest.java

public static void main(String[] args) {
    Employee staff = new Employee("Java Source", 35500);

    staff.raiseSalary(5.25);//from   w  w  w.ja  va 2s . c  om

    try {
        PrintWriter out = new PrintWriter(new FileWriter("employee.dat"));
        writeData(staff, out);
        out.close();
    } catch (IOException e) {
        System.out.print("Error: " + e);
        System.exit(1);
    }

    try {
        BufferedReader in = new BufferedReader(new FileReader("employee.dat"));
        Employee e = readData(in);
        e.print();
        in.close();
    } catch (IOException e) {
        System.out.print("Error: " + e);
        System.exit(1);
    }
}

From source file:Main.java

public static void main(String[] args) {
    try {/*from   w  ww. j a v a2s .  c o  m*/
        BufferedReader input = new BufferedReader(new FileReader(args[0]));
        ArrayList list = new ArrayList();
        String line;
        while ((line = input.readLine()) != null) {
            list.add(line);
        }
        input.close();

        Collections.reverse(list);

        PrintWriter output = new PrintWriter(new BufferedWriter(new FileWriter(args[1])));
        for (Iterator i = list.iterator(); i.hasNext();) {
            output.println((String) i.next());
        }
        output.close();
    } catch (IOException e) {
        System.err.println(e);
    }
}

From source file:OutputStreamDemo.java

public static void main(String[] args) throws IOException {
    OutputStream os1 = new FileOutputStream("tmp1.dat");
    writeints("Unbuffered: ", 1000000, os1);
    OutputStream os2 = new BufferedOutputStream(new FileOutputStream("tmp2.dat"));
    writeints("Buffered:   ", 1000000, os2);
    Writer wr1 = new FileWriter("tmp1.dat");
    writeints("Unbuffered: ", 1000000, wr1);
    Writer wr2 = new BufferedWriter(new FileWriter("tmp2.dat"));
    writeints("Buffered:   ", 1000000, wr2);
}

From source file:SVGGraphics2DDemo.java

public static void main(String args[]) throws IOException {
    SVGGraphics2DDemo sv2Demo = new SVGGraphics2DDemo();
    DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
    Document doc = domImpl.createDocument(null, "svg", null);
    SVGGraphics2D svg = new SVGGraphics2D(doc);
    sv2Demo.paint(svg);/*from  ww  w .  j a v  a 2s. c  o  m*/
    svg.stream(new FileWriter("booktitle.svg"), false);
}