Search code examples
javatypesbufferedreaderstreamreader

inputStreamReader won't recognise type JFileChooser


I have a variable, inFileName of type JFileChooser.

I've called this variable to the method HexFinder in class checksumFinder. It is to be used in the inputStreamReader inside a BufferedReader. (I'm using this line to call it)

cf.HexFinder(inFileName,null,null,null);

This is causing an error as the inputStreamReader will only accept variables of type String. (Here's my code for the BufferedReader)

BufferedReader reader = new BufferedReader(new InputStreamReader(this.getClass().getResourceAsStream(inFileName)));

Is there a way that I can get the inputStreamReader to read in inFileName? If not then how can I resolve this? Any help is much appreciated.


Solution

  • If you are trying to read a file chosen by a JFileChooser then you can do the following;

    File file = inFileName.getSelectedFile();
    BufferedReader reader = new BufferedReader(new FileReader(file));
    

    Note that FileReader uses the default character encoding. You can manually specify the encoding like this;

    String charset = "UTF-8";
    BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), charset));