Example usage for java.io FileWriter write

List of usage examples for java.io FileWriter write

Introduction

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

Prototype

public void write(int c) throws IOException 

Source Link

Document

Writes a single character.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    File file = new File("user.txt");

    FileWriter writer = new FileWriter(file, true);
    writer.write("username=java;password=secret" + System.getProperty("line.separator"));
    writer.flush();/*from w ww.  ja v  a  2s  .  com*/
    writer.close();
}

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  ww  .j av a2s. c om
            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 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();/*from ww w  .  ja  v  a2 s  . c o  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[]) throws IOException {
    // Write output to a file.
    FileWriter fout = new FileWriter("test.txt");
    fout.write("int: 1  double 1.0  boolean true");
    fout.close();//from   w w  w .  jav  a  2s .  c o  m

    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: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();//from w  ww  . 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: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:ScanMixed.java

public static void main(String args[]) throws IOException {
    int i;/*from w w  w. ja  va 2s.  c  o  m*/
    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: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 .j  a  v  a2s .c om*/

    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: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();/*from  w  w  w  .  ja va 2s. c  o m*/

    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: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  ww.  jav a2  s .co m*/

    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);
}