Showing posts with label File handling. Show all posts
Showing posts with label File handling. Show all posts

Friday, 24 June 2011

Remember to properly close the File!

Today few comments on how to deal with Files, Streams, Connections and anything else that is ‘closeable’ in Java. To simplify we will focus on a FileWriter class and a simple code snippet with a bug that uses it:

public void writeToFile(String fileName, String content) {
try {
FileWriter file = new FileWriter(fileName);
file.write(content);
} catch (IOException e) {
// Log the exception
}
}

So what’s wrong? Well, plenty of things: we do not check the input values (maybe they are null or rubbish?), we do not care whether the file existed or is a directory, we do not care about specifying the encoding for the file. On the other hand you could argue that all of this are not problems but ‘features’, that the contract of this method says: just write using a FileWriter. That may be, but still there is one thing wrong with this code – we did not close the file.

Is that important? Well, yeah! Not closing the file may result in corrupted output of your write and makes the system still occupy the resources that could be freed. Always remember to close your Files, Streams, Connections! Let’s make a quick fix to the code and remove the bug:

public void writeToFile(String fileName, String content) {
try {
FileWriter file = new FileWriter(fileName);
file.write(content);
file.close();
} catch (IOException e) {
// Log the exception
}
}

Fixed? I guess some of you think it is fixed, but in fact it is not – what if there is an exception thrown during the write? The method will exit and the file will not be closed! Okay, there is a fix for that – use try/finally clause. This will guarantee that no matter what will happen in write the file will be closed.

public void writeToFile(String fileName, String content) {
FileWriter file = null;
try {
file = new FileWriter(fileName);
file.write(content);
} catch (IOException e) {
// Log the exception
} finally {
file.close();
}
}

Fixed? Well… no. The exception can be also thrown in the FileWriter constructor, so in finally block the file will be null. And this will result in NullPointerException. Let’s deal with that:

public void writeToFile(String fileName, String content) {
FileWriter file = null;
try {
file = new FileWriter(fileName);
file.write(content);
} catch (IOException e) {
// Log the exception
} finally {
if (file != null) {
file.close();
}
}
}

Done? Not yet… file.close() also can throw an exception! Since the method writeToFile does not allow any checked exception to be thrown, we have to deal with that also:

public void writeToFile(String fileName, String content) {
try {
FileWriter file = new FileWriter(fileName);
try {
file.write(content);
} finally {
file.close();
}
} catch (IOException e) {
// Log the exception
}
}

Finally (pun intended) we are done and the bug is fixed! Notice that in this code we did not had to check whether file is null before closing it – this is because the only way file is null is if an exception was thrown in the constructor in line 3 and this would lead to skipping most of the code and going to line 10. As you can see closing a closeable object is not a trivial task, so remember to do it properly!

There is another issue appearing in this problem – readability. If this the code above seems readable to you, then try to imagine dealing with more than one closeable object – it can get really messy! Therefore whenever you work with closeable objects it is a good idea to split the code into 2 methods: one that deals with exceptions and closing and the other which just does the main code. To see how this would work with the code above take a look at the following:

// Here we handle opening the file and manage exceptions.
public void writeToFile(String fileName, String content) {
try {
FileWriter file = new FileWriter(fileName);
try {
performWriteToFile(file, content);
} finally {
file.close();
}
} catch (IOException e) {
// Log the exception
}
}

// Here we perform all the operations on the file.
// This method usually is much longer.
private void performWriteToFile(FileWriter file,
String content) throws IOException {
file.write(content);
}


Wednesday, 11 May 2011

Write byte array to file using BufferedOutputStream

Writing the function:

public static void writeByteArrayToFile(String fileName, byte[] byteArr)
{
BufferedOutputStream bos = null;
try
{
//create an object of FileOutputStream
FileOutputStream fos = new FileOutputStream(new File(strFileName));
//create an object of BufferedOutputStream
bos = new BufferedOutputStream(fos);


/*
* To write byte array to file use,
* public void write(byte[] b) method of BufferedOutputStream
* class.
*/
System.out.println("Writing byte array to file");
bos.write(byteArr);
System.out.println("File written");
}
catch(FileNotFoundException fnfe)
{
System.out.println("Specified file not found" + fnfe);
}
catch(IOException ioe)
{
System.out.println("Error while writing file" + ioe);
}
finally
{
if(bos != null){
try
{
//flush the BufferedOutputStream
bos.flush();
//close the BufferedOutputStream
bos.close();
}
catch(Exception e){//don't swallow
//exceptions..add your code accordingly
}
}
}
}


Using the above function:


String fileName = "C:\demo.txt";
String str = "BufferedOutputStream Example to write byte array";
writeByteArrayToFile(fileName,str.getBytes());

Write byte to file using BufferedOutputStream

 

public static void writeByte2File(byte b, String fileName)
{
BufferedOutputStream bos = null;

try
{
//Create FileOutputStream for the file
FileOutputStream fos = new FileOutputStream(fileName);
//Create BufferedOutputStream object
//for FileOutputStream

bos = new BufferedOutputStream(fos);

/*
* To write a byte to BufferedOutputStream use,
* public void write(int b) method of BufferedOutputStream
* class.
*
* This method may throw IOException if something goes
* wrong.
*/

System.out.println("BufferedOutputStream: writing file");

bos.write(b);

System.out.println("BufferedOutputStream : written file");
}
catch(FileNotFoundException fnfe)
{
System.out.println("Specified file not found" + fnfe);
}
catch(IOException ioe)
{
System.out.println("Error while writing to file" + ioe);
}
finally
{
try
{
if(bos != null)
{
bos.flush();
bos.close();
}

}
catch(Exception e){}

}
}



Eg. of calling function:


String fileName="C:\demo.txt";
byte b = 65;
//this will write a character represented by ascii 65
//i.e. 'A'
writeByte2File(b,fileName);

Tuesday, 10 May 2011

Read File in String Using Java BufferedInputStream Example

public static String readFile(String fileName){
File file = new File(fileName);
BufferedInputStream bin = null;

try
{
//create FileInputStream object
FileInputStream fin = new FileInputStream(file);

//create object of BufferedInputStream
bin = new BufferedInputStream(fin);

//create a byte array
byte[] contents = new byte[1024];

int bytesRead=0;
String strFileContents;
StringBuffer buf= new StringBuffer();

while( (bytesRead = bin.read(contents)) != -1){

strFileContents = new String(contents, 0, bytesRead);
buf.append(strFileContents)
System.out.print(strFileContents);
}
return buf.toString();
}
catch(FileNotFoundException e)
{
System.out.println("File not found" + e);
}
catch(IOException ioe)
{
System.out.println("Exception while reading the file "
+ ioe);
}
finally
{
//close the BufferedInputStream using close method
try{
if(bin != null)
bin.close();
return String.Empty();
}catch(IOException ioe)
{
System.out.println("Error while closing the stream :"
+ ioe);
return String.Empty();
}

}
}


File name can be "C://FileIO//ReadFile.txt".

Moving file or directory from one directory to another

Here we write function to copy file or directory from one place to other.

public static boolean moveFileOrDirectory(String src, String dst){
File source = new File(src);
if(!source.exists())
return false;


File destination = new File(dest);
if(!destination.exists()){
System.out.print("Mentioned directory does not exist.
\nDo you want to create a new directory(Y/N)? "
);

String chk = readString();
if(chk.equals("Y") || chk.equals("y")){
destination.mkdir();
return copyDirectory(source, destination);
}else{
return false;
}
}else //if file or directory already exists, we have to copy and
//overwrite the already existing one
{
System.out.println("Given file or folder name already exists.
\nDo you want to replace now?"
);
String chk = readString();
if("Y".equals(chk) )
return copyDirectory(source, destination);
else return false;
}
}



 


Now this function makes use of copyDirectory function which can be written like this:



public static boolean copyDirectory(File sourceDir, File destDir) throws
IOException{

if(!destDir.exists()){
destDir.mkdir();
}

File[] children = sourceDir.listFiles();
for(File sourceChild : children){

String name = sourceChild.getName();

File destChild = new File(destDir, name);

if(sourceChild.isDirectory()){

return copyDirectory(sourceChild, destChild);

}

else{

return copyFile(sourceChild, destChild);

}

}

}



Now this in turn makes use of copyFile function.



public static void copyFile(File source, File dest) throws IOException{

if(!dest.exists()){

dest.createNewFile();

}

InputStream in = null;

OutputStream out = null;

try{

in = new FileInputStream(source);

out = new FileOutputStream(dest);

byte[] buf = new byte[1024];

int len;

while((len = in.read(buf)) > 0){

out.write(buf, 0, len);

}

}

finally{

in.close();

out.close();

}
return true;

}

Copy multiple files (java)

We have already seen the function to copy 1 file to another destination. Here we have to write function to copy multiple file to 1 location.

Refer this article for copyFile() function.

public static boolean copyMultipleFiles(String dst, String ... src){
for(String oneSrc : src)
{
boolean isCopied = copyFile(oneSrc,dst);
if(!isCopied)
return isCopied;
//Note that i have not yet figured out the case of rollback in case of
//unsuccessful copy of 1 file. Because this depends on your application.
//So if you want to rule out any copy if even 1 copy file fails,
//just call the delete function and so on.
}
return true;
}



Also note that this method uses varargs function, but you can use array instead.

Java - Copying one file to another

Here is function called copyFile  which takes 2 strings – source and destination. It copies source to destination.

 

private static void copyFile(String srcFile, String dstFile){

try{

File f1 = new File(srcFile);

File f2 = new File(dstFile);

InputStream in = new FileInputStream(f1);

//Append the file
OutputStream out = new FileOutputStream(f2,true);
//Overwrite the file.
OutputStream out = new FileOutputStream(f2);


byte[] buf = new byte[1024];

int len;

while ((len = in.read(buf)) > 0){

out.write(buf, 0, len);

}

in.close();

out.close();

System.out.println("File copied.");

}

catch(FileNotFoundException ex){

System.out.println(ex.getMessage() + " in
the specified directory."
);
return false;

}

catch(IOException e){

System.out.println(e.getMessage());
return false;

}
return true;
}

Rename the File or Directory in java

This example shows function, which renames the file or directory. The function takes old name and new name, and renames accordingly.

public boolean renameFile(String oldName, String newName)
{
File oldfile = new File(oldName);
if(!oldfile.exists())
{
System.out.println("File or directory does not exist.");
return false;
}
File newfile = new File(newName);
boolean reName = oldfile.renameTo(newfile);
if(!reName )
{
System.out.println("File or directory does not rename successfully.");
return reName ;
}else{
System.out.println("File or directory rename is successfully.");
return reName;
}
}

Append to file in java

See this article to see about FileWriter and BufferedWriter in java. We have already seen how to write to file in java. Appending is on the same lines, just adding true to FileWriter object.

FileWriter fstream = new FileWriter("out.txt",true);



Full example:


public static void appendToFile(String fileName)
{
try{
//Create file
FileWriter fstream = new FileWriter(fileName,true);
BufferedWriter out = new BufferedWriter(fstream);
out.write("Hello I just wanted to append");
//Close the output stream
out.close();
catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}

Java Write To File using BufferedWriter

For writing data to a file, the class FileWriter and BufferedWriter are used.

FileWriter :

FileWriter is a subclass of OutputStreamWriter class that is used to write text (as opposed to binary data) to a file. You create a FileWriter by specifying the file to be written to, or optionally, when the data should be appended to the end of an existing file instead of overwriting that file. The FileWriter class creates an internal FileOutputStream to write bytes to the specified file

BufferedWriter :

The BufferedWriter class is used to write text to a character-output stream, buffering characters so as to provide for the efficient writing of single characters, arrays and strings.
The constructor of the FileWriter class takes the file name which has to be buffered by the BufferedWriter stream. The write( ) method of BufferedWriter class is used to create the file into specified directory.

Here again decorator pattern is used, where FileWriter object is decorated to BufferedWriter to carry out write operation.

public static String readString()
{
BufferedReader in ;
String str = String.Empty();
try{
in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Please enter string to be input: ");
str = in.readLine();
}
return str;
}

public static boolean writeFile(String fileName)
{
boolean exist = file.createNewFile();
if (!exist)
{
System.out.println("File already exists.");
return exist;

}
else
{
FileWriter fstream = new FileWriter(file_name);
BufferedWriter out = new BufferedWriter(fstream);
out.write(readString());
out.close();
System.out.println("File created successfully.");
return exist;

}
}



See also how to append text file.

Java: read a text file line by line

This class reads a text file line by line and echos the contents to standard out.
Here we have used BufferedReader to do this.
import java.io.*;

public static String readFile(String fileName)
// nested initialization,
// or in design pattern jargon, decorate FileInputStream with
// BufferedInputStream.
BufferedReader in = new BufferedReader(new FileReader(fileName));
try {
// read file line by line
String line = in.readLine();
while (line != null) {
printLine(line);
line = in.readLine();
}
}
finally {
// always close input stream
in.close();
}
}
private static void printLine(String line) {
System.out.println(line);
}

Create a File

For this createNewFile() method is used. It returns true if file is created and false otherwise. So for eg. it returns false if file already exist, so it can't create the new file here.

import java.io.*;
//now wherever you want do this:
public static void main(String[] args){
File file = new File("C://demo.txt");
boolean isCreated;
try
{
isCreated= file.createNewFile();
}
catch(IOException ioe)
{
System.out.println("Error in creating a new empty file :" + ioe);
}
}

Monday, 9 May 2011

Working with Directory tutorial

Following topics can be covered in this tutorial

How to Concatenate Files using SequenceInputStream

The SequenceInputStream(in the API reference documentation) creates a single input stream from multiple input sources. This example program uses SequenceInputStream to implement a concatenation utility that sequentially concatenates files together in the order they are listed on the command line.

So lets first create the Utility class which perform this application, say we call it ListOfFiles which takes list of file names in its constructor, and then concatenates them by creating a new InputStream for every filename listed as argument.

Creating the Utility Class

import java.util.*;
import java.io.*;

public class ListOfFiles implements Enumeration {

private String[] listOfFiles;
private int current = 0;

public ListOfFiles(String[] listOfFiles) {
this.listOfFiles = listOfFiles;
}

public boolean hasMoreElements() {
if (current < listOfFiles.length)
return true;
else
return false;
}

public Object nextElement() {
InputStream in = null;

if (!hasMoreElements())
throw new NoSuchElementException("No more files.");
else {
String nextElement = listOfFiles[current];
current++;
try {
in = new FileInputStream(nextElement);
} catch (FileNotFoundException e) {
System.err.println("ListOfFiles: Can't open " + nextElement);
}
}
return in;
}
}


ListOfFiles implements the Enumeration(in the API reference documentation) interface. You'll see how this comes into play as we walk through the rest of the program.


Using this Utility class


import java.io.*;

public class Concatenate {
public static void main(String[] args) throws IOException {
ListOfFiles mylist = new ListOfFiles(args);

SequenceInputStream s = new SequenceInputStream(mylist);
int c;

while ((c = s.read()) != -1)
System.out.write(c);

s.close();
}
}


After the main method creates the SequenceInputStream, it reads from it one byte at a time. When the SequenceInputStream needs an InputStream from a new source (such as for the first byte read or when it runs off the end of the current input stream), it calls nextElement on the Enumeration object to get the next InputStream. ListOfFiles creates FileInputStream objects lazily, meaning that whenever SequenceInputStream calls nextElement, ListOfFiles opens a FileInputStream on the next filename in the list and returns the stream. When the ListOfFiles runs out of files to read (it has no more elements), nextElement returns null, and the call to SequenceInputStream's read method returns -1 to indicate the end of input.

Concatenate simply echos all the data read from the SequenceInputStream to the standard output.

How to use file stream?


Monday, 2 May 2011

How to write Java code to listen runtime changes in text file

I had a requirement to write a java code to listen continues changing text file.I have used following code to do that.

public void listenServer() throws IOException {
Reader fileReader = new FileReader("/home/vaani/server.log");
BufferedReader input = new BufferedReader(fileReader);
String line = null;
while (true) {
if ((line = input.readLine()) != null) {
System.out.println(line);
continue;
}
try {
Thread.sleep(1000L);
} catch (InterruptedException x) {
Thread.currentThread().interrupt();
break;
}
}
input.close();
return isException;
}

Monday, 25 April 2011

Java read file as utf8

Specify "UTF-8" as charsetName to the constructor of InputStreamReader. From JavaDoc:

An InputStreamReader is a bridge from byte streams to character streams: It reads bytes and decodes them into characters using a specified charset. The charset that it uses may be specified by name or may be given explicitly, or the platform's default charset may be accepted.
BufferedReader r = new BufferedReader(
new InputStreamReader(
new FileInputStream("com/demo/io/test.txt"),
"UTF-8"
)
);
String l = r.readLine();
while (l != null) {
System.out.println(l);
l = r.readLine();
}
r.close();

Java read file as byte array

import java.io.*;

public class ByteFileReader {
public static void main(String[] args) throws Exception {
String fileName = "com/demo/io/test.txt";
ByteFileReader r = new ByteFileReader();
byte[] bytes = r.getBytesFromFile(new File(fileName));
System.out.printf("Read %d bytes from file %s:\n", bytes.length, fileName);
System.out.write(bytes, 0, bytes.length);
}

public byte[] getBytesFromFile(File file) throws IOException {
if (! file.exists()) {
return null;
}

// get the size of the file
long length = file.length();
if (length > Integer.MAX_VALUE) {
throw new IOException(
String.format(
"The file %s is too large to be hold in a byte array",
file.getName()
)
);
}

int len = (int) length;
byte[] bytes = new byte[len];

InputStream in = new FileInputStream(file);

// read in the bytes
int offset = 0, n = 0;
while (offset < len && n >= 0) {
n = in.read(bytes, offset, len - offset);
offset += n;
}

if (offset < len) {
throw new IOException("Faile to read all contents of file: " + file.getName());
}

in.close();
return bytes;
}
}

Java file as url

 A file can be read as URL with the java.net package. You must provide absolute path after the URI scheme name file://.
package com.demo.io;

import java.io.*;
import java.net.*;

public class FileAsURL {
public static void main(String[] args) throws Exception {
URL url = new URL("file:///tmp/com/demo/io/test.txt");
URLConnection conn = url.openConnection();
BufferedReader r = new BufferedReader(
new InputStreamReader(
conn.getInputStream()
)
);
String l = r.readLine();
while (l != null) {
System.out.println(l);
l = r.readLine();
}
}
}