随机访问文件似乎在我运行代码以从文件中"delete"数据后从 UTF-16 BE 切换到 UTF-16 LE



我有一个Java类的项目,我必须制作一个可以包含数据的随机访问文件,并对数据做一些事情。 但是,我遇到了一个问题,即"删除"一段数据(将所有与其相关的行替换为"SPACE"(将编码从 UTF-16 BE 切换到 LE。 我哪里出错了,我该如何解决?

在编码方面,我非常缺乏经验,我所尝试的只是用小写字母留出空格并将单词更改为"空格">

不幸的是,由于我缺乏经验,我的代码非常混乱,我不知道如何给出最小值。 对于我在这里粘贴几乎整个文件的事实,我深表歉意。

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
public class CS1181Project01Wheeler {
public static void main(String[] args) {
//main RAF where data is stored
RandomAccessFile data=null;
try {
//creation of file/loading file
data=new RandomAccessFile("data.dat", "rw");
} catch (FileNotFoundException ex) {
Logger.getLogger(CS1181Project01Wheeler.class.getName()).log(Level.SEVERE, null, ex);
System.out.println("this shouldn't be possible");
}
//scanner to scan user input
Scanner userInput=new Scanner(System.in);
boolean finished=false;
System.out.println("Welcome to Project01 random access file reader.");
//the "main menu"/user interface
while(!finished){
System.out.println("Please select what you want to do:");
System.out.println("1. add a film");
System.out.println("2. remove a film");
System.out.println("3. find a film");
System.out.println("4. get stats on all films in this list");
System.out.println("5. about");
System.out.println("6. quit");
int choice=userInput.nextInt();
userInput.nextLine();
//adding a film
if(choice==1){
System.out.println("please enter the title of the film you would like to add");
String title=userInput.nextLine();
String space=null;
boolean done=false;
while(!done){
try {
space=data.readLine();
if(space=="SPACE"){
data.writeChars(title+"n");
done=true;
}
if(data.getFilePointer()>=data.length()){
data.seek(data.length());
data.writeChars(title+"n");
done=true;
}
} catch (IOException ex) {
Logger.getLogger(CS1181Project01Wheeler.class.getName()).log(Level.SEVERE, null, ex);
break;
}
}
System.out.println("How long is the film in minutes?");
int length=userInput.nextInt();
userInput.nextLine();
//the code for adding the line repeats itself for each question related to the film
done=false;
while(!done){
try {
space=data.readLine();
if(space=="SPACE"){
data.writeChars(length+"n");
done=true;
}
if(data.getFilePointer()>=data.length()){
data.seek(data.length());
data.writeChars(length+"n");
done=true;
}
} catch (IOException ex) {
Logger.getLogger(CS1181Project01Wheeler.class.getName()).log(Level.SEVERE, null, ex);
break;
}
}     
System.out.println("Who is the biggest actor in the film?");
String actor=userInput.nextLine();
done=false;
while(!done){
try {
space=data.readLine();
if(space=="SPACE"){
data.writeChars(actor+"n");
done=true;
}
if(data.getFilePointer()>=data.length()){
data.seek(data.length());
data.writeChars(actor+"n");
done=true;
}
} catch (IOException ex) {
Logger.getLogger(CS1181Project01Wheeler.class.getName()).log(Level.SEVERE, null, ex);
break;
}
} 
System.out.println("What year did it release in?");
int year=userInput.nextInt();
userInput.nextLine();
done=false;
while(!done){
try {
space=data.readLine();
if(space=="SPACE"){
data.writeChars(year+"n");
done=true;
}
if(data.getFilePointer()>=data.length()){
data.seek(data.length());
data.writeChars(year+"n");
done=true;
}
} catch (IOException ex) {
Logger.getLogger(CS1181Project01Wheeler.class.getName()).log(Level.SEVERE, null, ex);
break;
}
}
System.out.println("What is the height of the biggest actor in cm?");
int height=userInput.nextInt();
userInput.nextLine();
done=false;
while(!done){
try {
space=data.readLine();
if(space=="SPACE"){
data.writeChars(height+"n");
done=true;
}
if(data.getFilePointer()>=data.length()){
data.seek(data.length());
data.writeChars(height+"n");
done=true;
}
} catch (IOException ex) {
Logger.getLogger(CS1181Project01Wheeler.class.getName()).log(Level.SEVERE, null, ex);
break;
}
}
System.out.println("how was the film received (poor/mixed/good)?");
done=false;
String reception = null;
while(!done){
reception=userInput.nextLine();
if("poor".equals(reception) || "mixed".equals(reception) || "good".equals(reception)){
done=true;
}
else{
System.out.println("Please enter poor, mixed, or good for the reception");
}
}
done=false;
while(!done){
try {
space=data.readLine();
if(space=="SPACE"){
data.writeChars(reception+"n");
done=true;
}
if(data.getFilePointer()>=data.length()){
data.seek(data.length());
data.writeChars(reception+"n");
done=true;
}
} catch (IOException ex) {
Logger.getLogger(CS1181Project01Wheeler.class.getName()).log(Level.SEVERE, null, ex);
break;
}
}        
System.out.println("film added");
}
//removing a film
if(choice==2){
System.out.println("What's the name of the film you want deleted?");
String title=userInput.nextLine();
try {
data.seek(0);
} catch (IOException ex) {
Logger.getLogger(CS1181Project01Wheeler.class.getName()).log(Level.SEVERE, null, ex);
}
boolean answer=false;
int titleAmount=0;
long backtrack=0;
char test=0;
//goes through the whole file to find where the film is located
while(!answer){
try {
System.out.println(data.getFilePointer());
test=data.readChar();
} catch (IOException ex) {
System.out.println("film does not exist");
break;
}
//what the code does if a letter matches the film's title
if(test==title.charAt(titleAmount)){
if(titleAmount==0){
try {
backtrack=data.getFilePointer()-1;
} catch (IOException ex) {
Logger.getLogger(CS1181Project01Wheeler.class.getName()).log(Level.SEVERE, null, ex);
}
}
titleAmount+=1;
if(titleAmount==title.length()){
answer=true;
System.out.println("move found");
}
}
else{
backtrack=0;
titleAmount=0;
}
}
try {
data.seek(backtrack);
} catch (IOException ex) {
Logger.getLogger(CS1181Project01Wheeler.class.getName()).log(Level.SEVERE, null, ex);
}
for(int i=0; i<6; i++){
try {
System.out.println(data.getFilePointer());
} catch (IOException ex) {
Logger.getLogger(CS1181Project01Wheeler.class.getName()).log(Level.SEVERE, null, ex);
}
try {
data.writeChars("spaced"+"n");
} catch (IOException ex) {
Logger.getLogger(CS1181Project01Wheeler.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
RandomAccessFile

方法writeCharwriteChars并没有声称它们编写UTF-16,但它们实际上确实如此,因为Java字符是UTF-16。writeChar总是先写入高字节,相当于UTF-16BE。

来自Javadoc:

将 {@code 字符} 作为双字节值写入文件,高 字节优先。写入从 文件指针。

(着重号后加(

最新更新