answersLogoWhite

0

How do you create a File in Java?

Updated: 8/10/2023
User Avatar

Wiki User

16y ago

Best Answer

There are a number of ways to create a file in java. The following example demonstrates a few:

import java.io.*;

import java.nio.charset.*;

public class Main {

public static void main(String[] args) {

try {

// Simplest way

PrintWriter out1 = new PrintWriter("file1.txt");

out1.println("hello file1!");

out1.close();

// Append to existing file if it already exists

PrintWriter out2 = new PrintWriter(new FileWriter("file2.txt", true));

out2.println("hello file2!");

out2.close();

// If non-default charset is needed

Charset charset = Charset.forName("UTF-16");

PrintWriter out3 = new PrintWriter(new OutputStreamWriter(new FileOutputStream("file3.txt"), charset));

out3.println("hello file3!");

out3.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

User Avatar

Wiki User

16y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

12y ago

1. create java file.

2. create main program

3. use File class to create file object

4. read javadoc for exact method and classes in Java or use IDE like Netbeans and Eclipse

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

asfwdgf

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you create a File in Java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How do you create setup of core java project?

how to create setup file in core java


Program in Java to create file and copy content of an existing file to it?

You need to use File class to create file in java and than Reader class implementation such as BufferedReder to read content.


How you make a project in java?

You create a text file with a .java extension. Then you put a proper class definition.


How do you create a project in Java?

It depends on what IDE you're using. For example, in JCreator, to create a new project you go to File > New > Project... (or press ctrl + shift + N) Or in Eclipse, click on File> New > Java Project


Who create Java and when?

Who create Java & when? Why he create java ? What are mane functions of it?


When java uses compiler?

What i know is java we will use compiler when it want to get class file(file with .class extension) from java file(file with .java extension).


Will the java compiler translate a source file that contains syntax errors?

No it will not. Any java source file that has syntax errors will not be translated fully. The compiler will spit out errors based on the syntax problems in your code.


What is the java file in java?

The java file has the actual JAVA code present in it,..i.e the entire coding is done in that file.. you can view it using any text editor.. After compiling it you get a class file of every class present in the java file When you open a class file in a text editor, you won't see anything that makes sence. You won't find the original code that you wrote in the java file. That is because a class file is a product of the compilation of a java file.


What type of file is created by Java?

Java created a file called class, and is identified by having the .class at the end. This file includes a Java bytecode, which then can be used on the Java Virtual Machine.


How java platform independent with complete architecture?

java is platform independent because java code is compile on javac compiler after create one class file this file is run any of paltform run this code becauase jvm is understand this code properly managed. jatin patel


How do you save a notepad file into a java file?

just add an extension .java when you save your notepad text file


What is java class file?

That's the compiled Java file, the one that can be distributed to the end-users.