java2db

Write String to file using FileWriter in java



The package java.io provides services to do all file input and output operations.See the below example to Write String to file.

Write String to a file in java First example (to Write String to file) :

This example writes a simple string value in a text file.
Write String to a file in java Create a file (Example : write.txt) in any valid location (Example: C:\\Java2dbExamples\\read.txt).
Write String to a file in java Specify the location of the (write.txt) file to write in the File constructor (line 14).
Write String to a file in java Write a simple String value (Ex : “Hi hello”) to the file using write() method in FileWriter class.
Write String to a file in java Finally close the file.
Note : While doing this , it throws an IOException. So handle it by throws IOException or using try,catch block.

 

Write String to a file in java Second Example (Read a String from one file and write it in another file)  :

It will read the data from a file in a string format and writes it to an another file.

Write String to a file in java create both input (Ex: read.txt) and output(Ex: write.txt) files  .
Write String to a file in java Type some text data in read.txt file.

Write String to a file in java Specify the location of the files in the File constructors to read(C:\\Java2dbExamples\\read.txt) and ….write(C:\\Java2dbExamples\\write.txt)  .

Write String to a file in java Read the data from file read.txt, and write it to write.txt file.
Write String to a file in java Finally close all the files.

Note : While doing the , it throws an IOException. So handle it by throws IOException or using try,catch block.

 Note : It throws FileNotFoundException , if the input or output files does not exist in the specified location.

 




Related Posts :
Get or Set Last Modified Date and Time for a File or directory in java
How to create a file from createNewFile method in java
   [will not be published]



^ <