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.
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.