title: How to read files from local disk drives like D:// or E:// or C://
body: <p>I have the code below to find the List of files in a specified directory that have a particular word .</p>
  
  <ul>
  <li><strong>isWordPresent(word,filepath)</strong> method will give whether the word is contained in the path defined.</li>
  </ul>
  
  <p>The code works absolutely fine until we have some folders inside the local drives. </p>
  
  <p>Eg: String directoryName="<em>D://FOLDER1</em>"  </p>
  
  <p>I am not able to do the same , however, with local drives. All combinations of the following gace NullPointerException at <em>//Code line C</em> (as shown in the code snippet).</p>
  
  <pre><code> - String directoryName= "*D://*"  OR String Directorypath = "*D:/*"  
   - String directoryName= "*D:\\*"  OR String directoryName= "*D:\*" 
  </code></pre>
  
  <p>( "D:\" would need an escape character, however, I have tried all combinations )</p>
  
  <p>IMportantly, i tried <strong>replacing the <em>code line A</em> to:</strong></p>
  
  <pre><code>  `File[] roots = File.listRoots();                            //code line A
   if(Arrays.asList(roots).toString().contains(directoryName)){ //code line B`
  </code></pre>
  
  <p><em>where <strong>String directoryName = "C:\"</strong>  and accordingly closed brackets.</em> </p>
  
  <p>The above changes worked until <em>//Code line C</em> where it showed NullpointerException</p>
  
  <p><strong>Is there a way i can access the D Drive?</strong></p>
  
  <blockquote>
    <p></p>
  </blockquote>
  
  <pre><code> `public void listFilesHavingTheWord(String directoryName,String word)
   throws IOException{
               File directory = new File(directoryName);
               //get all the files from a directory
               File[] fList = directory.listFiles();    //code line A
                                                        //code line B
               for (File file : fList){                 //code line C
                   if (file.isFile()){
                      String filepath=file.getAbsolutePath();
                      if(isWordPresent(word,filepath)){
                          int index=file.getName().lastIndexOf(".");
                          if (index &gt; 0) {
                              String fileNameWithoutExt = file.getName().substring(0, index);
                              System.out.println("word \""+word+"\" present in file--&gt; "+fileNameWithoutExt);
                          }
                      }
                  } else if (file.isDirectory()){
                      listFilesHavingTheWord(file.getAbsolutePath(),word);
                   }
               }
           }`
  </code></pre>