Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

file is empty, will fail

 
Tina Guilder
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
}
}
}
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15494
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by an "empty" file - do you mean that the file does not exist or that the length of the file is 0?

Read the API documentation of the method length() in class java.io.File. It says that the method returns 0 when the file does not exist. It also returns 0 when the length of the file is 0.

Why are you testing if length() returns -1? The documentation doesn't say that that method ever returns -1.
 
Tina Guilder
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the file is created..but nothing in it. I changed the -1 to zero and now it works.. thank you!

Tina
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic