简体   繁体   中英

How to create runnable jar file with txt file in project

I wanna to create runnable jar file but in my little project I have TextIO class for reading and writing in file and file .txt.

Its like this http://postimg.org/image/6lrcqnmil/

    String fileName = "src/***/******/*******/mini/game/gameFile.txt";
    String line = null;
    String s = "";

    try {
        FileReader fileReader = new FileReader(fileName);
        BufferedReader bufferedReader = new BufferedReader(fileReader);

        while((line = bufferedReader.readLine()) != null) {
            System.out.println(line);
            s+=line;
        }    
        bufferedReader.close();            
    }
    catch(FileNotFoundException ex) {
        System.out.println("Unable to open file '" + fileName + "'");                
    }

/**TextIO.readFile("**/****/*****/gameFile.txt");
        String s = "";
        while(!TextIO.eof()){
            s+=TextIO.getln();
            s+="\n";
        }**/

          String fileName = "src/***/******/*******/mini/game/gameFile.txt";


            String line = null;
            String s = "";

            try {
                FileReader fileReader = 
                    new FileReader(fileName);

                BufferedReader bufferedReader = 
                    new BufferedReader(fileReader);

                while((line = bufferedReader.readLine()) != null) {
                    System.out.println(line);
                    s+=line;
                }    


                bufferedReader.close();            
            }
            catch(FileNotFoundException ex) {
                System.out.println(
                    "Unable to open file '" + 
                    fileName + "'");                
            }

JOptionPane.showMessageDialog(null, s);

First I tried with TextIO than with this reader.

I guess your gameFile.txt file is within the classpath, so you can access it via the classloader:

InputStream is = TextIO.class.getResourceAsStream("/gameFile.txt");
BufferedReader in = new BufferedReader(new InputStreamReader(is));

Then read the content...

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