2

I want to ask you about the format of initialization in java.

What I know in the moment is:

int a = 1;
double b = 1.0;
String c = "java";

etc.

Now, in the class main I want to initialize a File, and I don't know how to do this?

ItamarG3
  • 4,092
  • 6
  • 31
  • 44
N. Liraj
  • 47
  • 1
  • 1
  • 5
  • It's hard to say "I want to initialise a file" without providing more information. You could try `File f = new File("some/location/here")` but that's not an _actual_ file. It may or may not work, depending on what you want to do with it, though. Can you add more details about the file and how are you going to use it? – VLAZ Oct 09 '16 at 15:44
  • 1
    Possible duplicate of [How to create a file and write to a file in Java?](http://stackoverflow.com/questions/2885173/how-to-create-a-file-and-write-to-a-file-in-java) – Chris Gong Oct 09 '16 at 16:11

2 Answers2

2

Firstly, a file is an object type, unlike int and double, which are primitive types. I am not sure how familiar you are with java, but to create an object you use the constructor for that object.

File has a constructor which receives a string of the location of that file on the computer.

File f = new File("path\\to\\your\\file");
ItamarG3
  • 4,092
  • 6
  • 31
  • 44
0

I think you are looking for something like this:

PrintWriter fileWriter = new PrintWriter("myFile.txt", "UTF-8");
fileWriter.println("Write to file.");
fileWriter.close();

This will create a file called myFile.txt and write the line "Write to file." to it.

c-bro
  • 486
  • 2
  • 13