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:edu.berkeley.compbio.ncbitaxonomy.Exporter.java

public static void main(String[] argv) {
    try {/*from   www .j a  v  a2 s .  co  m*/
        ApplicationContext ctx = NcbiTaxonomyDbContextFactory.makeNcbiTaxonomyDbContext();
        NcbiTaxonomyPhylogeny ncbi = (NcbiTaxonomyPhylogeny) ctx.getBean("ncbiTaxonomyPhylogeny");

        FileWriter treeWriter = new FileWriter("tree.newick");
        ncbi.toNewick(treeWriter, "", "", 0, 0);
        treeWriter.close();

        FileWriter synonymWriter = new FileWriter("synonyms");
        ncbi.writeSynonyms(synonymWriter);
        synonymWriter.close();

        for (String rank : ranks) {
            FileWriter rankWriter = new FileWriter(rank);
            Set<Integer> ids = ncbi.getTaxIdsWithRank(rank);
            for (Integer id : ids) {
                rankWriter.append(id.toString()).append("\n");
            }
            rankWriter.close();
        }
    } catch (Throwable e) {
        e.printStackTrace();
    }
}

From source file:Transform2.java

public static void main(String[] args) throws Exception {
    SAXBuilder builder = new SAXBuilder();
    Document document = builder.build(new File("r.xml"));
    Transformer transformer = TransformerFactory.newInstance().newTransformer(new StreamSource("program.xsl"));
    JDOMResult out = new JDOMResult();
    transformer.transform(new JDOMSource(document), out);
    XMLOutputter xmlOutputter = new XMLOutputter();
    xmlOutputter.output(out.getDocument(), new FileWriter("JDOMAlteredRichard.html"));
}

From source file:com.mysql.servlet.NewClass.java

public static void main(String[] args) {

    JSONObject headerFile = new JSONObject();

    JSONArray listOfAttrs = new JSONArray();
    listOfAttrs.add("id" + "");
    listOfAttrs.add("name" + "");
    listOfAttrs.add("age" + "");
    listOfAttrs.add("salary" + "");
    listOfAttrs.add("grade" + "");

    headerFile.put("Employee", listOfAttrs);

    try {/*  www . j  a  va 2s .  c om*/

        // Writing to a file  
        File file = new File("D:\\Employees.json");
        file.createNewFile();
        FileWriter fileWriter = new FileWriter(file);
        System.out.println("Writing JSON object to file");
        System.out.println("-----------------------");
        System.out.print(headerFile);

        fileWriter.write(headerFile.toJSONString());
        fileWriter.flush();
        fileWriter.close();

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

}

From source file:Main.java

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

    String file = url.getFile();/*from  w w w .jav a 2s.c  o  m*/
    Socket clientSocket = new Socket(websiteAddress, 80);

    BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

    OutputStreamWriter outWriter = new OutputStreamWriter(clientSocket.getOutputStream());
    outWriter.write("GET " + file + " HTTP/1.0\r\n\n");
    outWriter.flush();
    BufferedWriter out = new BufferedWriter(new FileWriter(file));
    boolean more = true;
    String input;
    while (more) {
        input = inFromServer.readLine();
        if (input == null)
            more = false;
        else {
            out.write(input);
        }
    }
    out.close();
    clientSocket.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
    System.out.println(stdin.readLine());
    BufferedReader in = new BufferedReader(new FileReader("Main.java"));
    String s, s2 = new String();
    while ((s = in.readLine()) != null)
        s2 += s + "\n";
    in.close();/*from   w  ww .j  a v  a 2s . co m*/
    StringReader in1 = new StringReader(s2);
    int c;
    while ((c = in1.read()) != -1)
        System.out.print((char) c);
    BufferedReader in2 = new BufferedReader(new StringReader(s2));
    PrintWriter out1 = new PrintWriter(new BufferedWriter(new FileWriter("IODemo.out")));
    int lineCount = 1;
    while ((s = in2.readLine()) != null)
        out1.println(lineCount++ + ": " + s);
    out1.close();
}

From source file:LogTest.java

public static void main(String[] args) throws IOException {
    String inputfile = args[0];/*  w  w  w.j  a v a 2s .  c om*/
    String outputfile = args[1];

    Map<String, Integer> map = new TreeMap<String, Integer>();

    Scanner scanner = new Scanner(new File(inputfile));
    while (scanner.hasNext()) {
        String word = scanner.next();
        Integer count = map.get(word);
        count = (count == null ? 1 : count + 1);
        map.put(word, count);
    }
    scanner.close();

    List<String> keys = new ArrayList<String>(map.keySet());
    Collections.sort(keys);

    PrintWriter out = new PrintWriter(new FileWriter(outputfile));
    for (String key : keys)
        out.println(key + " : " + map.get(key));
    out.close();
}

From source file:Main.java

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

    File xml = new File("yourFile.xml");

    Document doc = (Document) new SAXBuilder().build(xml);
    Element rootNode = doc.getRootElement();
    List list = rootNode.getChildren("staff");
    XMLOutputter xmlOut = new XMLOutputter();

    for (int i = 0; i < list.size(); i++) {

        Element node = (Element) list.get(i);
        if (node.getChildText("firstname").equals("sanjay"))
            node.getChild("salary").getChild("basic").setText("250000");
        xmlOut.setFormat(Format.getPrettyFormat());
        xmlOut.output(doc, new FileWriter("yourFile.xml"));
    }//from  w w w .j  a v a  2s .  co  m
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    Statement st = conn.createStatement();

    st.executeUpdate("create table survey (Id int, b CLOB);");

    PreparedStatement pstmt = conn.prepareStatement("INSERT INTO survey VALUES(1,?)");

    File file = new File("c:/Java_Dev/data.txt");

    FileReader reader = new FileReader(file);
    pstmt.setCharacterStream(1, reader);

    pstmt.execute();//from www .j  a  v  a2 s .com

    ResultSet resultSet = pstmt.executeQuery("select b from survey ");

    File data = new File("C:\\a.txt");
    Reader dataReader = resultSet.getCharacterStream(1);
    FileWriter writer = new FileWriter(data);
    char[] buffer = new char[1];
    while (dataReader.read(buffer) > 0) {
        writer.write(buffer);
    }
    writer.close();

    reader.close();
    st.close();
    conn.close();
}

From source file:jsonpractice.Jsonpractice.java

public static void main(String[] args) throws IOException {
    JSONObject countryObj = new JSONObject();
    countryObj.put("Name", "India");
    countryObj.put("Population", 1000000);

    JSONArray listOfStates = new JSONArray();
    listOfStates.add("Madhya Pradesh");
    listOfStates.add("Maharastra");
    listOfStates.add("Tamil Nadu");

    countryObj.put("States", listOfStates);

    try {/*from   ww w. java  2 s . c om*/
        File file = new File("C:\\Users\\user\\Documents\\CountryJSONFile.json");
        file.createNewFile();
        try (FileWriter fileWriter = new FileWriter(file)) {
            System.out.println("Writing JSON OBJECT to file");
            System.out.println("-----------------------------");
            System.out.println(countryObj);

            fileWriter.write(countryObj.toJSONString());
            fileWriter.flush();
        } catch (IOException e) {
        }

    } catch (IOException e) {
    }
}

From source file:json.WriteToFile.java

public static void main(String[] args) {
    JSONObject countryObj = new JSONObject();
    countryObj.put("Name", "Kenya");
    countryObj.put("Population", new Integer(430000000));
    JSONArray listOfCounties = new JSONArray();
    listOfCounties.add("Nairobi");
    listOfCounties.add("Kiambu");
    listOfCounties.add("Murang'a");

    countryObj.put("Counties", listOfCounties);
    try {//from   ww  w .j a va  2 s . c  om
        //writing to file
        File file = new File("/home/mars/Desktop/JsonExample1.json");
        file.createNewFile();
        FileWriter fw = new FileWriter(file);
        System.out.println("Writing JSON object to file");
        System.out.println("---------------------------");
        System.out.println(countryObj);

        fw.write(countryObj + "");
        fw.flush();
        fw.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}