Often, though, you’ll prefer sequential access to a file and should use one of the:
The class java.io.File can represent either a file or a directory. [JDK 1.7 introduces a more versatile java.nio.file.Path, which overcomes many limitations of java.io.File.]
A path string is used to locate a file or a directory. Unfortunately, path strings are system dependent, e.g., “c:\myproject\java\Hello.java” in Windows or “/myproject/java/Hello.java” in Unix/Mac.
A path could be absolute (beginning from the root) or relative (which is relative to a reference directory). Special notations “.” and “..” denote the current directory and the parent directory, respectively.
import java.io.File;
public class Maker {
public static void main(String[] args){
try{
File dir = new File("dir3");
dir.mkdir();
File file = new File(dir,"file3");
file.createNewFile();
}catch(Exception x){
}
}
}
Following example creates “/tmp/user/java/bin” directory:
import java.io.File;
public class CreateDir {
public static void main(String args[]) {
String dirname = "/tmp/user/java/bin";
File d = new File(dirname);
// Create directory now.
d.mkdirs();
}
}
Note: Java automatically takes care of path separators on UNIX and Windows as per conventions. If you use a forward slash (/) on a Windows version of Java, the path will still resolve correctly.
You can use list() method provided by File object to list down all the files and directories available in a directory as follows:
import java.io.File;
public class ReadDir {
public static void main(String[] args) {
File file = null;
String[] paths;
try{
// create new file object
file = new File("/tmp");
// array of files and directory
paths = file.list();
// for each name in the path array
for(String path:paths)
{
// prints filename and directory name
System.out.println(path);
}
}catch(Exception e){
// if any error occurs
e.printStackTrace();
}
}
}
This would produce following result based on the directories and files available in your /tmp directory:
test1.txt
test2.txt
ReadDir.java
ReadDir.class
public boolean exists() // Tests if this file/directory exists.
public long length() // Returns the length of this file.
public boolean isDirectory() // Tests if this instance is a directory.
public boolean isFile() // Tests if this instance is a file.
public boolean canRead() // Tests if this file is readable.
public boolean canWrite() // Tests if this file is writable.
public boolean delete() // Deletes this file/directory.
public void deleteOnExit() // Deletes this file/directory when the program terminates.
public boolean renameTo(File dest) // Renames this file.
public boolean mkdir() // Makes (Creates) this directory.
The I/O streams are often layered or chained with other I/O streams, for purposes such as buffering, filtering, or data-format conversion (between raw bytes and primitive types). For example, we can layer a BufferedInputStream to a FileInputStream for buffered input, and stack a DataInputStream in front for formatted data input (using primitives such as int, double), as illustrated in the following diagrams.
The number of total classes of Java I/O is large, and it is easy to get confused when to use which. The following are two methods for reading a file line by line.
Method 1:
private static void readFile1(File fin) throws IOException {
FileInputStream fis = new FileInputStream(fin);
//Construct BufferedReader from InputStreamReader
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
}
Method 2:
private static void readFile2(File fin) throws IOException {
// Construct BufferedReader from FileReader
BufferedReader br = new BufferedReader(new FileReader(fin));
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
}
Use the following code:
//use . to get current directory
File dir = new File(".");
File fin = new File(dir.getCanonicalPath() + File.separator + "in.txt");
readFile1(fin);
readFile2(fin);
Both works for reading a text file line by line.
The difference between the two methods is what to use to construct a BufferedReader. Method 1 uses InputStreamReader and Method 2 uses FileReader. What’s the difference between the two classes?
In summary, InputStreamReader is always a safer choice than FileReader.
It is worth to mention here that instead of using a concrete / or \ for a path, you should always use File.separator which can ensure that the separator is always correct for different operating systems. Also the path used should be relative, and that ensures the path is always correct.
public static void writeFile1() throws IOException {
File fout = new File("out.txt");
FileOutputStream fos = new FileOutputStream(fout);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
for (int i = 0; i < 10; i++) {
bw.write("something");
bw.newLine();
}
bw.close();
}
This example use FileOutputStream, instead you can use FileWriter or PrintWriter which is normally good enough for a text file operations.
public static void writeFile2() throws IOException {
FileWriter fw = new FileWriter("out.txt");
for (int i = 0; i < 10; i++) {
fw.write("something");
}
fw.close();
}
public static void writeFile3() throws IOException {
PrintWriter pw = new PrintWriter(new FileWriter("out.txt"));
for (int i = 0; i < 10; i++) {
pw.write("something");
}
pw.close();
}
public static void writeFile4() throws IOException {
File fout = new File("out.txt");
FileOutputStream fos = new FileOutputStream(fout);
OutputStreamWriter osw = new OutputStreamWriter(fos);
for (int i = 0; i < 10; i++) {
osw.write("something");
}
osw.close();
}
The main difference is that PrintWriter offers some additional methods for formatting such as println and printf. In addition, FileWriter throws IOException in case of any I/O failure. PrintWriter methods do not throws IOException, instead they set a boolean flag which can be obtained using checkError(). PrintWriter automatically invokes flush after every byte of data is written. In case of FileWriter, caller has to take care of invoking flush.
If you want your code to create a new file and erase previous existing file, FileWriter can simply take it place. To replace all content in an existing file, use this:
FileWriter fstream = new FileWriter(loc);
The code above will delete the existing file if it’s name is use in new file being writting.
To append/add something to an existing file, simply specify the second parameter to be true as following:
FileWriter fstream = new FileWriter(loc, true);
This will keep adding content to the existing file instead of creating a new version.
Here is a complete code example to do this. It is nothing particularly important but a quick code reference.
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
File dir = new File(".");
String loc = dir.getCanonicalPath() + File.separator + "Code.txt";
FileWriter fstream = new FileWriter(loc, true);
BufferedWriter out = new BufferedWriter(fstream);
out.write("something");
out.newLine();
//close buffer writer
out.close();
}
}
All the programming languages provide support for standard I/O where user’s program can take input from a keyboard and then produce output on the computer screen. If you are aware if C or C++ programming languages, then you must be aware of three standard devices STDIN, STDOUT and STDERR. Similar way Java provides following three standard streams
Following is a simple program which creates InputStreamReader to read standard input stream until the user types a “q”:
import java.io.*;
public class ReadConsole {
public static void main(String args[]) throws IOException
{
InputStreamReader cin = null;
try {
cin = new InputStreamReader(System.in);
System.out.println("Enter characters, 'q' to quit.");
char c;
do {
c = (char) cin.read();
System.out.print(c);
} while(c != 'q');
}finally {
if (cin != null) {
cin.close();
}
}
}
}
$javac ReadConsole.java
$java ReadConsole
Enter characters, 'q' to quit.
1
1
e
e
q
q
Leave a Comment