Методы записи файлов Java: как записать файл в Java

Вот несколько методов, которые можно использовать в Java для записи в файл:

  1. Использование класса FileWriter:

    import java.io.FileWriter;
    import java.io.IOException;
    public class FileWriteExample {
       public static void main(String[] args) {
           String data = "Hello, World!";
           try {
               FileWriter writer = new FileWriter("output.txt");
               writer.write(data);
               writer.close();
               System.out.println("Data has been written to the file.");
           } catch (IOException e) {
               System.out.println("An error occurred.");
               e.printStackTrace();
           }
       }
    }
  2. Использование класса BufferedWriter:

    import java.io.BufferedWriter;
    import java.io.FileWriter;
    import java.io.IOException;
    public class BufferedWriterExample {
       public static void main(String[] args) {
           String data = "Hello, World!";
           try {
               BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt"));
               writer.write(data);
               writer.close();
               System.out.println("Data has been written to the file.");
           } catch (IOException e) {
               System.out.println("An error occurred.");
               e.printStackTrace();
           }
       }
    }
  3. Использование класса PrintWriter:

    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
    public class PrintWriterExample {
       public static void main(String[] args) {
           String data = "Hello, World!";
           try {
               PrintWriter writer = new PrintWriter(new FileWriter("output.txt"));
               writer.println(data);
               writer.close();
               System.out.println("Data has been written to the file.");
           } catch (IOException e) {
               System.out.println("An error occurred.");
               e.printStackTrace();
           }
       }
    }
  4. Использование класса Files (Java 7+):

    import java.io.IOException;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.StandardOpenOption;
    public class FilesWriteExample {
       public static void main(String[] args) {
           String data = "Hello, World!";
           try {
               Path path = Path.of("output.txt");
               Files.writeString(path, data, StandardOpenOption.CREATE);
               System.out.println("Data has been written to the file.");
           } catch (IOException e) {
               System.out.println("An error occurred.");
               e.printStackTrace();
           }
       }
    }
  5. Использование FileOutputStream:

    import java.io.FileOutputStream;
    import java.io.IOException;
    public class FileOutputStreamExample {
       public static void main(String[] args) {
           String data = "Hello, World!";
           try {
               FileOutputStream fos = new FileOutputStream("output.txt");
               byte[] bytes = data.getBytes();
               fos.write(bytes);
               fos.close();
               System.out.println("Data has been written to the file.");
           } catch (IOException e) {
               System.out.println("An error occurred.");
               e.printStackTrace();
           }
       }
    }