-2
package com.company;

public class Main {

    public static void main(String[] args) {
    java.io.File file = new java.io.File("image/us.gif");
    System.out.println("Does it exist:" + file.exists());
    System.out.println("The file has " + file.length() + "bytes");
    System.out.println("Can it be read? " + file.canRead());
    }
}

I copied this code from my book Introduction to Java Programming, and it compiles correctly but it doesn't create the file, and returns false and zero bytes for the methods. Can someone help please I will give best answer.

xAnnette97
  • 13
  • 3
  • Do you want to create or to read the file? – Jorge Campos Feb 11 '17 at 01:05
  • 2
    `new java.io.File("image/us.gif");` alone basically does nothing. You need a byte stream of some sort for reading or writing. What are you trying to do? – Mick Mnemonic Feb 11 '17 at 01:06
  • 3
    `File` is a virtual concept of a `File`, it doesn't have to exist, instead, if you want to "create" a `File`, represented by the virtual concept, you should use [`File#createNewFile`](https://docs.oracle.com/javase/7/docs/api/java/io/File.html#createNewFile()) - beware, this will create a empty file. Assuming you know of a `File` on the system, you could point `File` to it and it will give you better results – MadProgrammer Feb 11 '17 at 01:07
  • Right-o. `File` is basically just a path name. The file it points to may or may not actually exist (that's why there's an `.exists()` method). It's up to you to create the file if you want to, and put data in it if you desire. The system doesn't do any of that for you. – markspace Feb 11 '17 at 01:14
  • Ok thanks guys I got it now. No answer in comments. Do I just delete or close? – xAnnette97 Feb 11 '17 at 01:17
  • This code isn't meant to create a file. Your question is based on a misapprehension. – user207421 Feb 11 '17 at 01:17
  • FYI, it's a common mistake, so don't feel bad. – Ellen Spertus Feb 11 '17 at 01:21
  • Possible duplicate of [How to create a file in a directory in java?](https://stackoverflow.com/questions/6142901/how-to-create-a-file-in-a-directory-in-java) – Bernhard Barker Mar 25 '18 at 08:45

1 Answers1

2

You will have to create the file manually unless it already exists. Creating a new File object should not be confused with creating a file in the filesystem.

To create the file you will have to use the method createFile(); which exists in the class File:

File someFile = new File("path.to.file");
someFile.createFile();

It would also be a good idea to check if the file exists before creating it to avoid overwriting it. this can be done by:

File someFile = new File("path.to.file");
if(!someFile.exists()) {
    someFile.createFile();
}

This will create a new empty file. That means that it's length will be 0. To write to the file you will need a byte stream. For example, using a FileWriter:

File test = new File("SomeFileName.txt");
FileWriter fw = new FileWriter(test);
fw.append("Hello! :D");
fw.close();


Note: Some methods i used in the examples above throw exceptions which you will have to handle.

fill͡pant͡
  • 1,147
  • 2
  • 12
  • 24