Home > Back-end >  read text from the current directory in java
read text from the current directory in java

Time:08-20

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.

like this

Path fileName = Path.of("E:\\java\\program\\src\\textfile.txt");

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?

The structure of the folder is like this

E:
----java
-----------program
-------------------src
------------------------textfile.txt
------------------------App.java

Both App.java and the text file are in the same folder

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 = "";
        for (int i = 0; i < word.length(); i  )
            if (word.charAt(i) != ' ') {
                stars  = '#';
            } else {
                stars  = ' ';
            }
        text = text.replaceAll(word, stars);

        File path = new File("E:\\java\\program\\src\\newtextfile.txt");
        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("E:\\java\\program\\src\\textfile.txt");
        String extract = Files.readString(fileName);
        String cen = "consectetur adipiscing elit";
        System.out.println(censor(extract, cen));
    }
}

CodePudding user response:

When you run an application, you are not running the App.java file directly. That file is compiled into an App.class file, and Java then runs your App.class file. The class file is probably created in a different directory, and when you run your application it doesn't know which directory the original App.java file was in (or even if it still exists or not).

What you can do is use the concept of current directory. If you just try to open the file using only its name (e.g. new File("textfile.txt")) it will look for that file in the current directory. You can tell your IDE which directory to run the application in. If you run the application from the command line, then you can cd to the correct directory before you run the application.

To be confirm which directory it's looking in, you can do this:

Path path = Path.of("textfile.txt").toAbsolutePath();
System.out.println("Trying to open file "   path);

CodePudding user response:

Since both the file and the app jar file will be in the same directory you can use System.getProperty("user.dir") to determine the current working directory of the app. In your case the name of the current working directory would be program.

You only want the filename to be set explicitly i assume you mean setting the file name with a java argument. The code below shows you how you can make a Path object using the current working directory as well as a user defined file name.

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 = "";
        for (int i = 0; i < word.length(); i  )
            if (word.charAt(i) != ' ') {
                stars  = '#';
            } else {
                stars  = ' ';
            }
        text = text.replaceAll(word, stars);

        File path = Path.of(System.getProperty("user.dir"), "newtextfile.txt").toFile();
        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(System.getProperty("user.dir"), args[0]);
        String extract = Files.readString(fileName);
        String cen = "consectetur adipiscing elit";
        System.out.println(censor(extract, cen));
    }
}

You would run your app like this: java -jar App.jar filename.txt

EDIT: Make sure your file is in the directory program as well and not in a subdirectory of program.

CodePudding user response:

You can simply substitute your file path with the file name in the consor method. In your main method you may use Path.get() for the existing file. Your code should look like this then:

import java.io.*;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths
import java.io.FileWriter;

class App{
    static String censor(String text,String word) throws IOException {
        String stars = "";
        for (int i = 0; i < word.length(); i  )
            if (word.charAt(i) != ' ') {
                stars  = '#';
            } else {
                stars  = ' ';
            }
        text = text.replaceAll(word, stars);

        File path = new File("newtextfile.txt");
        FileWriter wr = new FileWriter(path);
        wr.write(text);
        wr.flush();
        wr.close();
        return text;
    }

    public static void main(String[] args) throws IOException {
        Path fileName = Paths.get("textfile.txt");
        String extract = Files.readString(fileName);
        String cen = "consectetur adipiscing elit";
        System.out.println(censor(extract, cen));
    }
}
  •  Tags:  
  • java
  • Related