
HOW does it work?
the easiest way is to write a file, is by using the preset class FileWriter
- create a standard main-program setup
package File;
public class Write {
void start () {
}
public static void main(String[] args) {
new Write().start();
}
}
a small example as this does not necessarily need a constructor
- import
- FileWriter
- preset exception class, for example IOException
import java.io.BufferedWriter;
import java.io.IOException;
- create a FileWriter object
in the start method
void start () {
FileWriter writer = new FileWriter(location);
}
- If you want to make your code more efficient: import BufferWriter and create a BufferedWriter object
void start() throws IOException {
FileWriter writer = new FileWriter(location);
BufferedWriter bufferedWriter = new BufferedWriter(writer);
bufferedWriter.write(fileContent);
bufferedWriter.close();
}
- create the value for the variables :
String fileContent = "HELLOWORLD";
String location = "/Users/x/Downloads/example.txt";
Program
package File;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class Write {
String fileContent = "HELLOWORLD";
String location = "/Users/x/Downloads/example.txt";
void start() throws IOException {
FileWriter writer = new FileWriter(location);
BufferedWriter bufferedWriter = new BufferedWriter(writer);
bufferedWriter.write(fileContent);
bufferedWriter.close();
}
public static void main(String[] args) throws IOException {
new Write().start();
}
}
OUTPUT

Synonym: FILE OUtPUT | BUFFERREADER | FILESTREAM | DISPLay output in FILE