Java file handling tutorial
This tutorial demonstrates you the basic Java file handling concepts with code examples.
|Name:||Java file handling tutorial|
|Product:||See complete product|
Bookmark Java file handling tutorial
Step 1 - Java file handling basics
Java file handling tutorial
One of the most frequently used task in programming is writing to and reading from a file. To do this in Java there are more possibilities. At this time only the most frequently used text file handling solutions will be presented.
You are probably aware that Java is one of the most popular programming languages that people use. It is so popular because it can be used so widely from application software to web applications. You will find it being used by people everywhere such as those who are writing programmes and even those who work for big companies like O2.co.uk. As a language it is class-based, concurrent, general purpose and object-orientated. This allows it to have as few implementation dependencies as possible.
To write anything to a file first of all we need a file name we want to use. The file name is a simple string like like this:
- String fileName = "test.txt";
If you want to write in a file which is located elsewhere you need to define the
complete file name and path in your fileName variable:
- String fileName = "c:\\filedemo\\test.txt";
However if you define a path in your file name then you have to take care the path separator. On windows system the '\' is used and you need to backslash it so you need to write '\\', in Unix,Linux systems
the separator is a simple slash '/'.
To make your code OS independent you can get the separator character as follows:
- String separator = File.separator;
Open a file
To open a file for writing use the FileWriter class and create an instance from it.
The file name is passed in the constructor like this:
- FileWriter writer = new FileWriter(fileName);
This code opens the file in overwrite mode. If you want to append to the file then
you need to use an other constructor like this:
- FileWriter writer = new FileWriter(fileName,true);
Besides this the constructor can throw an IOException so we put all of the code inside
a try-catch block.
Write to file
At this point we have a writer object and we can send real content to the file. You can do this using the write() method, which has more variant but the most commonly used requires a string as input parameter.
Calling the write() method doesn't mean that it immediately writes the data into the file. The output is maybe cached so if you want to send your data immediately to the file you need to call the flush() method.
As last step you should close the file with the close() method and you are done.
The basic write method looks like this:
However in a real world situation the FileWriter usually not used directly. Instead of FileWriter the BufferedWriter or from Java 1.5 the PrintWriter are used. These writer objects gives you more flexibility to handle your IO. Here is a simple BufferedWriter example:
Next Step of Java file handling tutorial
Tags: java file handling tutorial, java file handling, file handling tutorial, java, file
|Java file handling tutorial - Table of contents|
|Step 1 - Java file handling basics|
|Step 2 - Reading from a file|