简体   繁体   中英

Java - Read the file within the package from another package

I have a Java project with the package com.myproject.schema where I have the test.txt file.
To read the content of that file from the package com.myproject.imp.tasks (from which I want to read the file) I'm trying the example below using the ClassLoader approach.

InputStream is = getClass().getResourceAsStream("/com/myproject/schema/test.txt");

However, getting null as a result.

Could you please advise some alternative way or how to fix this?

Many thanks to all for your replies.
I could solve the issue. The getResourceAsStream() was looking for the text file inside the directory where the java classes are compiled and stored, however, the file was kept only in the non-compiled package, so that's why was not possible to find.Copying the file properly, the issue has gone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM