在线等...紧急求救!java如何将控制台的文件写入txt文本,怎样将以下输出的代码保存到txt文本中???

2024-12-27 22:13:53
推荐回答(1个)
回答1:

void saveToFile(String path, String name, String con)
{
if(!name.endsWith(".txt"))
name = name + ".txt";
File file = new File(path + File.separator + name);
FileOutputStream fos = null;
try
{
if(!file.exists())
{
file.createNewFile();
}
fos = new FileOutputStream(file);
fos.write(con.getBytes());
} catch (Exception e)
{
System.out.println("文件出错");
}
finally
{
try
{
fos.flush();
fos.close();
} catch (IOException e)
{
}
}
}