AleZZZZZ
AleZZZZZ

Reputation: 3

My code fails at a certain point. Java File IO

I am new to Java. I programmed in C++. I am trying to work with files but my code fails when I try create a file, that is, when the program tests if the file exists, it fails, but I have already created the file.

public Schedule(String name, String event)
{
   String filename= name+event+".txt";
   File TimeTable=new File(filename);
   if (TimeTable.exists()&&TimeTable.isFile()){ 
      writeToFile(TimeTable,name,event,filename);
      System.out.println("In constructor");
   }//fails here

}

Upvotes: 0

Views: 48

Answers (1)

KeV
KeV

Reputation: 2891

I have already created the file

If the following line is the reason you are saying this, then you're wrong.

File TimeTable=new File(filename);

This is making an abstract representation of file/directory pathnames but will not make the file if it doesn't exist.

File TimeTable=new File(filename); TimeTable.createNewFile();

The createNewFile method will make the physical file if it doesn't already exist.

Upvotes: 1

Related Questions