Untitled

 avatar
unknown
plain_text
2 years ago
748 B
3
Indexable
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class WriteToFileExample {
    public static void main(String[] args) {
        String filePath = "example.txt";
        String data = "Hello, World!";
        
        try {
            OutputStream outputStream = new FileOutputStream(filePath);
            
            byte[] bytes = data.getBytes();
            outputStream.write(bytes);
            
            outputStream.close();
            
            System.out.println("Data written to the file successfully!");
        } catch (IOException e) {
            System.out.println("An error occurred while writing to the file: " + e.getMessage());
        }
    }
}
Editor is loading...