This site requires JavaScript, please enable it in your browser!
Greenfoot back
Vesuv
Vesuv wrote ...

2018/10/9

Using I/O File in Jar ends with FileNotFoundException

Vesuv Vesuv

2018/10/9

#
Hi everybody, I tried to make a highscore System with different textfiles, which are just having the ending .hsc . In Greenfoot inself (inside the editor) the whole Project works and needs no adjustments, but when exporting the project to jar, it requires the files outside of the jar. (If added an additional directory it needs to be in the same folder not in the jar) I want that those files stay in the Project. Someone got a clue? Code:
/**
 * Write a description of class ScoreWriter here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
 
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class ScoreWriter  
{
    
    private static final File HSEASY = new File("highscoreE.hsc");
    private static final File HSMEDI = new File("highscoreM.hsc"); 
    private static final File HSHARD = new File("highscoreH.hsc");
    private static final File HSEASYENDL = new File("highscoreEE.hsc");
    private static final File HSMEDIENDL = new File("highscoreME.hsc");
    private static final File HSHARDENDL = new File("highscoreHE.hsc");
    private File file;
    private String[] entrys = new String[10];
    private FileReader reader;
    private FileWriter writer;
    
    public ScoreWriter(int fileNum) throws IOException{
        switch(fileNum){
        case 0: file = HSEASY;
        break;
        case 1: file = HSMEDI;
        break;
        case 2: file = HSHARD;
        break;
        case 3: file = HSEASYENDL;
        break;
        case 4: file = HSMEDIENDL;
        break;
        case 5: file = HSHARDENDL;
        break;
        
        }
        if (!file.exists()){
        createEmpty();
    }
    }
    
    private void createEmpty() throws IOException{
    writer = new FileWriter(file);
    for(int i = 0; i<=9    ;i++){
    writer.write("[ ],0-0x0201");
    writer.flush();
    }
    writer.close();
    }
    
    public Score[] readScore() throws IOException{
    
        reader = new FileReader(file);
        StringBuilder builder = new StringBuilder();
        int n = 0;
        
        for(n=0;n<file.length();n++){
            char c =((char)reader.read());
            builder.append(c);
            
        }
        entrys = builder.toString().split("-0x0201");
        
         
         
            Score[] scr = new Score[10];
            String sub[][] = new String[10][2];
            for(int i =0;i<10;i++){
                sub[i] = entrys[i].split(",");
                scr[i] = new Score(sub[i][0], Integer.valueOf(sub[i][1]));
            }
            
        return scr;
    }
    
    public void newEntry(String name, int score) throws IOException{
    String[][] subentrys = new String[10][2];
    reader = new FileReader(file);
    StringBuilder builder = new StringBuilder();
    int n,j,i;
    for(n = 0; n<file.length(); n++){
        char c =((char)reader.read());
        builder.append(c);
    }
        entrys = (builder.toString().split("-0x0201"));
        /**
         * Testing Splitter
         */
    for(n=0;n<10;n++){
            System.out.println(entrys[n]);
            }
    for (j=0; j < 10;j++){
        subentrys[j]=entrys[j].toString().split(",");
      }
        for (n=0;n<10;n++){
        //System.out.println(subentrys[n][1]+" <= " +score);
    if (Integer.valueOf(subentrys[n][1]) < score){
        for(i=8;i>=n;i--){
        System.out.println(n+"   |   "+j);
        System.out.println(i+1 +" = "+ i);
        entrys[i+1] = entrys[i];
        }
        //System.out.println(i+1 +" = "+ i +"outer!");
        //System.out.println(i);
        entrys[i+1] = (name +","+score);
        
        updateFile();
        break;
        }
    }
    }
    
    
    
    
    
    
    private void updateFile()throws IOException{
    writer = new FileWriter(file);
    for(int i = 0; i <= 9; i++){
        writer.write(entrys[i]+"-0x0201");
        writer.flush();
    }
    writer.close();
    }
}
The Error if you use additional folders: line 16:
private static final File HSEASY = new File("highscore/highscoreE.hsc");
java.io.FileNotFoundException: highscore\highscoreE.hsc (Das System kann den angegebenen Pfad nicht finden)
        at java.io.FileOutputStream.open0(Native Method)
        at java.io.FileOutputStream.open(Unknown Source)
        at java.io.FileOutputStream.<init>(Unknown Source)
        at java.io.FileOutputStream.<init>(Unknown Source)
        at java.io.FileWriter.<init>(Unknown Source)
        at ScoreWriter.createEmpty(ScoreWriter.java:49)
        at ScoreWriter.<init>(ScoreWriter.java:44)
        at HighScore.writeScore(HighScore.java:49)
        at HighScore.<init>(HighScore.java:42)
        at MainMenu.act(MainMenu.java:150)
        at greenfoot.core.Simulation.actWorld(Simulation.java:573)
        at greenfoot.core.Simulation.runOneLoop(Simulation.java:506)
        at greenfoot.core.Simulation.runContent(Simulation.java:193)
        at greenfoot.core.Simulation.run(Simulation.java:183)
java.lang.NullPointerException
        at HighScore.decrypt(HighScore.java:79)
        at HighScore.writeScore(HighScore.java:69)
        at HighScore.<init>(HighScore.java:42)
        at MainMenu.act(MainMenu.java:150)
        at greenfoot.core.Simulation.actWorld(Simulation.java:573)
        at greenfoot.core.Simulation.runOneLoop(Simulation.java:506)
        at greenfoot.core.Simulation.runContent(Simulation.java:193)
        at greenfoot.core.Simulation.run(Simulation.java:183)
Edit: java.io.FileNotFoundException: highscore\highscoreE.hsc (Das System kann den angegebenen Pfad nicht finden) means "The System cannot find the given path"
davmac davmac

2018/10/10

#
See: this page It talks about applets rather than standalone jars, but the solution is the same. (You cannot read a file inside a jar using java's File class, because a File refers to a file in the filesystem, not a file inside some other file).
davmac davmac

2018/10/10

#
Oh - and you won't be able to update the file using that method. If you really want to keep high scores persistent, I suggest looking into the Java Preferences API. There's a brief tutorial here.
You need to login to post a reply.