ive been working on this code, and i am stumped again... I have to make it so if a file is empty the progam will not work. The program still works even if the file is empty... here is my code.. any help would be great!!! thank you!!
Tina
import java.io.*;
public class program {
//usage
public static void usage()
{
System.err.println("ERROR: This progam requires exactly 2 arguments which are filenames:\n");
System.err.println("ERROR: first file contents are prepended to the second filename\n\n");
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
if (args.length !=2)
{
usage();
return;
//terminate program,
}
File FirstFile = new File(args[0]);
//checking for first file
if (!FirstFile.exists())
{
System.err.println("ERROR: The first file does not exist!\n\n");
return;
//terminate program
//if file is empty program will fail
}
if (FirstFile.length() == -1){
System.err.println("ERROR: First Document is Empty");
return;
}
File SecondFile = new File(args[1]);
//check the second file
if (!SecondFile.exists())
{
System.err.println("ERROR: The Second file does not exist!\n\n");
return;
//terminate program
}
//if file is empty program will fail
if (SecondFile.length() == -1){
System.err.println("ERROR: Second Document is Empty");
return;
}
//Taking 2 files and creating a new file to combine the files as one
try {
//the file that will show up first in the document
File inputFile = new File("c:\\file1.txt");
File inputFile1 = new File("c:\\file2.txt");
File outputFile = new File("c:\\file3.txt");
FileInputStream fis1 = new FileInputStream(SecondFile);
FileInputStream fis2 = new FileInputStream(FirstFile);
FileOutputStream fos = new FileOutputStream(outputFile);
//writes second file to documents
int c = 0;
while ((c = fis1.read()) != -1){
fos.write(c);
}
fis1.close();
//writes first file under second file
while ((c = fis2.read()) != -1){
fos.write(c);
}
fis2.close();
fos.close();
//finds any error
} catch (FileNotFoundException e) {
System.err.println("FileStream: " + e);
} catch (IOException e) {
System.err.println("FileStreams: " + e);
}
}
}