Acitropy
Acitropy

Reputation: 113

How to Write Data (String) to Text File

I currently have the lines of code:

ResultSet RS = cs.executeQuery();
....
FileOutputStream outFile = new FileOutputStream(file, false);
while(RS.next()) {
    outFile.write(RS.getString(1));
}

There is only one column in the resultset. I'm getting an error that RS.getString(1) must be an int.

Upvotes: 0

Views: 7697

Answers (2)

Reimeus
Reimeus

Reputation: 159804

FileOutputStream#write expects an int value. You could simply use a PrintWriter here instead which has an overloaded write method for writing String values:

PrintWriter writer = new PrintWriter(new FileOutputStream(file, false));
writer.write(...);

or to write each String on a new line use println

writer.println(...);

Upvotes: 2

Otávio Santana
Otávio Santana

Reputation: 328

PLease try it.

     public static void main(String[] arg) throws IOException{
    File file=new File("C:\\file.txt");
    String text="myText";
    FileOutputStream fileOutputStream=new FileOutputStream(file);
    fileOutputStream.write(text.getBytes());
    fileOutputStream.close();

}

Upvotes: 0

Related Questions