我想将字符的名称和性别导出到文本文件中。此外,我需要在Mac和PC计算机上导出。我尝试了各种方法,但没有一种方法适用于我,或者我可能将它们放错了。
我尝试改变了我的代码:
import java.util.Scanner;
import java.io.PrintWriter;
import java.io.File;
import java.util.Scanner;
public class Character {
public static void main (String[] args) {
String characterGender;
String characterName;
Scanner scan = new Scanner(System.in);
System.out.println("Welcome to Ricardo's character creator!");
System.out.println("Is your character a boy or a girl?");
characterGender = scan.nextLine();
if (characterGender.equals("girl")) {
System.out.println("Awesome! Please enter her name: ");
characterName = scan.nextLine();
} else {
System.out.println("Awesome! Please enter his name: ");
characterName = scan.nextLine();
}
System.out.print("Alright! " + characterName + " has been created!");
PrintWriter out = new PrintWriter("Character.txt");
out.println(characterName);
out.println(characterGender);
out.close();
}
}
我收到这样的错误:
Character.java:34:错误:未报告的异常FileNotFoundException;必须被抓或宣布被抛出。
答案 0 :(得分:0)
您需要处理FileNotFoundException。这可以通过以下方式完成:
捕获异常(首选方法):
PrintWriter out;
try {
out = new PrintWriter("Character.txt");
out.println(characterName);
out.println(characterGender);
out.close();
} catch (FileNotFoundException e) {
System.err.println("File doesn't exist");
e.printStackTrace();
}
或抛出异常:
public static void main (String[] args) throws FileNotFoundException
注意:两种情况都必须包含import java.io.FileNotFoundException;
。
以下是Exceptions上的一些java文档。
答案 1 :(得分:0)
尝试使用文件编写器而不是print Writer。 下面是我的示例代码,希望你不要介意这个硬解释。
String filetowrite = "C:/Users/Desktop/test.txt"; // Point to your location
FileWriter fw = new FileWriter(filetowrite);
fw.write("Hello, This is the test");
fw.close();
答案 2 :(得分:-1)
由于您从Console& amp;读取数据将其写入文件建议使用BufferedReader& BufferedWriter 。处理异常&如果您想使用Log4j添加日志文件中发生的异常的详细信息,您可以执行以下操作:
BufferedReader br = null;
BufferedWriter bw = null
Logger logger = LogManager.getLogger(Character.class);
try {
String data;
br = new BufferedReader(new InputStreamReader(System.in));
File file = new File("Character.txt");
// if file doesn't exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
bw = new BufferedWriter(fw);
while ((data = br.readLine()) != null) {
System.out.println(data);
bw.write(data);
}
} catch (IOException e) {
StringWriter() stack = new StringWriter();
e.printStackTrace(new PrintWriter(stack));
logger.fatal(stack.toString(), Character.class);
} finally {
try {
if (br != null && bw != null){
bw.close();
br.close();
}
} catch (IOException ex) {
StringWriter() stack = new StringWriter();
e.printStackTrace(new PrintWriter(stack));
logger.fatal(stack.toString(), Character.class);
}
}
或
您可以使用JDK 1.7以后的try-with-resources
,它将负责关闭所使用的资源。在你的情况下,它是一个文件。
try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
String data;
File file = new File("Character.txt");
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
while ((data = br.readLine()) != null) {
System.out.println(data);
bw.write(data);
}
} catch (IOException e) {
e.printStackTrace();
}