Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Fully qualified filename (including path)

807601Mar 13 2008 — edited Mar 13 2008
I have a sample program I'm playing with to learn a bit about JAVA. In the code below I have variables named 'file1' and 'file2' which are defined with a filenames 'file1.txt' and file2.txt respectively.

So when the program executes it looks for these files in the current directory. However, if I want to change these values to 'c:\file1.txt' and 'c:\ttmsapps\file2.txt' then I can't compile the program.

Would someone be able to show me in the code below how to specify a fully qualified filename? I'd certainly appreciate seeing the example.
import java.io.File;
import java.lang.Object;
import java.util.Date;
import java.text.Format;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

public class CompareFileDates {
  public static void main(String[] args) {

    String fileStatus = " ";
    String file1 = "file1.txt";
    String file2 = "file2.txt";
    long date1 = 0;
    long date2 = 0;
    String finalDate1 = null;
    String finalDate2 = null;
    boolean file1Exists = (new File(file1)).exists();
    boolean file2Exists = (new File(file2)).exists();


    if (file1Exists) {
        // Get the timestamp from file 1 and format it
        date1 = (new File(file1)).lastModified();
	Date newDate1 = new Date(date1); 
	DateFormat df = new SimpleDateFormat();
	finalDate1 = df.format(newDate1); 
    }
    else {
        // File or directory does not exist
        fileStatus = file1 + " does not exist.  ";
    }
...more
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 10 2008
Added on Mar 13 2008
12 comments
1,183 views