write file Java

HOW does it work?

the easiest way is to write a file, is by using the preset class FileWriter

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

  2. import
    1. FileWriter
    2. preset exception class, for example IOException
      import java.io.BufferedWriter;
      import java.io.IOException;
  3. create a FileWriter object
    in the start method
    void start () {
    	FileWriter writer = new FileWriter(location);
    }
  4. 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();
    }
  5. 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

file Java

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