我正在尝试为 Web 服务器上的 html 文件创建备份文件。
我希望备份与现有文件位于同一位置(这是一个快速修复)。我想使用 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);
}
}
但是如果我像这样设置 bckPath,它就会起作用。
String bckPath = "C://dev/server/tomcat6/webapps/sample-site/index_sdjf---sd.html";
我正在 Windows 上工作,不确定这是否有什么不同。
String bckPath = pathName+"\"+bckName 的结果; 是 bckPath = C:\dev\server\tomcat6\webapps\sample-site\filename.html - 这不会生成新文件。
最佳答案
使用File.pathSeparator,这样你就不需要担心你正在使用什么操作系统。
关于java - 创建文件导致问题,File.getPath() 似乎不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1011463/