在java中从控制台输入一些字符串,并将该类信息保存到日志文件中,怎么做啊????

2024-12-18 13:31:23
推荐回答(1个)
回答1:

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class FileWrite {

public void fileWrite() throws IOException{
Scanner scanner = new Scanner(System.in);
System.out.println("输入一些东西:");
String string = scanner.next();
File file = new File("C://","Example.txt");
if(!file.exists()){
file.createNewFile();
}
FileWriter fileWriter = new FileWriter(file);
fileWriter.write(string);
fileWriter.close();

}

public static void main(String[] args) throws IOException{
FileWrite fWrite = new FileWrite();
fWrite.fileWrite();
}
}