File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes file is empty, will fail Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "file is empty, will fail" Watch "file is empty, will fail" New topic
Author

file is empty, will fail

Tina Guilder
Greenhorn

Joined: Aug 23, 2006
Posts: 9
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

Joined: Aug 16, 2005
Posts: 14150
    
  18

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Tina Guilder
Greenhorn

Joined: Aug 23, 2006
Posts: 9
the file is created..but nothing in it. I changed the -1 to zero and now it works.. thank you!

Tina
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: file is empty, will fail