Хорошо, давайте ответим на ваш вопрос.
Scanner in = new Scanner(System.in);
File inputFile = new File(hello.txt);
Scanner in = new Scanner(inputFile);
PrintWriter out = new PrintWriter(hello.txt);
out.println("Hello, World!");
String line = in.nextLine();
in.close();
out.close();
Ваш код не компилируется, потому что вы ввели две переменные с одинаковым именем in
, а вы не объявили hello.txt
. Чтобы решить эту проблему, следуя вашей идее.
public static void main(String[] args) throws Exception {
String filePath = "hello.txt";
File inputFile = new File(filePath);
PrintWriter printWriter = new PrintWriter(inputFile);
printWriter.write("Hello World!");
printWriter.close();
InputStream is = new FileInputStream(inputFile.getPath());
BufferedReader buf = new BufferedReader(new InputStreamReader(is));
String line = buf.readLine();
System.out.println(line);
is.close();
buf.close();
}
Добро пожаловать в мир java!