我需要代码将Aidan Dowling
这样的单行写入文本文件。
到目前为止我的代码是:
package battleships.startmenu;
import java.util.Scanner;
public class Startmenu {
public static void main(String[] args) {
System.out.print("Hello! Please enter your first name: ");
Scanner reader = new Scanner(System.in);
String a;
String b;
a=reader.next();
System.out.print("Enter your second name: ");
b=reader.next();
System.out.println("Hello " + a+ " " + b + "!");
System.out.println("\tHello");
String name;
name = a + b;
}
}
我希望变量名称打印到名为name.txt的文档
答案 0 :(得分:1)
创建作家
PrintWriter writer = new PrintWriter("name.txt", "UTF-8");
写一行
writer.println(name);
关闭作家
writer.close();
答案 1 :(得分:0)
将name
写入name.txt
:
Files.write(Paths.get("name.txt"), Arrays.asList(name), StandardCharsets.UTF_8);