嘿堆栈溢出的人,我有点麻烦了解如何编写和附加文件的工作原理。这是我一直要求做的事爪哇写作/附加文件
•将合约的数据保存(追加)到基于文本的摘要文件(contracts.txt)。
文件中的每一行必须持有的细节与用空格或制表符分隔下列信息的单一合同:
•合同日期(今天的日期 - 看技术细节格式)。
•包装(1 =小,2 =中等3 =大)
•数据包(1 =低,2 =中等,3 =高和4 =无限制)。
•时期月
•允许从封装分钟(是或否)
•参考号码拨打国际电话。
•每月费用(以便士计算)。
•客户名称。
我期待写的那种文件的例子是this。
到目前为止,我有这样的代码
public void appendFile()
{
PrintWriter output = null;
File confidential = new File("contracts.txt");
try
{
// create new file
FileWriter fw = new FileWriter(confidential, true);
output = new PrintWriter(fw);
}
catch (FileNotFoundException e) // Problem with file
{
System.out.println("Error: Problem creating the file! Program closing");
System.exit(0);
}
catch (IOException ex)
{
System.out.println("Error: Problem creating the file! Program closing");
System.exit(0);
}
output.print(date);
output.print(" ");
output.print(minutes);
output.print(" ");
output.print(data);
output.print(" ");
output.print(length);
output.print(" ");
output.print(international);
output.print(" ");
output.print(ref);
output.print(" ");
output.print(price);
output.print(" ");
output.print(name);
output.close();
}// end of main`
正如你看到的,我的代码可能是错的,我不知道这种方法是否甚至不应该在一个单独的类或只是坚持代码它在主要课程?我很欣赏花时间来解决我的问题,我希望有人能够帮助我,因为我现在正要挫败我的头发,不知道该怎么做。
当你运行你的代码时会发生什么?看起来不错,期待一个非常小的bug,那个bug应该是你真正的问题。 – Ferrybig
@TangoSheep在问一个新问题之前,你应该搜索这个问题。 – mob41
@AnthonyLaw我试图搜索,没有问题有帮助我的答案。 – TangoSheep