parsing files in applets

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • janani
    New Member
    • Jun 2006
    • 9

    #1

    parsing files in applets

    how to upload a xls or txt file and parse the same using java applets???
  • D_C
    Contributor
    • Jun 2006
    • 293

    #2
    Careful. For security reasons, Java applets don't let you read files.

    It is possible, but there are some hoops that need to be jumped through. I ran into this problem during the end of the semester of a group project. We did not have the time to figure it out.

    I have some old code from that project I can post here. If you can figure it out, great. This reads a text file into a String array, each line of text getting it's own entry.

    Code:
       public String[] readFile(String fileName)
       {
          String file = "";
          String temp;
    
          String[] lines = new String[1];
          String[] moreLines = new String[0];
          int INDEX = 0;
    
          try
          {
             FileReader fr = new FileReader(fileName);
             BufferedReader br = new BufferedReader(fr);
             do
             {
                temp = br.readLine();
                if(temp == null)
                   break;
                else
                {
                   if(INDEX == lines.length)  // add more entries, could make a subroutine but
                   {                          // this should be the only code that needs this.
                      moreLines = new String[1+(int)(lines.length*1.618)];
                      System.arraycopy(lines,0,moreLines,0,lines.length);
                      lines = moreLines;
                   }
                   lines[INDEX++] = temp;
                }
             } while(true); // do
          }
    
          catch (FileNotFoundException fnfe)
          {
             System.err.println("The file " + fileName + " was not found.");
          }
    
          catch (IOException ioe)
          {
             System.err.println("There is an error in the file.");
             // file.length() should be 0
          }
    
          catch (Exception e)
          {
             System.out.println("An unknown error occurred.");
             System.out.println(e);
          }
    
          moreLines = new String[INDEX];
          System.arraycopy(lines,0,moreLines,0,INDEX);
          return moreLines;
       }

    Comment

    Working...