0

Guys i got a problem about using fileChooser and using File reader same time at Java.I need a help. Copy txt files to array word by word (each word will stay diffrent array index number) with using fileChooser.

Alevalbay
  • 3
  • 1
  • if you can't think both at same time, one way is that you can just get the directory of the file with JFileChooser an then read that file with any approach you want. – Amir Hedieh Dec 01 '18 at 12:41

1 Answers1

0

Write this inside the actionPerformed method :

    final JFileChooser fc = new JFileChooser("E://");
    int returnVal = fc.showOpenDialog(this);
    System.out.println(returnVal);

    if (returnVal == JFileChooser.APPROVE_OPTION) 
    {
        File file = fc.getSelectedFile();
        String p = file.getPath();
        try(BufferedReader bufRead = new BufferedReader(new FileReader(p)))
    {


        StringBuilder sb = new StringBuilder();
        String s = "";
        while((s=bufRead.readLine())!=null)
        {
            sb.append(s+" ");
        }
        String[] words= sb.toString().split(" ");
        for(String a:words)
        {
            System.out.println(a);// printing out each word
        }


    }
    catch(FileNotFoundException e)
    {
        System.out.println("File not found : "+e.getMessage());
    }
    catch(IOException ex)
    {
        System.out.println("Exception : "+ex.getMessage());
    }
    } 
    else 
    {
        System.out.println("Open command cancelled by user.");
    }

Here I am printing out each word. You can do whatever with the words stored in the array. I hope this helps.

arunken
  • 415
  • 3
  • 15