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

问题描述 投票:1回答:1

我有一个针对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);
                    }
                }
            }
java utf-16 randomaccessfile
1个回答
0
投票

RandomAccessFile方法writeCharwriteChars并未声明它们编写UTF-16,但是由于Java字符位于UTF-16中,因此它们确实有效。writeChar总是首先写入高字节,等效于UTF-16BE

Javadoc

将{@code char}作为两个字节的值写入文件,高字节优先。写操作从当前位置开始文件指针。

((已添加重点)

© www.soinside.com 2019 - 2024. All rights reserved.