Java considers new files to be folders

My Java program keeps thinking that every new file is not a file, but a folder.

I had a search error, and now I can not save the file using FileWriter . If I create a new file and check if it is a file or a directory, it says that it is a directory.

I initially had a long way to go, which is why I got rid of this. I also used to write in a separate thread, and got rid of it, but the problem still persists.

If I create a new class with simple

 java.io.File file = new java.io.File("test.csv"); output.print(file.isDirectory()); 

It looks like the truth.

However, I can save the graphics using javax.imageio. *

Can anyone help?

Edit: I am using eclispe 3.7.2 with java 1.7.0 ... File.isFile () is false, File.createNewFile () fails (java.io.FileNotFoundException (Access is Denied)). When searching in the directory that I specify, there is a new folder called test.csv. Therefore, it creates a new folder and treats it as a folder, despite the fact that I indicate that it is a csv file. I tried other file types, even without a file type. But the same problem. He believes that this is a folder, not a file. It just puzzles me.

+4
source share
4 answers

Java new File() does NOT independently create files or directories. Therefore, you must show your code. I suspect you have something like file.mkdirs(); somewhere - if your file refers to "test.csv", at that moment it will create a directory called "test.csv"

+9
source

In your sample code, you have not created a file yet. Can you also print the value of file.isFile() ?

for our entertainment.

Then do the same, but call, for example. file.createNewFile() first, to get the file.

+1
source

I think you are missing something, maby you have the directory in which you are located, and which is called the same as the file you are trying to create, you can check this using the isFile () function to make sure that it is not a file and is trying to get the path using getAbsolutePath () to localize where this directory is installed.

0
source

Found a problem. As expected, it was my own stupidity, as I knew. I moved the File.mkdirs () method after I added the file name to the file path, making the full path, including the file, directory. Then I could not write, because it was already created as a directory.

I could swear that I tested it without the mkdirs () command, but I think not. Well. Scratch even before stupidity and do not delete all possible options.

Thanks for all the tips!

0
source

Source: https://habr.com/ru/post/1434787/


All Articles