#Locate and Read a specific file in a directory

3 messages · Page 1 of 1 (latest)

silent cloud
#

Line 20 (if (filename.matches()))is breaking the code cause i have been passing "name" which is a string it . Any suggestions please

import java.io.*;
import java.nio.file.Path;  
import com.opencsv.CSVReader;
import java.io.FileReader;  
import java.util.Arrays; 
import java.util.List;
 class CsvToArray {
    public static void main(String [] args){
      //Directory
      File directory = new File("C:\\Users\\person\\source\\Java\\");
     //store all files in the directory
      File[] containingFileNames = directory.listFiles();
      //check to make sure that the list is not empty
         if (containingFileNames != null && containingFileNames.length>0)
      {
         //now iterate through the files 
         for (File fileName : containingFileNames) 
         {
            
            if (fileName.matches()) 
            {
           //Use bufferedReader class to read the file 
            BufferedReader reader = new BufferedReader(new FileReader(fileName));
            System.out.println(reader);
            }
         }

      }
         
                                    
    }
       
   }

west wigeonBOT
#

This post has been reserved for your question.

Hey @silent cloud! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.