5 Replies Latest reply: Aug 19, 2009 1:41 PM by DrClap RSS

    Filereader only reads when launched from terminal

    843789
      My program consists of a JDialog which is launched from a JApplet. Part of my code requires that an input string be checked against a "dictionary" to see if it is a valid word. The dictionary consists of several word lists, organized by length, in a folder called "Data."

      Here is the relevant code:
           public boolean checkDictionary (String word) {
                boolean acceptable = false;
                try {
                     BufferedReader reader = new BufferedReader(new FileReader("Data/"+word.length()+"lw.txt"));
                     String stringRead = reader.readLine();
                     while(stringRead != null) {
                          stringRead = reader.readLine();  // read next line
                          if (stringRead.equals(word)) {
                               acceptable = true;
                               reader.close();
                          }
                     }
                      reader.close();
                }
                 catch (Exception ex) {
                }
                return acceptable;
           } 
      When I run the program from the terminal (either by typing "java Anagrams" or "firefox Launcher.html"), everything works fine, but if I run it by double-clicking the Launcher.html icon, it fails to read the data files. Everything else in the program works normally, and it doesn't make any difference whether I use a web browser, or appletviewer. Unfortunately, I can't tell what exception is thrown, because the output isn't sent to a terminal.

      Any ideas?
        • 1. Re: Filereader only reads when launched from terminal
          JosAH
          It doesn't answer you question but the logic of that method is incorrect: it never checks the first String in the file and it always reads all the words no matter whether or not the word was found already. Make that:
               public boolean checkDictionary (String word) {
                    boolean acceptable = false;
                     try {
                          BufferedReader reader = new BufferedReader(new FileReader("Data/"+word.length()+"lw.txt"));
                          String stringRead = reader.readLine();
                          for (String stringRead= reader.readLine(); stringRead != null; stringRead= reader.readLine()) {
                               if (stringRead.equals(word)) {
                                    acceptable = true;
                                    break;
                               }
                          }
                           reader.close();
                     }
                      catch (Exception ex) {
                     }
                     return acceptable;
                } 
          kind regards,

          Jos
          • 2. Re: Filereader only reads when launched from terminal
            843789
            quagga wrote:
            When I run the program from the terminal (either by typing "java Anagrams" or "firefox Launcher.html"), everything works fine, but if I run it by double-clicking the Launcher.html icon, it fails to read the data files. Everything else in the program works normally, and it doesn't make any difference whether I use a web browser, or appletviewer. Unfortunately, I can't tell what exception is thrown, because the output isn't sent to a terminal.

            Any ideas?
            Google ( [applet security restrictions|http://www.google.com/search?q=applet+security+restrictions] ).
            • 3. Re: Filereader only reads when launched from terminal
              843789
              Thank you both. I was expecting that there was something in my code that was wrong, and I hadn't thought in this vein.

              Presumably, what I'm trying to do is feasible so long as I can figure out how to configure the security settings, create a signature etc? If that's the case, I think I'll save these issues for later, because the main goal right now is getting the program to work on my computer.
              • 4. Re: Filereader only reads when launched from terminal
                843789
                Frequently in cases like this using Class.getResourceAsStream, rather than creating a FileReader, is the answer, although in this particular use case it might not be. Check it out.
                • 5. Re: Filereader only reads when launched from terminal
                  DrClap
                  quagga wrote:
                  Presumably, what I'm trying to do is feasible so long as I can figure out how to configure the security settings, create a signature etc?
                  Well, no. The first thing to do is to decide where this file is to be read from. If it's to be read from the server, then what paulcw said is what you should do. No need to sign the applet in this case. You'd only need to do that if you plan to read the file from the client's computer.