V
V
vapi2018-04-19 22:30:04
Java
vapi, 2018-04-19 22:30:04

How to create an object of type File?

Hello. There is a problem. Cannot create a File object representing a file on disk. I create a File object for a directory - everything is in order, but when I create a File object for a file - it does not work
Create a File for the directory

public static void main(String[] args) {

        // определяем объект для каталога
        File dir = new File("C://Java projects");
        if(dir.isDirectory()){
            System.out.println("это директория");  //работает
        }
    }

Create a file for a directory
public static void main(String[] args) {

        File file=new File("C://Java projects//Films.txt");
        if(file.isFile()){
            System.out.println("это файл");
        }else {
            System.out.println("это не файл"); // выводи эту строку
        }
   }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
vapi, 2018-04-19
@vapi

Everything works, I'm sorry. due to inattention, I was stupid for 40 minutes. the file name is Fimls, when it specified an absolute path in the File constructor, the file name was specified by Films.

L
l1l1l1, 2018-04-19
@l1l1l1

Try to use existsfor space isFile.
And I would like to see the error that the program gives you.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question