java复制文件函数仅在调试模式下工作
我有一段代码来复制一个特定的文件,我已经使用这个函数很多年了,它工作正常 问题是,现在我正在用java awt/swing编写一个程序,我的copyFile函数只在调试模式下工作……我不明白为什么 这是它抛出的错误:java复制文件函数仅在调试模式下工作,java,debugging,io,Java,Debugging,Io,我有一段代码来复制一个特定的文件,我已经使用这个函数很多年了,它工作正常 问题是,现在我正在用java awt/swing编写一个程序,我的copyFile函数只在调试模式下工作……我不明白为什么 这是它抛出的错误: can't copy directory: QQQ.wav source file is unreadable: QQQ.wav Error occoured QQQ.wav (The system cannot find the file specified) 但是,当我在调试
can't copy directory: QQQ.wav
source file is unreadable: QQQ.wav
Error occoured QQQ.wav (The system cannot find the file specified)
但是,当我在调试模式下运行程序时,它可以工作
谁能帮我一下吗
功能复制文件:
public static void copyFile(File varFromFile, File varToFile) throws IOException {
// First make sure the source file exists, is a file, and is readable.
if (!varFromFile.exists())
System.err.println("no such source file: " + varFromFile);
if (!varFromFile.isFile())
System.err.println("can't copy directory: " + varFromFile);
if (!varFromFile.canRead())
System.err.println("source file is unreadable: " + varFromFile);
// If the destination is a directory, use the source file name
// as the destination file name
if (varToFile.isDirectory())
varToFile = new File(varToFile, varFromFile.getName());
// If the destination exists, make sure it is a writable file
// and ask before overwriting it. If the destination doesn't
// exist, make sure the directory exists and is writable.
if (varToFile.exists()) {
if (!varToFile.canWrite())
System.out.println("destination file is unwriteable: "
+ varToFile);
} else {
// If file doesn't exist, check if directory exists and is
// writable. If getParent() returns null, then the directory is
// the current directory. so look up the user. Directory system
// property to
// find out what that is.
// The destination directory
String varParent = varToFile.getParent();
// If none, use the current directory
if (varParent == null)
varParent = System.getProperty("user.dir");
// Convert it to a file.
File vardir = new File(varParent);
if (!vardir.exists())
System.out.print("destination directory doesn't exist: "
+ varParent);
if (vardir.isFile())
System.out
.print("destination is not a directory: " + varParent);
if (!vardir.canWrite())
System.out.print("destination directory is unwriteable: "
+ varParent);
}
// If we've gotten this far, then everything is okay.
// So we copy the file, a buffer of bytes at a time.
// Stream to read from source
FileInputStream varFromSource = null;
// Stream to write to destination
FileOutputStream VarToDestination = null;
try {
// Create input stream
varFromSource = new FileInputStream(varFromFile);
// Create output stream
VarToDestination = new FileOutputStream(varToFile);
// To hold file contents
byte[] buffer = new byte[4096];
// How many bytes in buffer
int bytes_read;
// Read until EOF
while ((bytes_read = varFromSource.read(buffer)) != -1)
VarToDestination.write(buffer, 0, bytes_read);
//System.out.println("File copied !!!");
} catch (Exception e) {
System.err.println("Error occoured " + e.getMessage());
} finally {
if (varFromSource != null) {
try {
varFromSource.close();
} catch (IOException e) {
System.err.println("Error is " + e.getMessage());
}
}
if (VarToDestination != null) {
try {
VarToDestination.close();
} catch (IOException e) {
System.err.println("Error is " + e.getMessage());
}
}
}
公共静态void copyFile(File varFromFile,File varToFile)引发IOException{
//首先确保源文件存在、是一个文件且可读。
如果(!varFromFile.exists())
System.err.println(“没有这样的源文件:“+varFromFile”);
如果(!varFromFile.isFile())
System.err.println(“无法复制目录:+varFromFile”);
如果(!varFromFile.canRead())
System.err.println(“源文件不可读:“+varFromFile”);
//如果目标是目录,请使用源文件名
//作为目标文件名
if(varToFile.isDirectory())
varToFile=新文件(varToFile,varFromFile.getName());
//如果目标存在,请确保它是可写文件
//并在覆盖它之前询问。如果目标没有
//存在,请确保目录存在且可写。
if(varToFile.exists()){
如果(!varToFile.canWrite())
System.out.println(“目标文件不可写入:”
+瓦托菲尔);
}否则{
//如果文件不存在,请检查目录是否存在并且
//可写。如果getParent()返回null,则目录为空
//当前目录。因此,请查找user.directory系统
//财产
//找出那是什么。
//目标目录
字符串varParent=varToFile.getParent();
//如果没有,请使用当前目录
if(varParent==null)
varParent=System.getProperty(“user.dir”);
//将其转换为文件。
File vardir=新文件(varParent);
如果(!vardir.exists())
System.out.print(“目标目录不存在:”
+varParent);
if(vardir.isFile())
系统输出
.print(“目的地不是目录:“+varParent”);
如果(!vardir.canWrite())
System.out.print(“目标目录不可写入:”
+varParent);
}
//如果我们走到这一步,一切都会好起来的。
//所以我们复制文件,一次复制一个字节的缓冲区。
//要从源读取的流
FileInputStream varFromSource=null;
//要写入目标的流
FileOutputStream VarToDestination=null;
试一试{
//创建输入流
varFromSource=新文件输入流(varFromFile);
//创建输出流
VarToDestination=新文件输出流(varToFile);
//保存文件内容
字节[]缓冲区=新字节[4096];
//缓冲区中有多少字节
读取整数字节;
//读到EOF
而((bytes_read=varFromSource.read(buffer))!=-1)
写入(缓冲区,0,字节\读取);
//System.out.println(“文件复制!!!”;
}捕获(例外e){
System.err.println(“Error occoured”+e.getMessage());
}最后{
if(varFromSource!=null){
试一试{
varFromSource.close();
}捕获(IOE异常){
System.err.println(“错误为”+e.getMessage());
}
}
if(VarToDestination!=null){
试一试{
VarToDestination.close();
}捕获(IOE异常){
System.err.println(“错误为”+e.getMessage());
}
}
}
那么一些代码呢?您的程序是否有读写文件的权限?将catch块更改为
System.err.println(“Error occoured”+e);
,这样我们就可以看到实际的异常了。e.getMessage()它本身很少有用。请下载SysInternals Process Monitor,为Path contains QQQ.wav设置一个筛选器,并查看它试图从何处加载文件。您所说的“在调试模式下运行程序”到底是什么意思?