tags:

views:

195

answers:

2

I am trying to create a back up file for an html file on a web server.

I want the backup to be in the same location as the existing file (it's a quick fix). I want to create the file using File file = new File(PathName);

public void backUpOldPage(String oldContent) throws IOException{
            // this.uri is a class variable with the path of the file to be backed up
 String fileName = new File(this.uri).getName();
 String pathName = new File(this.uri).getPath();
    System.out.println(pathName);
    String bckPath = pathName+"\\"+bckName;

 FileOutputStream fout;  

 try
 {
     // Open an output stream
     fout = new FileOutputStream (bckFile);
     fout.close();  
 }
 // Catches any error conditions
 catch (IOException e)
 {
  System.err.println ("Unable to write to file");
  System.exit(-1);
 }
}

But if instead I was to set bckPath like this, it will work.

String bckPath = "C://dev/server/tomcat6/webapps/sample-site/index_sdjf---sd.html";

I am working on Windows, not sure if that makes a difference.

The result of String bckPath = pathName+"\"+bckName; is bckPath = C:\dev\server\tomcat6\webapps\sample-site\filename.html - this doesn't result in a new file.

+1  A: 

Use File.pathSeparator, that way you dont need to worry what OS you are using.

Visage
A: 

Try to use File.getCanonicalPath() instead of plain getPath(). This helps if the orginal path is not fully specified.

Regarding slashes, / or \ or File.pathSeparator is not causing the problem, because they are all the same on Windows and Java. (And you do not define bckFile in your code, only bckPath. Also use getCanonicalPath() on the new created bckPath.)

Peter Kofler