Java Program II assignment 2

profilewilden
java-db2.docx

Discussion Board 2

William Denison

27 NOV 2014

A Java class that allows writing to files is Class FileWriter. This class is a part of java.io package and extends (inherits from) OutputStreamWriter class.

This class allows us to write character files conveniently. The reason I chose this class is because of the functionality provided by FileWriter class. It allows us to write to a new file, as well as append content to an existing file. Given a filename as a string parameter, it creates the file if it does not already exist. So, the user is not required to explicitly create a new file before writing to it using FileWriter class.

FileWriter class provides several constructors, which are as follows:

1) FileWriter(File file) - takes File object as a parameter, which corresponds to the file, the data is to be written to.

2) FileWriter(File file, boolean append) - takes two parameters: one is the File object to which data is to be written to, and second is a boolean parameter which indicates whether data is to be appended to the file

3) FileWriter(String filename) - takes a string parameter for the filename

4) FileWriter(String filename, Boolean append) - takes two parameters, one for the filename (string) and a Boolean value indicating whether data should be appended to the file or not

Pseudo code:

1. Create a string array and initialize elements of the array with given data

String[] dataArray = new String[4];

dataArray[0] = "1001 Jennifer Ward SUPPLIES 2140.20 BOOKS 5200.10 PAPER 455.23 NORTH Phone";

dataArray[1] = "1003 Athena Andrews SUPPLIES 5155.55 BOOKS 6300.50 PAPER 223.25 SOUTH Email";

dataArray[2] = "1005 Andy Smith SUPPLIES 6155.55 BOOKS 300.50 PAPER 55.55 SOUTH Email";

dataArray[3] = "1006 Robert Stearns SUPPLIES 7255.55 BOOKS 8300.50 PAPER 77.77 SOUTH Phone";

2. Create a File object and pass the filename

File file = new File(“data.txt”);

3. Check if file exits by that name, if it does not create a new file as follows:

file.createNewFile();

4. Create FileWriter object by passing the File object created above

5. In a for loop write each element of the array to the file:

for(int i=0; i<dataArray.length; i++){

writer.write(dataArray[i]); //write the line to file

writer.write("\n"); //write a newline to the file

}

6. Close the writer.

7. Catch any exceptions if thrown

RealCode:

import java.io.*;

class FileWritingExample

{

public static void main(String args[]){

String[] dataArray = new String[4];

dataArray[0] = "1001 Jennifer Ward SUPPLIES 2140.20 BOOKS 5200.10 PAPER 455.23 NORTH Phone";

dataArray[1] = "1003 Athena Andrews SUPPLIES 5155.55 BOOKS 6300.50 PAPER 223.25 SOUTH Email";

dataArray[2] = "1005 Andy Smith SUPPLIES 6155.55 BOOKS 300.50 PAPER 55.55 SOUTH Email";

dataArray[3] = "1006 Robert Stearns SUPPLIES 7255.55 BOOKS 8300.50 PAPER 77.77 SOUTH Phone";

try{

File file = new File("data.txt");

//check if file already exists

if(!file.exists()){

file.createNewFile(); //create a new file if it does not exist

}

//create FileWriter object using File object created above

FileWriter writer = new FileWriter(file);

//write content to file

for(int i=0; i<dataArray.length; i++){

writer.write(dataArray[i]); //write the line to file

writer.write("\n"); //write a newline to the file

}

writer.flush();

writer.close(); //close the writer stream

}catch(IOException ioe){

//display the exception stacktrace

System.out.println("Error while writing to the file");

ioe.printStackTrace();

}

}

}