gpt4 book ai didi

java - 使用随机访问文件删除记录

转载 作者:行者123 更新时间:2023-11-30 04:02:42 25 4
gpt4 key购买 nike

此问题与线程 Address Book Functionality 有关,该线程因过于宽泛而被关闭。

我正在使用随机访问文件开发一个小型测试应用程序。我附上了源代码。我想从文件中删除一条记录。我在 stackoverflow Delete Record Random Access File 上搜索了类似的线程。这些线程建议针对记录设置一个标记为已删除,而不是显示它。我想真正删除它。我尝试搜索记录并尝试将空记录附加到该文件指针位置。什么都没发生。请建议,正确的方法是什么?

我附上了下面的源代码。

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

}
}

使用的示例数据文件具有以下数据。

enter image description here

最佳答案

这段代码有几个问题

  1. 您正在写入 12345,并在删除中查找 123456(简单)
  2. 如果您查看 writeBytes 方法的作用,您会发现它会创建一个大小等于您写入的字符串长度的字节数组。在你的情况下为零,所以什么也没有写。因此,您需要编写一个字符串,其长度与要删除的字符数相同(将 xxxxx 视为其他帖子中提到的删除字符串)

旁注:您在循环开始时将文件指针设置为零,这将在第一次读取后发生变化,因此您需要跟踪文件指针。由于删除是逐字符进行的,因此您需要找到记录的长度并构造“删除”字符串。

顺便说一句,如果您想真正删除已删除的字符,最好将文件读取到缓冲区,完成所有读取和写入,然后将文件转储回来。仅写入空白字符(或空字符串)不会向前移动字符。

关于java - 使用随机访问文件删除记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21560941/

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