如何在项目中使用txt文件创建可运行的jar文件

时间:2015-07-15 09:12:58

标签: java jar

我想创建可运行的jar文件但是在我的小项目中我有TextIO类用于在文件和文件.txt中读写。

就像这样 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);

首先,我尝试使用TextIO而不是使用此阅读器。

1 个答案:

答案 0 :(得分:2)

我猜你的gameFile.txt文件在classpath中,所以你可以通过classloader访问它:

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

然后阅读内容......