简体   繁体   English

如何读取位于项目文件夹java中的文件

[英]How to read a file that is located in the project folder java

Using eclipse, I have a file titled bad_words.txt in my project folder titled HackGSU.使用 Eclipse,我在名为 HackGSU 的项目文件夹中有一个名为 bad_words.txt 的文件。 I want locate the file and read the contents of the file.我想定位文件并读取文件的内容。 I saw this answer how to read text file relative path and I tried this:我看到了这个如何读取文本文件相对路径的答案,我试过这个:

private static String words[][] = new String[3][];
private static int ok = 17 + 2; //Add two for comment & empty line in text file
private static int med = 26 + 2; //Add two for comment & empty line in text file
private static int bad = 430 + 1; //Add one for comment in text file
private static String p = new File("").getAbsolutePath();

public static String[][] openFile() {

    p.concat("/HackGSU/bad_words.txt");

    //Set the a limit for the amount of words in each row
    words[0] = new String[getOk()];
    words[1] = new String[getMed()];
    words[2] = new String[getBad()];

    //Read the text file to add bad words to an array
    try {
        FileReader fr = new FileReader(p);
        //Wrap file
        BufferedReader br = new BufferedReader(fr);

        //Add each line of file into the words array
        for(int i = 0; i < words.length; i++) {             

            for(int j = 0; j < words[i].length; j++) {
                try {
                    words[i][j] = br.readLine();
                }
                catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
    catch (FileNotFoundException e1) {
        e1.printStackTrace();
    }

    return words;
}

But I received this traceback:但我收到了这个回溯:

java.io.FileNotFoundException: C:\Users\nbrow_000\workspaceProjects\HackGSU (Access is denied)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileReader.<init>(Unknown Source)
at gsu.hack.harassment.BadWords.openFile(BadWords.java:28)
at gsu.hack.harassment.HarassFilter.<clinit>(HarassFilter.java:10)
at gsu.hack.harassment.CheckPercentage.main(CheckPercentage.java:20)

Exception in thread "main" java.lang.NullPointerException
at gsu.hack.harassment.HarassFilter.checkHarass(HarassFilter.java:23)
at gsu.hack.harassment.CheckPercentage.main(CheckPercentage.java:20)

I also so this answer How to read a text file directly from Internet using Java?我也这样回答如何使用 Java 直接从 Internet 读取文本文件? , but I had a problem with the URL constructor. ,但我的 URL 构造函数有问题。

If I put the local file path (C://.../bad_words.txt) it works just fine, but how can I get the program to read the file so that if I package the software it will still find the proper file path.如果我把本地文件路径 (C://.../bad_words.txt) 它工作得很好,但我怎样才能让程序读取文件,以便如果我打包软件它仍然会找到正确的文件小路。

Looking at the error it doesn't look like you are getting the full path.查看错误,您似乎没有获得完整路径。 Instead of代替

p.concat("/HackGSU/bad_words.txt");

Try尝试

p = p.concat("/HackGSU/bad_words.txt");

Check out the answer in this question, it might be what you are looking for! 找出这个问题的答案,可能是您要寻找的!

How to read file from relative path in Java project? 如何从Java项目中的相对路径读取文件? java.io.File cannot find the path specified java.io.File找不到指定的路径

If your resource is already in the classpath, you don't need to make use of relative paths with the File class.如果您的资源已经在类路径中,则不需要对File类使用相对路径。 You can easily obtain it from the classpath like:您可以轻松地从类路径中获取它,例如:

java.net.URL url = getClass().getResource("bad_words.txt");
File file = new File(url.getPath());

or even directly to an inputstream:甚至直接到输入流:

InputStream in = getClass().getResourceAsStream("bad_words.txt");

Since you have a static method use:由于您有static方法,请使用:

InputStream in = YourClass.class.getResourceAsStream("bad_words.txt");

Furthermore, as I mentioned it already in my comment:此外,正如我在评论中已经提到的那样:

p.concat("/HackGSU/bad_words.txt");

does not concat to p but returns a new concatenated String, because Strings are immutable.不连接到p而是返回一个新的连接字符串,因为字符串是不可变的。 Simply use: p+="/HackGSU/bad_words.txt" instead只需使用: p+="/HackGSU/bad_words.txt"代替

You probably should use backslash instead of slash:您可能应该使用反斜杠而不是斜杠:

p.concat("\HackGSU\bad_words.txt");

You can also try to type double backslash:您也可以尝试键入双反斜杠:

p.concat("\\HackGSU\\bad_words.txt");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何读取位于eclipse中java项目文件夹中的bin文件 - How to read bin file located in java project's folder in eclipse 如何打开位于 java 中项目文件夹以外的文件夹中的文本文件(或任何文件) - how to open a text file(or any file) located in a folder other than the project folder in java Java:从位于项目文件夹下的文件加载数据失败 - Java: Loading data from a file located under project folder fails 如何读取位于JSP Web应用程序中项目文件夹之一中的图像 - How to Read an Image located in one of the Project Folder in JSP Web Application 如何在工作区外部的文件夹中的java类中读取属性文件? - How can I read properties file in java class located in a folder outside workspace? 如何从Java中的项目根文件夹读取具有相对路径的文件? - How to read file with relative path from project root folder in Java? 如何使用Java Netbeans读取项目文件夹外部的txt文件 - How to read in txt file outside of project folder with Java Netbeans 如何在创建文件后读取文件而不刷新Java中的项目文件夹 - How to read a file after creating it without refreshing the project folder in Java 如何从Java中的项目文件夹中读取图像? - how to read image from project folder in java? 如何连接到位于项目文件夹内的数据库 - How to connect to a database located inside project folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM