title: read text from the current directory in java
body: <p>i have written a program that reads contents from a text file. the program will check the contents of the text for a particular phrase is in it or not. if yes it will replace the phrase in the content with #.  The program works fine, but I am reading the file by writing the complete path explicitly.</p>
  <p>like this</p>
  <pre><code>Path fileName = Path.of(&quot;E:\\java\\program\\src\\textfile.txt&quot;);
  </code></pre>
  <p>I want to change that. I need to read the file from the directory without writing the complete path explicitly. just the name of the text file. since they are in the same folder. how can i do that?</p>
  <p>The structure of the folder is like this</p>
  <pre><code>E:
  ----java
  -----------program
  -------------------src
  ------------------------textfile.txt
  ------------------------App.java
  </code></pre>
  <p>Both App.java and the text file are in the same folder</p>
  <pre><code>import java.io.*;
  import java.io.IOException;
  import java.nio.file.Files;
  import java.nio.file.Path;
  import java.io.FileWriter;
  
  class App{
      static String censor(String text,String word) throws IOException {
          String stars = &quot;&quot;;
          for (int i = 0; i &lt; word.length(); i++)
              if (word.charAt(i) != ' ') {
                  stars += '#';
              } else {
                  stars += ' ';
              }
          text = text.replaceAll(word, stars);
  
          File path = new File(&quot;E:\\java\\program\\src\\newtextfile.txt&quot;);
          FileWriter wr = new FileWriter(path);
          wr.write(text);
          wr.flush();
          wr.close();
          return text;
      }
  
      public static void main(String[] args) throws IOException {
          Path fileName = Path.of(&quot;E:\\java\\program\\src\\textfile.txt&quot;);
          String extract = Files.readString(fileName);
          String cen = &quot;consectetur adipiscing elit&quot;;
          System.out.println(censor(extract, cen));
      }
  }
  </code></pre>