在文件中编写完成的Java示例


在文件中编写完成的Java示例

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileWriter;

import java.io.IOException;



public class FileExample {

   public static void main(String[] args) {

      try {

         String content = "codingdict is one the best site in the world";

         File file = new File("C:\\Users\\codingdict7\\Desktop\\abc.txt");

         if (!file.exists()) {

            file.createNewFile();

         }

         FileWriter fw = new FileWriter(file.getAbsoluteFile());

         BufferedWriter bw = new BufferedWriter(fw);

         bw.write(content);

         bw.close();



         System.out.println("Done");

      } catch (IOException e) {

         e.printStackTrace();

      }

   }

}