i cant get the file with the file path

i was trying to read a text file in the resources file(i was using maven) but it wont work with relative path, im using filereader
9 Replies
JavaBot
JavaBot7mo ago
This post has been reserved for your question.
Hey @coolsigma1003! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Dexter
Dexter7mo ago
the code:
public class FileUtils {
public static String readAsString(String filePath) {
StringBuilder result = new StringBuilder();

try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = reader.readLine()) != null) {
result.append(line).append('\n');
}
} catch (IOException e) {
e.printStackTrace(); // Handle exceptions appropriately
}
return result.toString();
}
public static Object[] readEachLine(String filePath) {
ArrayList<String> result = new ArrayList<String>();
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = reader.readLine()) != null) {
result.add(line);
}
} catch (IOException e) {
e.printStackTrace();
}
return result.toArray();
}
}
public class FileUtils {
public static String readAsString(String filePath) {
StringBuilder result = new StringBuilder();

try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = reader.readLine()) != null) {
result.append(line).append('\n');
}
} catch (IOException e) {
e.printStackTrace(); // Handle exceptions appropriately
}
return result.toString();
}
public static Object[] readEachLine(String filePath) {
ArrayList<String> result = new ArrayList<String>();
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = reader.readLine()) != null) {
result.add(line);
}
} catch (IOException e) {
e.printStackTrace();
}
return result.toArray();
}
}
This message has been formatted automatically. You can disable this using /preferences.
coolsigma103
coolsigma103OP7mo ago
work❤️ :
FileUtils.readAsString("D:\\Bao\\eclipse-Workspace\\103GE-CORE\\src\\main\\resources\\shaders\\FragmentShader.fs")
FileUtils.readAsString("D:\\Bao\\eclipse-Workspace\\103GE-CORE\\src\\main\\resources\\shaders\\FragmentShader.fs")
hell nah💔:
FileUtils.readAsString("\\src\\main\\resources\\shaders\\FragmentShader.fs")
FileUtils.readAsString("\\src\\main\\resources\\shaders\\FragmentShader.fs")
dan1st
dan1st7mo ago
Resources are not files you can use getClass().getClassLoader().getResource("shaders/FragmentShader.fs") or similar accessing resources as files does not work when the application is packaged (e.g. in a JAR file) see https://stackoverflow.com/q/20389255/10871900 and also https://stackoverflow.com/q/15749192/10871900
JavaBot
JavaBot7mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
coolsigma103
coolsigma103OP7mo ago
thank you it work
JavaBot
JavaBot7mo ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
coolsigma103
coolsigma103OP7mo ago
❤️
JavaBot
JavaBot7mo ago
Post Closed
This post has been closed by <@1352291760360914964>.

Did you find this page helpful?