我怎样才能到达文件结尾?

How can I reach End Of File?

我正在设计一个程序,用于保存消息中特定字符的索引位置。然后,我需要根据索引位置检索这些字符。我将这些字符的位置保存在一个 .txt 文件中。我检索了它们,但最后,我收到了这条消息“线程“AWT-EventQueue-0”java.lang.IndexOutOfBoundsException 中的异常:长度 120 的索引 120 超出范围”。 我的代码:

int n;
String s; 
int lineNumCount = 0;
String coverText = stegoMsg.getText(); // get the stego text from the textfield
int k = coverText.length(); // get the length for the stego text
int lineNumb = 1;
Scanner myFile = null;
          try{
               Scanner file = new Scanner(new File("location.txt"));//location.txt is the file that has the locations for the characters
               myFile = file;
             }catch (FileNotFoundException e){
                 System.out.println("File does not found");
             }
        while (myFile.hasNextLine()){
            //Count the Number of the lines in location.txt
            //1. Read the File
            File fileLocation = new File("location.txt"); 
            if(fileLocation.exists()){
                try {
                    FileReader fr = new FileReader(fileLocation);
                    LineNumberReader lr = new LineNumberReader(fr); //2. Read the lines for location.txt
                    
                    while((lr.readLine()) !=null){
                    lineNumCount++;
                            }
                   // System.out.println("Total Number of the Lines " + lineNumCount);
                } catch (FileNotFoundException ex) {
                    Logger.getLogger(ExtPage.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    Logger.getLogger(ExtPage.class.getName()).log(Level.SEVERE, null, ex);
                }
            
           for(int x = 0; x<lineNumCount; ++x){
            try{
                String line = Files.readAllLines(Paths.get("location.txt")).get(lineNumb);
               // System.out.println("Line First " + line);
                BufferedReader bufrd = new BufferedReader(new FileReader("SFile.txt")); //SFile.txt is the file that has the messsage that I need to take the location for the specific characters
                int nn = Integer.parseInt(line);  
                s = bufrd.readLine();
                System.out.println("The Location " + nn  + " is : "+ s.charAt(nn)); // read the character that has this location
                lineNumb++;
                
                }catch(IOException e){
                    System.out.println("Line 334");
                }
           }
            }
        }
        myFile.close();
 }

是否可以指导我如何解决异常? 感谢您提供的任何帮助。

这是解决方案...

        int n;
        String s; 
        int lineNumCount = 0;
        String coverText = stegoMsg.getText(); // get the stego text from the textfield
        int k = coverText.length(); // get the length for the stego text
        int lineNumb = 1;
        Scanner myFile = null;
        try{
        Scanner file = new Scanner(new File("location.txt"));
        myFile = file;
        }catch (FileNotFoundException e){
            System.out.println("File does not found");
        }
       try{
        while (myFile.hasNext()){
            //Count the Number of the lines in location.txt
            //1. Read the File
            File fileLocation = new File("C:\Users\Farah\Dropbox\Steganography codes\NewStegoTech\location.txt");
            if(fileLocation.exists()){
                try {
                    FileReader fr = new FileReader(fileLocation);
                    LineNumberReader lr = new LineNumberReader(fr); //2. Read the lines for location.txt
                    
                    while((lr.readLine()) !=null){
                    lineNumCount++;
                            }
                } catch (FileNotFoundException ex) {
                    Logger.getLogger(ExtPage.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    Logger.getLogger(ExtPage.class.getName()).log(Level.SEVERE, null, ex);
                }
            
          try{
                String line = Files.readAllLines(Paths.get("location.txt")).get(lineNumb);
               // System.out.println("Line First " + line);
                BufferedReader bufrd = new BufferedReader(new FileReader("Stego File.txt"));
                int nn = Integer.parseInt(line);  
                s = bufrd.readLine();
                System.out.println("The Loocation " + nn  + " is : "+ s.charAt(nn)); // read the character that has this location
                lineNumb++;
                }catch(IOException e){
                    System.out.println(e);
                }
           
            }
        }
       }catch(IndexOutOfBoundsException e)
       {
           System.out.println("Finish reading file");
       }
       
        myFile.close();