Jump to content
Frequently Asked Questions
  • Are you not able to open the client? Try following our getting started guide
  • Still not working? Try downloading and running JarFix
  • Help! My bot doesn't do anything! Enable fresh start in client settings and restart the client
  • How to purchase with PayPal/OSRS/Crypto gold? You can purchase vouchers from other users
  • How to access .txt file in the Project folder or src?


    althtu

    Recommended Posts

    When I'm just compiling and building a project in IntelliJ I can add a text file to my Project and then access it at the following path

    Path inputDataPath = Paths.get("src","ItemIds.txt");
    System.out.println(inputDataPath.toString());

    But when I try accessing at that path for my bot script (which also has the file) I end up with a FileNotFoundException.

    java.io.FileNotFoundException: src\ItemIds.txt (The system cannot find the path specified)

    So, my question is - How can I add and access a data file from my bot scripts?

    Link to comment
    Share on other sites

    Just to clarify, I just want the path to the data file. I'm thinking it could be the following

    Paths.get(System.getProperty("user.dir"),"Scripts","MyScriptName","ItemIds.txt")

    which assumes you can get into MyScriptName.jar by Scripts/MyScriptName/ItemIds.txt.

    Link to comment
    Share on other sites

    After a bit of looking around, I found a good answer on StackOverflow.

    You can add the file to your project as normal but because its being built as a jar file you can't just read it like a regular text file. Based on Drew MacInnis' post on Stackoverflow, I was able to read the .txt file as follows:

        public List<Integer> readJarResource(String filename) {
            List<Integer> itemIds = new ArrayList<>();
    
            try (InputStream in = getClass().getResourceAsStream(filename);
                 BufferedReader reader = new BufferedReader(new InputStreamReader(in))) {
                while (reader.ready()) {
                    String data = reader.readLine();
                    itemIds.add(Integer.parseInt(data));
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println(itemIds);
            return itemIds;
        }

    Happy days.

    Link to comment
    Share on other sites

    Archived

    This topic is now archived and is closed to further replies.

    ×
    ×
    • Create New...

    Important Information

    We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.