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.
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?Fileis a virtual concept of aFile, it doesn't have to exist, instead, if you want to "create" aFile, represented by the virtual concept, you should useFile#createNewFile- beware, this will create a empty file. Assuming you know of aFileon the system, you could pointFileto it and it will give you better resultsFileis 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.