主题:为什么renameTo(File)操作失败
public void saveAsFile()
{
fc.addChoosableFileFilter(filter);
fc.setDialogTitle("另存为");
int len=fc.showSaveDialog(NotePad.this);
if (len == JFileChooser.APPROVE_OPTION)
{
file = fc.getSelectedFile();
String fileName=file.getAbsolutePath();
int extensionIndex=fileName.indexOf(".txt");
if (extensionIndex<0)
{
String str=fileName+".txt";
System.out.println(file.renameTo(new File(str)));
System.out.print(str);
}
........
try
{
BufferedWriter output=new BufferedWriter(new FileWriter(file));
String out=textArea.getText();
out=out.replaceAll("\n", "\r\n");
output.write(out);
output.close();
} catch (FileNotFoundException e1)
{
e1.printStackTrace();
}
catch(IOException ex)
{
}
}
一个输出为
false
C:\Documents and Settings\Owner\桌面\w.txt
如renameTo(File)操作成功返回true
为什么renameTo(File)操作失败
{
fc.addChoosableFileFilter(filter);
fc.setDialogTitle("另存为");
int len=fc.showSaveDialog(NotePad.this);
if (len == JFileChooser.APPROVE_OPTION)
{
file = fc.getSelectedFile();
String fileName=file.getAbsolutePath();
int extensionIndex=fileName.indexOf(".txt");
if (extensionIndex<0)
{
String str=fileName+".txt";
System.out.println(file.renameTo(new File(str)));
System.out.print(str);
}
........
try
{
BufferedWriter output=new BufferedWriter(new FileWriter(file));
String out=textArea.getText();
out=out.replaceAll("\n", "\r\n");
output.write(out);
output.close();
} catch (FileNotFoundException e1)
{
e1.printStackTrace();
}
catch(IOException ex)
{
}
}
一个输出为
false
C:\Documents and Settings\Owner\桌面\w.txt
如renameTo(File)操作成功返回true
为什么renameTo(File)操作失败