answersLogoWhite

0

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

17y ago

What else can I help you with?

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


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).


Who create Java and when?

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


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 do you save a notepad file into a java file?

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


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


What is JFM in Java and what is the use of it?

jFM is a java file manager. It is used to access the file system.