tags:

views:

45

answers:

2

I m trying to upload multiple files in swings applications.I have declared an array to hold the values of selected files but when i click on upload button only 1 file is getting uploaded. How can i upload all selected files into database?

The code to Open and Upload File is ....

 public void openFile() 
 {
      JFileChooser jfc = new JFileChooser();
      jfc.setMultiSelectionEnabled(true);// added line
      int result = jfc.showOpenDialog(this);
      if(result == JFileChooser.CANCEL_OPTION) return;
      try {
            ArrayList<String> FileData = new ArrayList<String>();
            File[] file = jfc.getSelectedFiles();
            String s=""; int c=0;
            for(int i=0;i<file.length;i++) //added
            {  
              jep.setText(file[i].toString()); // added
            }
            return FileData;
          } 
          catch (Exception e) 
          {
             JOptionPane.showMessageDialog(this,e.getMessage(),
            "File error",JOptionPane.ERROR_MESSAGE);
          }
   }
A: 
    // get list of selected files
    File[] file = jfc.getSelectedFiles();
    String s=""; int c=0;
    for(int i=0;i<file.length;i++) //added
    {  
      // The toString will just return you back the path of the file object times the number of bytes in the file.
      jep.setText(file[i].toString()); // added
    }
    return FileData;

This code will not work. If you want a method to read a file given a file name into an array of Strings then you will need:

   /**
    * Read entire contents of a text file.
    *
    * @param fileName Text file name
    * @return ArrayList of String (line) elements
    * @throws FileNotFoundException
    * @throws IOException
    */
   public static ArrayList readTextFile( String fileName )
      throws FileNotFoundException, IOException
   {
      ArrayList lines = new ArrayList();
      BufferedReader in = null;
      try
      {
         in = new BufferedReader( new FileReader( fileName ));
         String line;
         while ( ( line = in.readLine()) != null )
         {
            lines.add( line );
         }
      }
      finally
      {
         if ( in != null )
         {
            try
            {
               in.close();
            }
            catch ( IOException ex )
            {
            }
         }
      }
      return lines;
   }

Since you want to read in the contents of several file, just do this in a loop and do each one individually. If you load it all in memory you might run into problems.

Romain Hippeau
A: 

You haven't clearly defined what you actually mean by "upload multiple files". I'm presuming that you wish to load the contents of multiple text files one after the other into a single JEditorPane. If that is the case, Romain's answer shows you an example of loading a single file. What your code is actually doing though is looping through the array of File objects and setting their path (that is what toString returns on File) as the text within your editor pane. Each one is overriding the last, so that is why you end up with a single file path. I might also suggest when you pop up your JFileChooser you limit the files a user can select to specific file types (e.g. txt, html).

Avrom