Java 复制/移动文件程序-方法是扫描目标目录而不是源目录

Java 复制/移动文件程序-方法是扫描目标目录而不是源目录,java,netbeans,Java,Netbeans,正如标题所说。该程序从源目录复制并将其移动到目标目录,但是我在isDirFile方法中得到一个空异常错误,我假设这意味着包含所有数据的数组返回为空,这使我相信目标目录正在被扫描,而不是源目录,到目前为止,我我不知道为什么会这样 编辑:输出 输入要复制的目录。C:/Users/jim/Desktop/pff.txt 输入目标目录将被移动到。C:/Users/jim/Desktop/bob 线程“main”java.lang.NullPointerException中出现异常 位于javaappli

正如标题所说。该程序从源目录复制并将其移动到目标目录,但是我在isDirFile方法中得到一个空异常错误,我假设这意味着包含所有数据的数组返回为空,这使我相信目标目录正在被扫描,而不是源目录,到目前为止,我我不知道为什么会这样

编辑:输出

输入要复制的目录。C:/Users/jim/Desktop/pff.txt
输入目标目录将被移动到。C:/Users/jim/Desktop/bob
线程“main”java.lang.NullPointerException中出现异常
位于javaapplication59.javaapplication59.main(javaapplication59.java:31)
位于javaapplication59.javaapplication59.main(javaapplication59.java54)
Java结果:1
生成成功(总时间:13秒)


它们是
isDirFile(dirName,destName)
for(File entry:entries){

显示NPE发生位置的堆栈跟踪在哪里?通常,如果抽象路径不存在、不是目录或Java无法读取(如Windows上的
My Music
目录),您将从
listFiles
获得
空值因此,在我的系统上运行您的代码似乎工作正常(我列出了目录内容,而不是复制它们)。我将验证
源代码,确保它是一个目录而不是“特殊链接”,而java.io.File
无法process@MadProgrammer我把我的输出错误编辑成原来的问题。我只是用一个简单的文本文件来测试它,输出代码语句出现在代码片段中。考虑提供一个演示你的问题的代码。这不是一个代码转储,而是一个你正在做什么的例子。这是你遇到的问题。这将减少混乱,并获得更好的响应
import java.io.*;
import java.util.Scanner;


public class Javaapplication59 {

public static void main(String[] args) throws Exception {
//Create a new instance of scanner to get user input
Scanner scanner = new Scanner (System.in);

//Ask user to input the directory to be copied
System.out.print("Input directory to be copied.");

//Save input as String
String dirName = scanner.nextLine();

//Ask user to input destination where direction will be copied
System.out.print("Input destination directory will be moved to.");

//Save input as String
String destName = scanner.nextLine();

//Run method to determine if it is a directory or file
isDirFile(dirName, destName);


}//end main

public static void isDirFile (String source, String dest) throws Exception{
//Create a File object for new directory in new location with same name
//as source directory
File dirFile = new File (dest, new File(source).getName());

//Make the new directory
dirFile.mkdirs();

//Create an array of File class objects for each item in the source
//directory
File[] entries; 

//If source directory exists
if (dirFile.exists()){
    //If the source directory is a directory
    if (dirFile.isDirectory()){

        //Get the data and load the array
        entries = new File(source).listFiles();

        //Iterate the array using alternate for statement
        for (File entry : entries){
            if (entry.isFile()){
                copyFile (entry.getAbsolutePath(), dest);
            } //end if
            else {
                isDirFile (entry.getAbsolutePath(), dest);
            }  //end else if
        }//end for
    }//end if
}//end if
else {
    System.out.println("File does not exist.");
} //end else
}

public static void copyFile (String source, String dest) throws Exception {

//declare Files
File sourceFile = null;
File destFile = null;

//declare stream variables
FileInputStream sourceStream = null;
FileOutputStream destStream = null;

//declare buffering variables
BufferedInputStream bufferedSource = null;
BufferedOutputStream bufferedDest = null;

try {
    //Create File objects for source and destination files
    sourceFile = new File (source);
    destFile = new File (dest);

    //Create file streams for the source and destination
    sourceStream = new FileInputStream(sourceFile);
    destStream = new FileOutputStream(destFile);

    //Buffer the file streams with a buffer size of 8k
    bufferedSource = new BufferedInputStream(sourceStream,8182);
    bufferedDest = new BufferedOutputStream(destStream,8182);

    //Use an integer to transfer data between files
    int transfer;

    //Alert user as to what is happening
    System.out.println("Beginning file copy:");
    System.out.println("\tCopying " + source);
    System.out.println("\tTo      " + dest);

    //Read a byte while checking for End of File (EOF)
    while ((transfer = bufferedSource.read()) !=-1){

    //Write a byte
    bufferedDest.write(transfer);
}//end while

}//end try

catch (IOException e){
    e.printStackTrace();
    System.out.println("An unexpected I/O error occurred.");
}//end catch

finally {
    //close file streams
    if (bufferedSource !=null)
        bufferedSource.close();

    if (bufferedDest !=null)
        bufferedDest.close();

    System.out.println("Your files have been copied correctly and "
            + "closed.");
}//end finally
}//end copyDir

}//end class