使用扫描仪类在文本文件中书写

Vin*_*rma 2 java file java.util.scanner

我遇到了很多关于如何使用ScannerJava 读取文本文件的程序.以下是使用Scanner以下方法在Java中读取文本文件的一些虚拟代码:

public static void main(String[] args) {

    File file = new File("10_Random");

    try {

        Scanner sc = new Scanner(file);

        while (sc.hasNextLine()) {
            int i = sc.nextInt();
            System.out.println(i);
        }
        sc.close();
    } 
    catch (FileNotFoundException e) {
        e.printStackTrace();
    }
 }
Run Code Online (Sandbox Code Playgroud)

但是,请任何人帮助我.txt使用Scannerjava 在文件中"编写"一些文本(即字符串或整数类型文本).我不知道如何编写该代码.

小智 7

Scanner不能用于写作目的,只能用于阅读.我喜欢用a BufferedWriter来写文本文件.

BufferedWriter out = new BufferedWriter(new FileWriter(file));
out.write("Write the string to text file");
out.newLine();
Run Code Online (Sandbox Code Playgroud)