如何到达文件结束?

How can I reach End Of File?

提问人:Asst. Farah 提问时间:9/18/2021 最后编辑:Asst. Farah 更新时间:9/25/2021 访问量:59

问:

我正在设计一个程序,用于保存消息中特定字符的索引位置。然后,我需要根据它们的索引位置检索这些字符。我将这些字符的位置保存在 .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();
 }

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

Java 索引 位置 EOF

评论

1赞 g00se 9/18/2021
代码似乎很混乱。您似乎多次处理同一个文件(位置 .txt)。如果你需要计算其中的行数(我不相信你这样做),你应该调用并计算它们。你能用简单的话描述一下你到底在做什么吗?hasNextLineScanner
0赞 Asst. Farah 9/18/2021
我编辑了我的代码..我打错了电话.所以我正在做的是:我在(location.txt)文件中保存了一些字符的索引,这些字符取自SFile.txt中的消息。然后在这些代码中,我想从 SFile.txt 中检索将位置保存在 location.txt 中的字符。...希望我能转移我的想法hasNextLinehasNext
0赞 g00se 9/18/2021
就我个人而言,我会将 SFile.txt 放入一个,然后您可以使用正确的位置随机访问该文件。就位置而言,我会将位置保存为List<String><line number><space><line offset>
0赞 Christopher 9/18/2021
lineNumb 初始化为 1,然后递增。请记住,Java 使用从零开始的索引。
0赞 Asst. Farah 9/18/2021
@Christopher对的..但我对检索没有任何问题。我刚刚遇到了一个错误,告诉(索引 120 超出长度 120 的界限)。

答:

0赞 Asst. Farah 9/25/2021 #1

这是解决方案......

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