Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
748 B
0
Indexable
Never
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());
        }
    }
}