2014-10-20 38 views
0

我想将角色的名称和性别导出到文本文件。另外,我需要在Mac和PC电脑上导出。我尝试了各种方法,但都没有为我工作,或者我可能错误地放置了它们。将数据从Java导出到文本文件

我改变了我的代码和我尝试:

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: error: unreported exception FileNotFoundException; must be caught or declared to be thrown.

回答

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文档。

0

尝试使用文件编写器而不是打印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(); 
+1

这仍然会遇到OP的问题,这是没有处理异常。它只是一个IOException,而不是FileNotFoundException。 – Grice 2014-10-20 20:00:18

-1

既然你是从控制台& 读取数据写入到文件建议使用的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); 
    } 
} 

OR

您可以使用try-with-resources,可从JDK 1.7起,这将负责关闭使用的资源。在你的情况下,它是一个文件。

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(); 
} 
+0

OP不从文件读取,他正在从标准输入读取。他想写一个文件。 – Grice 2014-10-20 20:02:18