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: | 18222 |
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.
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:
- 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:
Code:
public void writeFile() { String fileName = "c:\\test.txt"; try { writer.write("Test text."); writer.close(); e.printStackTrace(); } }
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:
public void writeFile() { String fileName = "c:\\test.txt"; try { writer.write("Test text."); writer.newLine(); writer.write("Line2"); writer.close(); e.printStackTrace(); } }
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 |
