开发者

Groovy - create file issue: The filename, directory name or volume label syntax is incorrect

开发者 https://www.devze.com 2023-01-22 15:31 出处:网络
I\'m running a script made in Groovy from Soap UI and the script needs to generate lots of files. Those files have also in the name two numbers from a list (all the combinations in that list are diffe

I'm running a script made in Groovy from Soap UI and the script needs to generate lots of files. Those files have also in the name two numbers from a list (all the combinations in that list are different), and there are 1303 combinations available and the script generates just 1235 files.

A part of the code is:

filename = groovyUtils.projectPath + "\\" + "$file"+"_OK.txt";
targetFile = new File(filename);
targetFile.createNewFile();

where $file is actually that part of the file name which include those 2 combinations from that list:

file = "abc" + "-$firstNumer"+"_$secondNumber"

For those file which are not created is a message returned:"The filename, directory name or volume label syntax is incorrect".

I've tried puting another path:

filename = "D:\\rez\开发者_运维知识库\" + "\\" + "$file"+"_OK.txt";
targetFile = new File(filename);
targetFile.createNewFile(); 

and also:

File parentFolder = new File("D:\\rez\\");
File targetFile = new File(parentFolder, "$file"+"_OK.txt");
targetFile.createNewFile();

(which I've found here: What are possible reasons for java.io.IOException: "The filename, directory name, or volume label syntax is incorrect") but nothing worked.

I have no ideea where the problem is. Is strange that 1235 files are created ok, and the rest of them, 68 aren't created at all.

Thanks,


My guess is that some of the files have illegal characters in their paths. Exactly which characters are illegal is platform specific, e.g. on Windows they are

\ / : * ? " < > |

Why don't you log the full path of the file before targetFile.createNewFile(); is called and also log whether this method succeeded or not, e.g.

filename = groovyUtils.projectPath + "\\" + "$file"+"_OK.txt";
targetFile = new File(filename);
println "attempting to create file: $targetFile"

if (targetFile.createNewFile()) {
    println "Successfully created file $targetFile"
} else {
    println "Failed to create file $targetFile"
}

When the process is finished, check the logs and I suspect you'll see a common pattern in the ""Failed to create file...." messages


File.createNewFile() returns false when a file or directory with that name already exists. In all other failure cases (security, I/O) it throws an exception.

Evaluate createNewFile()'s return value or, additionally, use the File.exists() method:

File file = new File("foo")
// works the first time
createNewFile(file)
// prints an error message
createNewFile(file)

void createNewFile(File file) {
    if (!file.createNewFile()) {
        assert file.exists()
        println file.getPath() + " already exists."
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号