Archives for 

File Handling

Basic file operations

Reading and Writing with files in Java

The io package provides multiple ways to perform the file IO operations.

1)File Read/Write using BufferedStream

The code snippet is given below. Here first , we are writing a text to a file  sample.txt in /tmp folder. If you are using windows platform , then the file will be in the /tmp folder of the drive where your work space is located. For example if your workspace is in D drive then the file will be in /tmp of Drive.In the first part , we are writing the predefined string to the sample.txt and then closing the stream . Then reading the file.

import java.io.*;
/**
* User: Bijoy
* Date: 12/11/12
* Time: 7:40 AM
*/
public class BufferedSample {
public static void main(String[] args) {
try {
BufferedWriter buffWriter = new BufferedWriter(new FileWriter(new File("/tmp/sample.txt")));
String text = "This is a sample text message";
buffWriter.write(text);
buffWriter.flush();
buffWriter.close();
BufferedReader buff = new BufferedReader(new FileReader(new File("/tmp/sample.txt")));
System.out.println("------------------------------------");
String line = buff.readLine();
while (line != null) {
System.out.println(line);
line = buff.readLine();
}
System.out.println("-------------------------------------");
} catch (IOException e) {
e.printStackTrace();
}
}
}

The output of the program will be the same text message that we have written to the file

2)File Read/Write using ByteStream

In this case FileInputStream and FileOutputStream objects are creating. Then the sample string message is writing to the file using FileoutputStream object. The same file is reading by the FileInputStream object.

import java.io.*;
/**
* User: Bijoy
* Date: 12/11/12
* Time: 7:55 AM
*/
public class ByteStreamSample {

public static void main(String[] args) {
try {
int ch = 0;
FileOutputStream outputStream = new FileOutputStream(new File("/tmp/sample.txt"));
FileInputStream inputStream = new FileInputStream(new File("/tmp/sample.txt"));
String text = "This is a sample text message";
outputStream.write(text.getBytes());
outputStream.flush();
outputStream.close();
byte[] array = new byte[100];
System.out.println("---------------------------------");
ch = inputStream.read(array);
while (ch != -1) {
byte[] tmpArray = new byte[ch];
System.arraycopy(array, 0, tmpArray, 0, ch);
System.out.println(new String(tmpArray, "UTF-8"));
ch = inputStream.read(array);
}
System.out.println("---------------------------------");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException io) {
io.printStackTrace();
}
}
}

 

3)File Read/Write using Character Streams

In this case FileWriter and FileReader objects are creating . A raw string is writing to the file using FileWriter object. Then the same file is reading using FileReader object.

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/**
* User: Bijoy
* Date: 12/11/12
* Time: 8:23 AM
*/
public class CharacterStreamSample {
public static void main(String[] args) {
try {
FileWriter writer = new FileWriter(new File("/tmp/sample.txt"));
FileReader reader = new FileReader(new File("/tmp/sample.txt"));
String text = "This is a sample text";
writer.write(text.toCharArray());
writer.flush();
writer.close();
char[] array = new char[100];
int num = reader.read(array);
System.out.println("---------------------------------");
while (num != -1) {
System.out.println(new String(array));
num = reader.read(array);
}
System.out.println("---------------------------------");

} catch (IOException io) {
io.printStackTrace();
}

}
}