java copy file函数仅在调试模式下有效

时间:2014-02-24 11:12:02

标签: 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)

但是当我在调试模式下运行程序时,它可以工作.. !!

有人可以帮我吗???

功能copyFile:

    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());
            }
        }
    }

0 个答案:

没有答案