【问题标题】:Unable to write to file Scanner try/catch Java无法写入文件扫描仪尝试/捕获 Java
【发布时间】:2015-03-03 14:45:18
【问题描述】:

当我运行这个程序时,它意味着写入文本文件,文本文件与我的项目文件夹位于同一目录中。

try
{                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
Scanner inFile = new Scanner(new File ("details.txt"));
while(inFile.hasNext())
{
String line = inFile.nextLine(); //reads the line of text
Scanner del = new Scanner(line).useDelimiter("#"); //scanner class to delimit
String name = del.next();
String gender = del.next();
int age = del.nextInt();
double amount = del.nextDouble();
txaDisplay.append(name+"\t"+gender+"\t"+age+"\t"+amount+"\n");
del.close();
}
inFile.close();//end try
}
catch(FileNotFoundException f)
    {
    txaDisplay.append("File Not Found");
    System.exit(0);}
}                                        


public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new yest().setVisible(true);
        }
    });}

【问题讨论】:

    标签: java loops output


    【解决方案1】:

    您需要实际写入文件。这是我给不熟悉文件输出的人的示例。这使用FileWriter 输出流。注意Data 这里是ArrayList

    public static void WriteFile(File file)
    {
        System.out.println("Writing to file");
        //Use a FileWriter to output to file
        FileWriter oFile = null;
        try
        {
            oFile = new FileWriter(file, false); //Set to true if you don't want to overwrite existing contents in file
    
            // Begin writing to file... line by line
            for (String line : Data)
                oFile.write(line + System.getProperty("line.separator")); //Since notepad doesn't display newline characters (\n) use this
                                                                          //If using another text document viewer then just use + "\n"
            //Flush and close output stream
            oFile.flush();
            oFile.close();
        } catch (IOException e)
        {
            // Handle it!
            e.printStackTrace();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2022-12-15
      • 2011-02-07
      • 2016-01-02
      • 2014-02-11
      • 1970-01-01
      • 2019-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多