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?
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?
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");
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.