gpt4 book ai didi

java - 无法写入文件扫描仪 try/catch Java

转载 作者:行者123 更新时间:2023-12-01 11:50:24 26 4
gpt4 key购买 nike

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

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);
}
});}

最佳答案

您需要实际写入文件。这是我为刚接触文件输出的人提供的一个示例。这使用 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();
}
}

关于java - 无法写入文件扫描仪 try/catch Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28834702/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com