Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-搜索目录后自动重命名文件_Java - Fatal编程技术网

Java-搜索目录后自动重命名文件

Java-搜索目录后自动重命名文件,java,Java,当我运行这个程序时,它会找到以yyyy开头的文件并正确返回这些文件;但是,它跳过my.File.exists()并转到其他“文件名更改失败”。我已经看这个代码很长时间了。我犯了什么错误 //Searches the path for files String pathToScan =("C:\\Users\\desktop\\test\\"); String target_file ; File folderToScan = new File(pathToS

当我运行这个程序时,它会找到以yyyy开头的文件并正确返回这些文件;但是,它跳过my.File.exists()并转到其他“文件名更改失败”。我已经看这个代码很长时间了。我犯了什么错误

//Searches the path for files

 String pathToScan =("C:\\Users\\desktop\\test\\");
        String target_file ; 
        File folderToScan = new File(pathToScan); 

    File[] listOfFiles = folderToScan.listFiles();

     for (int i = 0; i < listOfFiles.length; i++) {
            if (listOfFiles[i].isFile()) {
                target_file =  listOfFiles[i].getName();
                if (target_file.startsWith("YYYY")){
                    System.out.println("-----------------------------------------------");
                    System.out.println("Match Found: "+ target_file); 
                //if the target file exists 
                if(myFile.exists()){

                    long lastmod = myFile.lastModified();
                    SimpleDateFormat format = new SimpleDateFormat("YYYY-MM-DD");
                    String lastmodi =  format.format(new Date(lastmod));

                    File newfile = new File("C:\\Users\\desktop\\test\\"+lastmodi+".csv");

                    //If rename successful, then print success with file location
                    if(myFile.renameTo(newfile)){
                        System.out.println("File Name Change Successful, New File Created:" + newfile.getPath());
                    }       
                    else{
                    System.out.println("File Name Change Failed");
                    }
                }
//搜索文件的路径
字符串pathToScan=(“C:\\Users\\desktop\\test\\”;
字符串目标文件;
File folderToScan=新文件(pathToScan);
文件[]listOfFiles=folderToScan.listFiles();
for(int i=0;i
我认为您在
myFile
中引用了一个错误的文件。以下代码在添加代码后对我有效。其他原因可能是您无权在C:\Users\desktop\test\目录中写入和/或该目录不存在

    String pathToScan = ("C:\\Users\\<User Name>\\desktop\\test\\");
    String target_file;
    File folderToScan = new File(pathToScan);
    File myFile = null; // Added this

    File[] listOfFiles = folderToScan.listFiles();

    for (int i = 0; i < listOfFiles.length; i++) {
        if (listOfFiles[i].isFile()) {
            target_file = listOfFiles[i].getName();
            myFile = listOfFiles[i]; // Added this
            if (target_file.startsWith("YYYY")) {
                System.out
                        .println("-----------------------------------------------");
                System.out.println("Match Found: " + target_file);
                // if the target file exists
                if (myFile.exists()) {

                    long lastmod = myFile.lastModified();
                    SimpleDateFormat format = new SimpleDateFormat(
                            "YYYY-MM-DD");
                    String lastmodi = format.format(new Date(lastmod));

                    File newfile = new File(
                            "C:\\Users\\<User Name>\\desktop\\test\\"
                                    + lastmodi + ".csv");

                    // If rename successful, then print success with file
                    // location
                    if (myFile.renameTo(newfile)) {
                        System.out
                                .println("File Name Change Successful, New File Created:"
                                        + newfile.getPath());
                    } else {
                        System.out.println("File Name Change Failed");
                    }
                }
            }
        }
    }
字符串路径扫描=(“C:\\Users\\\\desktop\\test\\”;
字符串目标文件;
File folderToScan=新文件(pathToScan);
File myFile=null;//添加了这个
文件[]listOfFiles=folderToScan.listFiles();
for(int i=0;i
我将我的文件设置到我的目录“C:\\Users\\desktop\\test\\”当我运行程序时,为什么我得到的日期不正确,例如“2014-06-162”?另外,当我尝试重命名两个文件时,它会变成一个文件。你能帮助我吗?使用YYYY-MM-dd而不是yyy-MM-dd。dd表示一年中的天数。当然,当你重命名原始文件时,将是重命名的文件。如果你想要两个文件,你需要复制而不是重命名。谢谢你的帮助。当我尝试在命令promp上运行java文件时也是如此它告诉我有一个错误,“空指针异常错误”;但是,它符合。