Tuesday, July 7, 2009

Source Code to write into a file-Java

In this section, I will give you the code how to write into the file using all possible classes of the java.io package.


The classes I am using here are:

BufferWriter, BufferedOutputStream, OutputStream, DataOutputStream, FileWriter, FileOutputStream,PrintWriter, PrintStream.


The below code tells you all possible way of writing into the file using the above classes.

import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.PrintStream;
import java.io.Writer;
import java.io.File;

public class FileWrite {

/** Creates a new instance of FileWrite */
public FileWrite() {
}
String stringTobeWritten = "This is line to be written";

private void writeBufferWriter(){
try{
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("ReadMeBufferedWriter.txt",true));
bufferedWriter.write(stringTobeWritten);
bufferedWriter.close();
}catch(FileNotFoundException fnfe){
fnfe.printStackTrace();
}catch(IOException ioe){
ioe.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
}


private void writeBufferedOutputStream(){
try{
BufferedOutputStream bufferedOutput = new BufferedOutputStream(new FileOutputStream("ReadMeBufferedOutputStream.txt"));
bufferedOutput.write(stringTobeWritten.getBytes());
bufferedOutput.write("\n".getBytes());
bufferedOutput.write(65); // the byte representation [Ascii code] of 'A'
bufferedOutput.close();
}catch(FileNotFoundException fnfe){
fnfe.printStackTrace();
}catch(IOException ioe){
ioe.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
}


private void writeOutputStream(){
try{
FileOutputStream fos = new FileOutputStream("ReadMeFileOutputStream.txt");
Writer out = new OutputStreamWriter(fos,"UTF8");
out.write(stringTobeWritten);
out.close();
}catch(FileNotFoundException fnfe){
fnfe.printStackTrace();
}catch(IOException ioe){
ioe.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
}



private void writeDataOutputStream(){
try{
FileOutputStream fos = new FileOutputStream("ReadMeDataOutputStream.txt");
DataOutputStream dos = new DataOutputStream(fos);
int ch = 65;
dos.writeChar(ch); // Writing Character ascii of A
dos.writeBytes(stringTobeWritten); // writing string
dos.close();
}catch(FileNotFoundException fnfe){
fnfe.printStackTrace();
}catch(IOException ioe){
ioe.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
}



private void writeFileWriter(){
try{
FileWriter fw = new FileWriter("ReadMeFileWriter.txt");
fw.write(stringTobeWritten+"\n");
fw.close();
}catch(FileNotFoundException fnfe){
fnfe.printStackTrace();
}catch(IOException ioe){
ioe.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
}


private void writeFileOutputStream(){
try{
FileOutputStream fos = new FileOutputStream("ReadMeFileOutputStream.txt");
fos.write(stringTobeWritten.getBytes());
fos.close();
}catch(FileNotFoundException fnfe){
fnfe.printStackTrace();
}catch(IOException ioe){
ioe.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
}

private void writePrintWriter(){
try{
PrintWriter printWriter = new PrintWriter("ReadMePrintWriter.txt");
printWriter.write(stringTobeWritten);
printWriter.write(stringTobeWritten.toCharArray()); // Character Array
printWriter.write("\n");
printWriter.write('C'); //Character
printWriter.write(65); // Ascii of 'A'
printWriter.close();
}catch(FileNotFoundException fnfe){
fnfe.printStackTrace();
}catch(IOException ioe){
ioe.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
}


private void writePrintStream(){
try{
File file = new File("ReadMePrintStream.txt");
PrintStream p = new PrintStream(new FileOutputStream(file, false));
p.println(stringTobeWritten);
p.close();
}catch(FileNotFoundException fnfe){
fnfe.printStackTrace();
}catch(IOException ioe){
ioe.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
}



public static void main(String argd[]) {
FileWrite fileWriter = new FileWrite();
fileWriter.writeBufferWriter();
fileWriter.writeBufferedOutputStream();
fileWriter.writeOutputStream();
fileWriter.writeDataOutputStream();
fileWriter.writeFileWriter();
fileWriter.writeFileOutputStream();
fileWriter.writePrintWriter();
fileWriter.writePrintStream();
}

}


Any suggestions/feedback appreciated.

No comments:

Total Pageviews