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

public static void main(String args[]) throws IOException {
    int i;/*from   ww w .  jav  a2  s. co  m*/
    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

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

    int i;//w  w  w.ja  v  a  2 s.  co 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:FileWriterDemo.java

public static void main(String args[]) throws Exception {
    FileWriter fw = new FileWriter(args[0]);
    // Write strings to the file
    for (int i = 0; i < 12; i++) {
        fw.write("Line " + i + "\n");
    }//from   ww w  .  j  av a  2s  .  c  o m

    // Close file writer
    fw.close();
}

From source file:MainClass.java

public static void main(String[] a) {
    FileWriter writer;
    try {//from ww w. ja  v  a 2  s .  c o  m
        writer = new FileWriter("myFile.txt");
        char c = 'A';
        writer.write(c);
        writer.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

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]);
    }// w w w  .ja v a2 s.  com
    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[]) {

    try {/*from w w w. ja va2 s.c o  m*/

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

public static void main(String[] argv) throws Exception {
    FileWriter fw = new FileWriter("file.dat");
    String strs[] = { "com", "exe", "doc" };

    for (int i = 0; i < strs.length; i++) {
        fw.write(strs[i] + "\n");
    }//from w  w w .j  a v a2  s. co m
    fw.close();
}

From source file:Copy.java

public static void main(String[] args) throws IOException {
    File inputFile = new File("farrago.txt");
    File outputFile = new File("outagain.txt");

    FileReader in = new FileReader(inputFile);
    FileWriter out = new FileWriter(outputFile);
    int c;//from  www .  j av  a 2s .  c  o m

    while ((c = in.read()) != -1)
        out.write(c);

    in.close();
    out.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;/*  ww w . j av a  2 s . c  o  m*/
    while ((i = fr.read()) != -1) {
        fw.write(i);
    }
    fw.close();

    fr.close();
}

From source file:contractEditor.SPConfFileEditor.java

public static void main(String[] args) {

    JSONObject obj = new JSONObject();
    ArrayList fileList = new ArrayList();

    fileList.add("confSP" + File.separator + "HOST1.json");
    fileList.add("confSP" + File.separator + "HOST2.json");
    fileList.add("confSP" + File.separator + "HOST3.json");
    fileList.add("confSP" + File.separator + "HOST4.json");

    obj.put("contract_list_of_SP", fileList);

    try {//from www  .j av  a2  s .  c  om

        FileWriter file = new FileWriter("confSP" + File.separator + "contractFileList.json");
        file.write(obj.toJSONString());
        file.flush();
        file.close();

    } catch (IOException e) {
        e.printStackTrace();
    }

    System.out.print(obj);

}