-4

I want to be able to go through a folder containing files and display the files that have been specified. I currently have it hard coded... Cc

public void searchResult(String a) throws IOException {

    FileReader inputFile;
    a = "C:\\IO\\Project.txt";
    try {
        inputFile = new FileReader(a);
        BufferedReader br = new BufferedReader(inputFile);

        while ((str = br.readLine()) != null) {
            searchResult.setText(str);
        }

    } catch (FileNotFoundException ex) {
        Logger.getLogger(SearchResults.class.getName()).log(Level.SEVERE, null, ex);
    }

}

Please, I need something more dynamic.

demongolem
  • 9,474
  • 36
  • 90
  • 105
mitch
  • 67
  • 2
  • 11
  • 3
    This is pretty broad/vague. What is the specific problem you're encountering? – tnw Jul 05 '17 at 16:17
  • Is `String a` supposed to contain multiple files? If so, does it have a specific format, such as a particular delimiter for separating the file names? – VGR Jul 05 '17 at 16:33

1 Answers1

1

i currently have it hard coded

Do you understand how passing parameters work?

public void searchResult(String a) throws IOException 
{
    a = "C:\\IO\\Project.txt";

    try {
        inputFile = new FileReader(a);

What is the point of hardcoding the value of "a". The point of using parameters is to pass the file name as a parameter to you method.

So the code should simply be:

public void searchResult(String a) throws IOException 
{
    try {
        inputFile = new FileReader(a);

Also the following makes no sense:

while ((str = br.readLine()) != null) {
    searchResult.setText(str);

Every time you read a new line of text you replace the previous line of text. You need to append(...) the text.

Or, the better solution is to just use the read(...) method of the JTextArea to load data from the file.

camickr
  • 321,443
  • 19
  • 166
  • 288