【问题标题】:How can I reach End Of File?我怎样才能到达文件结尾?
【发布时间】:2021-09-18 10:31:55
【问题描述】:

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

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

【问题讨论】:

  • 代码看起来很混乱。您似乎多次处理同一个文件 (location.txt)。如果您需要计算其中的行数(我不相信您这样做),您应该在Scanner 上调用hasNextLine 并计算它们。你能用简单的话描述一下你到底在做什么吗?
  • 我编辑了我的代码..我打电话给hasNextLine,所以我错误地打电话给hasNext。所以我正在做的是:我在(location.txt)文件中保存了一些字符的索引,这些字符取自在 SFile.txt 中找到的消息。然后在这些代码中我想从 SFile 中检索字符。 txt 将位置保存在 location.txt 中。 ...希望我能转移我的想法
  • 就我个人而言,我会将 SFile.txt 放入 List&lt;String&gt;,然后您可以使用正确的位置随机访问该文件。就位置而言,我会将位置保存为&lt;line number&gt;&lt;space&gt;&lt;line offset&gt;
  • lineNumb 被初始化为 1,然后递增。请记住,Java 使用从零开始的索引。
  • @Christopher 对.. 但是我在检索方面没有任何问题.. 我只是遇到了错误提示(索引 120 超出了长度 120 的范围)。

标签: java indexing location eof


【解决方案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();

【讨论】:

    猜你喜欢
    • 2015-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多