0

I can't seem to understand how to pass a folder to load files from in the classpath. It works with the text file right in the same folder as the .class file or if I use files/test.txt instead of test.txt. What am I doing wrong?

Code:

import java.io.*;

public class T {
    public static void main(String[] args) {
        String line;
        File f = new File("test.txt");
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(f));
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
            }
        }
    }
}

Folders and files:

stuff/T.java
stuff/T.class

Somewhere there is a files folder with the test.txt file which I want to give in the classpath.

I am running the test from the stuff folder in the command line in windows using the command java -cp .../files T.

Sunspawn
  • 817
  • 1
  • 12
  • 27
  • Relative path to text.txt is `files/test.txt` if you want it to be just `test.txt` place it together with class files. Adding files to classpath will be useful if there are some classes or libraries. – Filip Bulovic Jun 21 '15 at 19:08
  • And what if I have a jar file to which I want to add some new text files to use for various things? How do I give them to the jar for use? – Sunspawn Jun 21 '15 at 19:21
  • you will again have to use relative path [http://stackoverflow.com/questions/2393194/how-to-access-resources-in-jar-file – Filip Bulovic Jun 21 '15 at 20:02
  • Don't understand the answers in the link you gave. Care to elaborate? – Sunspawn Jun 21 '15 at 20:18
  • Sorry about delay. Similarly to class com.something.Test if there are subfolders involved they must be part of relative path. In resource docs [https://docs.oracle.com/javase/8/docs/technotes/guides/lang/resources.html ] are listed available options, text file is the same as resource. – Filip Bulovic Jun 21 '15 at 21:07
  • Sorry, don't get how it is related to my question. The goal is not to find the text file in a sub-folder - the goal is, from within a jar file, be able to access text files in a different resource folder. – Sunspawn Jun 21 '15 at 23:17

2 Answers2

0
String dirPath = "/Users/you/folder/";
String fileName = "test.txt";

File directory = new File(dirPath);
File file = new File(directory, fileName);

// Read file now

you can use .exists() on any file object to check if it exists or not.

Raman Shrivastava
  • 2,923
  • 15
  • 26
0

Check if the File is a directory then loop through the contents of the directory if necessary.

public class T {

    public static void main(String[] args) {
        File f = new File("stuff");

        if(f.isDirectory()){
            for(File file:f.listFiles()){
                printFileName(file);
            }
        }else{
            printFileName(f);
        }
    }

    private static void printFileName(File f) {
        String line;
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(f));
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
            }
        }
    }
}

If you are unsure of which directory the code is looking for the Files output the current directory.

File file = new File(".");
System.out.println(file.getAbsolutePath());
Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189