1

I have this project for a java class where I have to make a random access file that can contain data and do things with the data. However, I've ran into a problem where "deleting" a piece of data (replacing all the lines related to it to "SPACE") switches the encoding from UTF-16 BE to LE. Where did I go wrong and how can I solve it?

I'm pretty inexperienced when it comes to coding, and all I've tried was making space in lowercase and changing the word to "spaced"

Unfortunately, due to my inexperience, my code is such a mess that I have no idea how to give a minimal. I apologize for the fact that I'm pasting almost the whole file here.

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

1 Answers1

0

RandomAccessFile methods writeChar and writeChars don't make a claim that they write UTF-16, but they effectively do since Java chars are in UTF-16. writeChar always writes the high-byte first, which is equivalent to UTF-16BE.

From the 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.

(emphasis added)

Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79