Java FileWriter Class

Java FileWriter Class:

We can use the Eclipse IDE for this example. If you do not know about it then follow this link- How to Install Eclipse For Java and create a program in it.

Java FileWriter Class extends OutputStreamWriter class and is a convenient class for writing character files. When a file is created and it does not exist prior to its creation, it will be created. In case a read-only file is attempted to open, an IOException will be thrown. Some of the constructor forms of this class are shown as follows.

ConstructorDescription
FileWriter (String file)Creates a new file. It gets the file name in a string.
FileWriter (File file)Creates a new file. It gest file name in the File object.

Methods of Java FileWriter Class:

MethodDescription
abstract void close ( )Closes the stream, flushing it first.
abstract void flush ( )Flushes the stream, that is, all output buffers are closed.
void write (char [ ], cbuf)Writes an array of characters.
Abstract void write(char [ ], cbuf, int off, int len)Writes a portion of an array of characters.
void write (String str)Writes a string.
void write (String str, int off, int len)Writes a portion of the string.

Java FileWriter Class Example:

FileWriter Class Java - Java FileWriter Class
import java.io.FileWriter;
import java.io.IOException;


public class File_writer {

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		
		FileWriter fw = new FileWriter ("java.txt");
		fw.write("I want to Learn Java Programming....");
		fw.close();
	
		System.out.println("Program Build Successfully...");
	}

}

After writing the above code, right-click on the project folder and go to properties as shown below.

java project folder - Java FileWriter Class

In the resource tab, select the location and copy it as shown below.

java resource location - Java FileWriter Class
paste java location - Java FileWriter Class
java.txt created by java filewriter class - Java FileWriter Class

Now open java.txt with notepad and your written text in the code editor will be seen here.

java.txt notepad - Java FileWriter Class

Tutorial MySQL
Tutorial Python
Procedure-Oriented Programming
Object-Oriented Programming
Modular Programming
Structured Programming
WHILE Loop in C Language
Java (programming language)– Wikipedia

Comments (No)

Leave a Reply