问题描述
此问题与线程因为过于宽泛而被关闭。
This question is related to thread Address Book Functionality which was closed considering being too broad.
我正在使用随机访问文件开发一个小型测试应用程序。我正在附上源代码。我想从文件中删除一条记录。我在stackoverflow上搜索了类似的线程。这些主题建议将一个标志设置为已删除记录而不显示它。我想删除它。我试图搜索记录并尝试将空记录附加到该文件指针位置。什么都没发生。请提示,最新的方法是什么?
I was developing a small test application using Random Access File. I am attaching the source code with it. I want to delete a record from the file. I have searched similar threads on stackoverflow Delete Record Random Access File.These threads suggest setting a flag as deleted against a record and not display it. I want to actually delete it. I tried to search the record and tried to append a null record to that file pointer position. Nothing happens. Please suggest, whats the correct way to do it?
我附上下面的源代码。
I am attaching the source code below.
package ben.io;
import java.io.Serializable;
/**
* @author Ben
* This is an object to hold phone book information
*/
public class PhoneBook implements Serializable {
String personName ;
String phoneNumber ;
String location ;
public PhoneBook() {
}
public PhoneBook(String personName, String phoneNumber, String location) {
this.personName = personName;
this.phoneNumber = phoneNumber;
this.location = location;
}
public String getPersonName() {
return personName;
}
public void setPersonName(String personName) {
this.personName = personName;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String toString(){
return "Name: " + personName + " Phone Number: " + phoneNumber + " Address: " + location;
}
}
具有所有功能的主类。
package ben.io;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import com.sun.xml.internal.ws.util.StringUtils;
/**
* @author Ben
* This class is responsible for testing various operations for Random Access File
*/
public class RandomAccessFileDriverTest {
public static void main(String[] args) throws IOException {
//Creating an instance of Random Access File
RandomAccessFile randomAccessFile = new RandomAccessFile("D:\\2014\\Habit Course 1 Study Targets\\Studies\\files\\ben.txt","rw");
//Finding size of Random Access File - Size is basically 36 why showing 38?
long size = randomAccessFile.length();
System.out.println("Size of Random Access file is: " + size);
//Finding position of File Pointer
long positionOfFilePointer = randomAccessFile.getFilePointer();
System.out.println("File Pointer Position: " + positionOfFilePointer);
//Reading Data from a Random Access File using readLine
/* randomAccessFile.seek(3);
String data = randomAccessFile.readLine();
System.out.println("Data is: " + data);*/
//Reading bytes from a Random Access File using read()
/* byte[] byteData = new byte[100];
randomAccessFile.read(byteData,3,18);
String strData = new String(byteData);
System.out.println("Data using Byte is: " + strData);
*/
//Adding record to Random Access File
PhoneBook phoneBook = new PhoneBook("Ben","12345","UAE");
//adding record for random access file
// randomAccessFile = addRecord(phoneBook,randomAccessFile);
//view records for random access file
// viewAll(randomAccessFile);
//Searching a record - Give a phone number as a key to Search
PhoneBook recordSearched = searchRecord("123456",randomAccessFile);
if(recordSearched == null){
System.out.println("Input is not valid");
}
else{
System.out.println(recordSearched);
}
//Delete a Record From File
deleteRecord("123456",randomAccessFile);
//View the records
viewAll(randomAccessFile);
}
public static RandomAccessFile addRecord(PhoneBook phoneBook, RandomAccessFile randomAccessFile) throws IOException {
long length = randomAccessFile.length();
//seeking position at end of file
randomAccessFile.seek(length);
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(phoneBook.getPersonName()).append(":").append(phoneBook.getPhoneNumber()).
append(":").append(phoneBook.getLocation());
if(randomAccessFile.getFilePointer() != 0){
randomAccessFile.writeBytes(System.getProperty("line.separator"));
}
randomAccessFile.writeBytes(stringBuffer.toString());
return randomAccessFile;
}
public static PhoneBook searchRecord(String phoneNumber , RandomAccessFile randomAccessFile) throws IOException {
PhoneBook phoneBook = null ;
//Setting file pointer to start of file
randomAccessFile.seek(0);
String data = randomAccessFile.readLine();
while (data != null ){
String[] recordToBeSearched = data.split(":");
String contactNo = recordToBeSearched[1];
if(contactNo != null && contactNo.equals(phoneNumber)){
phoneBook = new PhoneBook(recordToBeSearched[0],recordToBeSearched[1],recordToBeSearched[2]);
break ;
}
data = randomAccessFile.readLine();
}
return phoneBook;
}
public static void deleteRecord(String phoneNumber, RandomAccessFile randomAccessFile) throws IOException {
PhoneBook phoneBook = null ;
//Setting file pointer to start of file
randomAccessFile.seek(0);
String data = randomAccessFile.readLine();
while (data != null ){
String[] recordToBeSearched = data.split(":");
String contactNo = recordToBeSearched[1];
if(contactNo != null && contactNo.equals(phoneNumber)){
String blankData = new String();
randomAccessFile.writeBytes(blankData);
break ;
}
data = randomAccessFile.readLine();
}
}
public static void viewAll(RandomAccessFile randomAccessFile) throws IOException {
String data ;
//This is responsible for reading complete file
randomAccessFile.seek(0);
data = randomAccessFile.readLine();
while (data != null){
System.out.println(data);
data = randomAccessFile.readLine();
}
}
}
样本使用的数据文件包含以下数据。
The sample data file used has the following data.
推荐答案
此代码中有几个问题
- 您正在编写12345并在删除中寻找123456(简单的一个)
- 如果您查看writeBytes方法的作用,它会创建一个大小相等的字节数组到您编写的字符串的长度。在你的情况下,这是零,所以没有写入。所以你需要写的是一个字符串,其长度与要删除的字符数相同(将xxxxx视为删除字符串,如其他帖子所述)
附注:您在循环开始时将文件指针设置为零,这将在您第一次读取后更改,因此您需要跟踪文件指针。由于删除发生在char by char上,你需要找到记录的长度并构造'delete'字符串。
Side note: You are setting file pointer to zero in the beginning of the loop which will change after your first read, so you need to keep track of the file pointer. Since the delete happens on char by char, you need to find the length of the record and construct the 'delete' string.
Btw,如果你想真正摆脱它删除的字符,你最好读取缓冲区的文件,做所有的读取&写然后转储文件。只写白色空格字符(或空字符串)不会向前移动字符。
Btw, if you want to actually get rid of the deleted characters, you are better of reading the file of to a buffer, do all the read & write then dump the file back. Just writing white space chars (or empty string) wont move the characters forward.
这篇关于使用随机访问文件删除记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!