Java file handling tutorial


Home - Tutorials - File handling

This tutorial demonstrates you the basic Java file handling concepts with code examples.

Tutorial info:


Name:Java file handling tutorial
Total steps:2
Category:File handling
Date:2009-04-27
Level:Beginner
Product:See complete product
Viewed:95265

Bookmark Java file handling tutorial



AddThis Social Bookmark Button

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. 

Filename handling

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:

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:

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:

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:

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:   

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:

Code:
  1. public void writeFile() {
  2. String fileName = "c:\\test.txt";
  3.  
  4. try {
  5. FileWriter writer = new FileWriter(fileName,true);
  6. writer.write("Test text.");
  7. writer.close();
  8. } catch (IOException e) {
  9. e.printStackTrace();
  10. }
  11. }
  12.  

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:
Code:
  1. public void writeFile() {
  2. String fileName = "c:\\test.txt";
  3.  
  4. try {
  5. BufferedWriter writer = new BufferedWriter(new FileWriter(fileName,true));
  6. writer.write("Test text.");
  7. writer.newLine();
  8. writer.write("Line2");
  9. writer.close();
  10. } catch (IOException e) {
  11. e.printStackTrace();
  12. }
  13. }




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



F1 Site Family
AJAX F1
CSS F1
Database F1
Flash F1
HTML F1
Java F1
JavaScript F1
PhotoShop F1
PHP F1
Scripts F1
Tutorial F1
Windows F1

Total time: 0.2111