該程序應該創建一個.txt文件的空白記錄的數量,它可以稍後編輯,以添加名稱和平衡..等信息。我的問題是每當我放置另一條記錄時,第一個帳戶的餘額就會消失(但名稱不會)。如果我嘗試添加另一個帳戶,則以前帳戶的餘額也會消失。它可能被覆蓋或什麼的。txt文件中的數據被覆蓋
我該如何解決這個問題?
import java.io.*;
public class CreateBankFile {
private RandomAccessFile file;
public static void main(String[] args) throws IOException {
CreateBankFile blank = new CreateBankFile();
}
public CreateBankFile() throws IOException{
initialize();
int task = 0;
while(task == 0)
{
writeRecord();
}
}
private void initialize() {
Account acc = new Account();
try {
File fileName = new File("C:/Documents and Settings/x/Desktop/hehe.txt");
file = new RandomAccessFile(fileName, "rw");
// Create 100000 blank records in the file
for (int i = 0; i < 100001; i++)
acc.write(file);
} catch (IOException ioex){
System.out.println("File does not exists");
}
}
private void writeRecord() throws IOException {
Account acc = new Account();
BufferedReader data = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Account # ");
String accNo = data.readLine();
int recordNumber = Integer.parseInt(accNo);
try {
File fileName = new File("C:/Documents and Settings/x/Desktop/hehe.txt");
file = new RandomAccessFile(fileName, "rw");
// Write the record to the file
if (recordNumber > 0 && recordNumber < 100001) {
System.out.println("Enter Account Name: ");
String neym = data.readLine();
System.out.println("Enter Remaining Balance: ");
String numbah = data.readLine();
acc.setName(neym);
acc.setBalance(numbah);
// Go to the record location
file.seek((recordNumber - 1) * acc.size());
// Write the record out
acc.write(file);
System.out.println("\nUpdate Succesful!\n");
System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
}
} catch (Exception ioex){
System.out.println("File does not exists");
}
}
}
class Account {
private String name;
private String balance;
// Constructor
public Account() { this ("", ""); }
// Overloaded Constructor
public Account(String n, String b) {
name = n;
balance = b;
}
// Public method to read the data from the file
public void read(RandomAccessFile file) throws IOException {
setName(padData(file));
setBalance (padData(file));
}
// Public method to write the data to the file
public void write(RandomAccessFile file) throws IOException {
writeData(file, getName());
writeData(file, getBalance());
}
// The method that actually writes the data to the file
private void writeData(RandomAccessFile f, String n) throws IOException {
StringBuffer buf = null;
if (n != null)
buf = new StringBuffer(n);
else
buf = new StringBuffer(20);
buf.setLength(20);
f.writeChars(buf.toString());
}
// This pads the data so to make the data all the same size
// we will go for a size of 20
private String padData(RandomAccessFile f) throws IOException {
char data[] = new char[ 20 ], temp;
for (int i = 0; i < data.length; i++) {
temp = f.readChar();
data[ i ] = temp;
}
return new String (data).replace('\0', ' ');
}
// This method hard codes the value for the size of the record
// which is 20
public static int size() { return 20; }
// The get and set methods
public void setName(String n) { name = n; }
public void setBalance(String b) { balance = b; }
public String getName() { return name; }
public String getBalance() { return balance; }
}
您是否嘗試過使用調試器逐步執行文件操作? –
在你對這個解決方案投入太多之前,讓我指出,你的解決方案依賴於你的文本文件中所有'記錄'的長度完全相同。一旦用戶在記事本或其他文本編輯器中打開該文件,所有投注都將關閉。實際上,您編寫程序的方式只會存在一條「線」,因爲每條固定長度記錄/行末尾都沒有新行或crlf對。在我看來,你正在努力實現的可以使用電子表格來完成,而不需要任何代碼。 –