有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如何将字符串保存到。java中的txt文件

我已经完成了一个应用程序,测试了所有的功能,它们正在工作。然而,输入数据的一部分应该保存到数据库中。txt文件。我把它放在一个字符串中,但我对这个领域有点不了解,不知道如何将它保存到我电脑上的驱动器中。任何帮助都将不胜感激。代码在此链接上:http://sharetext.org/fSy2


共 (1) 个答案

  1. # 1 楼答案

    试试这个:

    public static void main(String args[])throws IOException {
    
      File file = new File("Hello1.txt");
      // creates the file
      file.createNewFile();
      // creates a FileWriter Object
      FileWriter writer = new FileWriter(file); 
      // Writes the content to the file
      writer.write("This\n is\n an\n example\n"); 
      writer.flush();
      writer.close();
    }
    

    阅读更多关于here