我编写了下面的代码&它创建了一个文件,并完美地写入其中,但我希望在输出中看到文件的内容,但我只得到了这条消息:"java.io.BufferedWriter@140e19d“。我不明白!有人能给我解释一下为什么我会收到这条消息吗?我应该怎么做才能看到文件的内容呢?tnx。
下面是我的代码:
package com.example.idea;
import java.io.FileNotFoundException;
import java.util.Formatter;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Formatter file = null;
Scanner sc =null;
try {
file = new Formatter("D:\\test.txt");
file.format("%s %s", "Hello", "World");
sc = new Scanner(String.valueOf(file));
while (sc.hasNext()){
System.out.println(sc.next());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally {
if (file != null) {
file.close();
}
if (sc != null) {
sc.close();
}
}
}
}
发布于 2018-06-02 16:49:19
使用以下命令:
sc = new Scanner(file);
https://stackoverflow.com/questions/50654736
复制相似问题