我有一个 java 类项目,我必须在其中创建一个可以包含数据并使用数据执行操作的随机访问文件。但是,我遇到了一个问题:“删除”一段数据(将所有与其相关的行替换为“SPACE”)会将编码从 UTF-16 BE 切换为 LE。我哪里出错了,该如何解决?
我在编码方面非常缺乏经验,我所尝试的只是用小写字母留出空格并将单词更改为“spaced”
不幸的是,由于我缺乏经验,我的代码非常困惑,我不知道如何给出最小值。对于我将几乎整个文件粘贴到此处的事实,我表示歉意。
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
方法 writeChar
和 writeChars
并未声明它们写入 UTF-16,但它们实际上是这样做的,因为 Java 字符是以 UTF-16 格式。 writeChar
总是先写入高字节,相当于UTF-16BE。
来自Javadoc :
Writes a {@code char} to the file as a two-byte value, high byte first. The write starts at the current position of the file pointer.
(添加强调)
关于java - 在我运行代码到文件中的 "delete"数据后,随机访问文件似乎从 UTF-16 BE 切换到 UTF-16 LE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58161349/