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) throws Exception {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    Connection conn = DriverManager.getConnection(url, username, password);
    PreparedStatement stmt = conn.prepareStatement("SELECT name, description, data FROM documents ");
    ResultSet resultSet = stmt.executeQuery();
    while (resultSet.next()) {
        String name = resultSet.getString(1);
        String description = resultSet.getString(2);
        File data = new File("C:\\a.txt");
        Reader reader = resultSet.getCharacterStream(3);
        FileWriter writer = new FileWriter(data);
        char[] buffer = new char[1];
        while (reader.read(buffer) > 0) {
            writer.write(buffer);//from  w  w  w. ja  va  2 s  . co m
        }
        writer.close();
    }
    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getHSQLConnection();
    System.out.println("Got Connection.");
    Statement st = conn.createStatement();
    st.executeUpdate("create table survey (id int,name varchar);");
    st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')");
    st.executeUpdate("insert into survey (id,name ) values (2,'anotherValue')");

    Statement stmt = conn.createStatement();
    String sqlQuery = "SELECT * FROM survey WHERE id='1'";
    WebRowSet webRS = new WebRowSetImpl();
    webRS.setCommand(sqlQuery);/*from  ww  w.  j a  v  a 2 s .com*/
    webRS.execute(conn);

    File file = new File("1.xml");
    FileWriter fw = new FileWriter(file);
    System.out.println("Writing db data to file " + file.getAbsolutePath());
    webRS.writeXml(fw);

    StringWriter sw = new StringWriter();
    webRS.writeXml(sw);
    System.out.println(sw.toString());
    fw.flush();
    fw.close();
    stmt.close();
    conn.close();
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    JPanel panel = new JPanel(new BorderLayout());
    JLabel label = new JLabel("Name: ");
    label.setDisplayedMnemonic(KeyEvent.VK_N);
    JTextField textField = new JTextField();
    label.setLabelFor(textField);/*from w w  w  .  ja v  a2  s. co m*/
    panel.add(label, BorderLayout.WEST);
    panel.add(textField, BorderLayout.CENTER);

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(panel, BorderLayout.NORTH);
    frame.add(new JButton("Somewhere Else"), BorderLayout.SOUTH);
    frame.setSize(250, 150);
    frame.setVisible(true);

    textField.setText("your text");
    String filename = "test.txt";

    FileWriter writer = new FileWriter(filename);
    textField.write(writer);
    writer.close();

}

From source file:de.tudarmstadt.ukp.dkpro.keyphrases.wikipediafilter.filter.util.FileFilter.java

public static void main(String[] args) throws IOException {
    Set<String> articles = new HashSet<String>();
    articles.addAll(FileUtils.readLines(new File("target/wikipedia/articles.txt")));

    BufferedWriter writer = new BufferedWriter(new FileWriter(new File("target/passwords.txt.filtered")));
    for (String line : FileUtils.readLines(new File("target/passwords.txt"))) {
        if (articles.contains(line.split("\t")[0].toLowerCase())) {
            writer.write(line);//w w w.  j  a  va 2s. co  m
            writer.newLine();
        }
        writer.close();
    }
}

From source file:edu.usc.ee599.CommunityStats.java

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

    File dir = new File("results5");
    PrintWriter writer = new PrintWriter(new FileWriter("results5_stats.txt"));

    File[] files = dir.listFiles();

    DescriptiveStatistics statistics1 = new DescriptiveStatistics();
    DescriptiveStatistics statistics2 = new DescriptiveStatistics();
    for (File file : files) {

        BufferedReader reader = new BufferedReader(new FileReader(file));

        String line1 = reader.readLine();
        String line2 = reader.readLine();

        int balanced = Integer.parseInt(line1.split(",")[1]);
        int unbalanced = Integer.parseInt(line2.split(",")[1]);

        double bp = (double) balanced / (double) (balanced + unbalanced);
        double up = (double) unbalanced / (double) (balanced + unbalanced);

        statistics1.addValue(bp);// w w w .j a va2  s  . c o m
        statistics2.addValue(up);

    }

    writer.println("AVG Balanced %: " + statistics1.getMean());
    writer.println("AVG Unbalanced %: " + statistics2.getMean());

    writer.println("STD Balanced %: " + statistics1.getStandardDeviation());
    writer.println("STD Unbalanced %: " + statistics2.getStandardDeviation());

    writer.flush();
    writer.close();

}

From source file:Main.java

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

    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    System.out.println("schema factory instance obtained is " + sf);

    Schema schema = sf.newSchema(new File(args[0]));
    System.out.println("schema obtained is = " + schema);
    Validator validator = schema.newValidator();

    String fileName = args[1].toString();
    String fileName2 = args[2].toString();
    javax.xml.transform.Result xmlResult = new javax.xml.transform.stax.StAXResult(
            XMLOutputFactory.newInstance().createXMLStreamWriter(new FileWriter(fileName2)));
    javax.xml.transform.Source xmlSource = new javax.xml.transform.stax.StAXSource(getXMLEventReader(fileName));
    validator.validate(new StreamSource(args[1]));
    validator.validate(xmlSource, xmlResult);

}

From source file:LabelSampleSaveText.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Label Focus Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel(new BorderLayout());
    JLabel label = new JLabel("Name: ");
    label.setDisplayedMnemonic(KeyEvent.VK_N);
    JTextField textField = new JTextField();
    label.setLabelFor(textField);/*from w ww  .j av  a 2s  .  c  o m*/
    panel.add(label, BorderLayout.WEST);
    panel.add(textField, BorderLayout.CENTER);
    frame.add(panel, BorderLayout.NORTH);
    frame.add(new JButton("Somewhere Else"), BorderLayout.SOUTH);
    frame.setSize(250, 150);
    frame.setVisible(true);

    textField.setText("your text");
    String filename = "test.txt";

    FileWriter writer = null;
    try {
        writer = new FileWriter(filename);
        textField.write(writer);
    } catch (IOException exception) {
        System.err.println("Save oops");
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (IOException exception) {
                System.err.println("Error closing writer");
                exception.printStackTrace();
            }
        }
    }
}

From source file:com.tomgibara.cluster.CreateGaussianMouse.java

public static void main(String[] args) throws IOException {
    GaussianRandomGenerator gen = new GaussianRandomGenerator(new JDKRandomGenerator());
    FileWriter writer = new FileWriter("R/gmouse.txt");
    try {//from  w  ww.jav a 2s .c  om
        writeCluster(gen, new double[] { 0, 0 }, new double[] { 4, 4 }, 100, writer);
        writeCluster(gen, new double[] { -4, 4 }, new double[] { 2, 2 }, 50, writer);
        writeCluster(gen, new double[] { 4, 4 }, new double[] { 2, 2 }, 50, writer);
    } finally {
        writer.close();
    }
}

From source file:ArrayFile.java

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

    PrintWriter out = new PrintWriter(new FileWriter("arrayfile.txt"));

    CaBioApplicationService appService = (CaBioApplicationService) ApplicationServiceProvider
            .getApplicationServiceFromUrl("http://cabioapi-qa.nci.nih.gov/cabio42");

    ArrayAnnotationService am = new ArrayAnnotationServiceImpl(appService);

    String arrayName = "HG-U133_Plus_2";

    Collection<ArrayReporter> reps = am.getReportersForPlatform(arrayName);

    out.println("Name\tSymbol\tHUGO Symbol\tUnigene Id\t" + "Sequence Type\tSequence Source\tTarget Id\t"
            + "Target Description\tCytoband\tChromosome\tStart\tEnd");

    int c = 0;/*www .ja  va 2 s .  c  o  m*/
    for (ArrayReporter rep : reps) {

        ExpressionArrayReporter er = (ExpressionArrayReporter) rep;

        out.println(resolve(er, "name") + "\t" + resolve(er, "gene.symbol") + "\t"
                + resolve(er, "gene.hugoSymbol") + "\t" + resolve(er, "gene.clusterId") + "\t"
                + resolve(er, "sequenceType") + "\t" + resolve(er, "sequenceSource") + "\t"
                + resolve(er, "targetId") + "\t" + resolve(er, "targetDescription") + "\t"
                + resolve(er, "cytogeneticLocationCollection[0].startCytoband.name") + "\t"
                + resolve(er, "physicalLocationCollection[0].chromosome.number") + "\t"
                + resolve(er, "physicalLocationCollection[0].chromosomalStartPosition") + "\t"
                + resolve(er, "physicalLocationCollection[0].chromosomalEndPosition"));

        if (c++ > 500)
            break;
    }

    out.close();
}

From source file:com.tomgibara.cluster.CreateUniformMouse.java

public static void main(String[] args) throws IOException {
    UniformRandomGenerator gen = new UniformRandomGenerator(new JDKRandomGenerator());
    FileWriter writer = new FileWriter("R/umouse.txt");
    try {/*from   w  w w.  j av  a2 s.c om*/
        writeCluster(gen, new double[] { 0, 0 }, new double[] { 4, 4 }, 300, writer);
        writeCluster(gen, new double[] { -4, 4 }, new double[] { 2, 2 }, 100, writer);
        writeCluster(gen, new double[] { 4, 4 }, new double[] { 2, 2 }, 100, writer);
    } finally {
        writer.close();
    }
}