Hi,
Welcome
to Java-recent, a blog on Java related concepts.
Today,in
this post we will discuss about reading/writing from/to a file programmatically.
In
most of the cases developers would have faced a scenario to read
/write from files like
logs
etc.
In
Java we have many ways to read/write from files using input and
output streams[File input/out put streams].
The
most popular,effective way of achieving this is by using Buffered
Reader and Buffered Writer classes in Java.
First
lets look at the definition of both the classes mentioned above.
Buffered
Reader :-
- Reads the text from a character-input stream by buffering character so as to provide efficient way of reading characters, and lines. (java.io.BufferedReader)
- There are two ways of creating BufferedReader object
- BufferedReader br = new BufferedReader(Reader r);Here we are passing a single argument Reader,reader may be like FileReader etc.The default size of buffer will be used,if we want to customize the buffer size then use second way as mentioned below.
- BufferedReader br = new BufferedReader(Reader rn,int sz);There are two arguments one will be a Reader like FileReader etc,the other one will be setting custom buffer size(in no of bytes)
- Default size of buffer is 8192 bytes
Buffered
Writer:-
- Writes text to a character-input stream by buffering character so as to provide efficient way of writing characters, and lines (java.io.BufferedWriter)
- There are two ways of creating BufferedWriter object
- BufferedWriter br = new BufferedWriter(Writer r);Here we are passing a single argument Writer ,writer may be FileWriter class etc.
- BufferedWriter bw = new BufferedWriter(Writer wr,int sz);There are two arguments one will be a Writer like FileWriter etc,the other one will be setting custom buffer size(in no of bytes)
- Default size of buffer is 8192 bytes
Lets
see an example of reading /writing content to text files
Example
:-
FileRead.java
public
class
FileRead {
//
This method reads the file content
public
void
readFile() throws
IOException {
//
Using file reader to read a text file in F drive
FileReader
fr = new
FileReader("F:\\JavaRecent.txt");
BufferedReader
br =
new
BufferedReader(fr);
//
readLine() will read a line from a file
String
sLine = br.readLine();
while
(sLine != null)
{
System.out.println(sLine);
sLine
= br.readLine();
}
}
//Method
to write content to a file
public
void
writeFile() throws
IOException {
BufferedWriter
br = null;
try
{
FileWriter
fw = new
FileWriter("F:\\WriteFile.txt");
br
= new
BufferedWriter(fw);
br.write("Hi");
br.newLine();
br.append("Welcome
to tutorial on File Writing2");
}
finally
{
br.flush();
br.close();
}
}
public
static
void
main(String[] args) {
FileRead
fileReader = new
FileRead();
try
{
fileReader.readFile();
fileReader.writeFile();
}
catch
(FileNotFoundException e) {
e.printStackTrace();
}
catch
(IOException ioe) {
ioe.printStackTrace();
}
}
}
Explanation
:-
Methods
:-
- main() :- is used to create Object of the class,call the methods readFile(), writeFile() and handle the exceptionsFileRead fileReader = new FileRead();try {fileReader.readFile();fileReader.writeFile();}
Created
an Object for FileRead class and calling respective methods in try block
- readFile():- is custom method used in reading data from the file “JavaRecent.txt”FileReader fr = new FileReader("F:\\JavaRecent.txt");
- Creating FileReader object and reference fr by passing location of the file to be read,we can pass absolute or relative path.
- After this we pass fr reference to BufferedReader object
- readLine() method is used to read the content line by line,we are iterating using while loop until there are no lines
- readLine() will return null @ EOF (End of the file)
- writeFile():- Is custom method used for writing the data to a file WriteFile.txtFileWriter fw = new FileWriter("F:\\WriteFile.txt");
- Creating FileWriter object and reference fw by passing the location of the file to be written.
- If the file is absent, a new one will be created
- If the file is present,it will be modified
- There are different methods to write content to a file,most popular are write() and append();
In my next posts we will see how to read content from excel files(.xls and .xlsx)
For
complete reference of the methods refer
http://docs.oracle.com/javase/6/docs/api/java/io/BufferedWriter.html
and
Happy
Learning
Please
provide your valuable comments on this article and share it across
your network.
Contact
me @ sudheer@javarecent.com
or admin@java-recent.com
No comments:
Post a Comment