Results 1 to 2 of 2
  1. #1
    gomdohri is offline Member
    Join Date
    Sep 2011
    Posts
    22
    Rep Power
    0

    Default How to change FileOutputStream to class.getResource()

    Java Code:
    public class HighScores {
    	
        //Inner class, Score. 
    	class Score implements Comparable<Score> {
    		String name;
    		int score;
    		
    		//Constructs scores from text file.
    		Score(StreamTokenizer stream) throws IOException {
    			if (stream.ttype != StreamTokenizer.TT_WORD) {
    				System.err.println("Expected highscore name value in highscores.txt not found. "+stream.lineno());
    				System.exit(1);
    			}
    			
    			name = stream.sval;
    			
    			stream.nextToken();
    			if (stream.ttype != StreamTokenizer.TT_NUMBER) {
    				System.err.println("Expected number in highscores.txt not found: "+stream.sval+", line: "+stream.lineno());
    				System.exit(1);
    			}
    			score = (int) stream.nval;
    			stream.nextToken();
    		}
    		
    		public Score(String n, int s) {
    			name = n;
    			score = s;
    		}
    
    		@Override
    		public String toString() {
    			return name+" "+score;
    		}
    
    		@Override
    		public int compareTo(Score o) {
    			return ((Integer) o.score).compareTo((Integer) score);
    		}
    	}
    	
    	ArrayList<Score> scores;
    	
    	public HighScores(String filename) {
    		
    		StreamTokenizer stream = null;
    		try {
    			stream = new StreamTokenizer(new BufferedReader(new InputStreamReader(new FileInputStream(filename))));
    		} catch (FileNotFoundException e1) {
    			System.err.println("File not found '"+filename+"'");
    			e1.printStackTrace();
    			System.exit(1);
    		}
    		
    		scores = new ArrayList<Score>();
    		try {
    			parse(stream);
    		} catch (IOException e) {
    			System.err.println("IO exception while reading file "+filename);
    			e.printStackTrace();
    			System.exit(1);
    		}
    	}
    	
    	//Parse the text of the highscore file.
    	void parse(StreamTokenizer stream) throws IOException {
    		stream.nextToken();
    		if (!(stream.ttype == StreamTokenizer.TT_WORD && stream.sval.equals("begin"))) {
    			System.err.println("Expected token \"begin\" not found: "+stream.sval+", line: "+stream.lineno());
    			System.exit(1);
    		}
    		stream.nextToken();
    		while (!(stream.ttype == StreamTokenizer.TT_WORD && stream.sval.equals("end"))) {
    			scores.add(new Score(stream));
    		}
    	}
    	
    
    	
    	@Override
    	public String toString() {
    		Collections.sort(scores);
    		String rtn = "";
    		for (Score s:scores)
    			rtn += "  " +s+ "\n";
    		return rtn;
    	}
    	
    	//Add a score to the highscore list if it is larger than any one of the old scores.
    	public void addScore(String name, int score) throws IOException{
    		
    		Score scoreToReplace = null;
    		Boolean hasScoreReplaceValue = false;
    		for (Score s : scores){
    			if (score > s.score && hasScoreReplaceValue == false){
    				hasScoreReplaceValue = true;
    			scoreToReplace = s;}
    			}
    		
    		if (scoreToReplace != null){
    		scores.add(scores.indexOf(scoreToReplace), new Score(name, score));
    		scores.remove(scoreToReplace);
    		
    		try {
    			FileOutputStream out = new FileOutputStream("src/Teeter/highscores.txt");
    			out.write("begin".getBytes());
    			for (Score s : scores){
    				out.write((" "+s.name+" "+s.score+" ").getBytes());
    			}
    			out.write("end".getBytes());
    			out.flush();
    			
    		} catch (FileNotFoundException e) {
    			System.err.println("Error. highscores.txt not found.");
    			e.printStackTrace();
    		}
    		}
    FileOutputStream out = new FileOutputStream("src/Teeter/highscores.txt");

    I know that in order to run this with jar file, I must change it to class.getResources() method but I just don't know to change this. What can be the good way to change this? Please... [when I compress this into jar file I must create the folder ('Teeter') and put jar file with src folder and then put highscores.txt into that src folder. I just want it to be compiled by just operating jar file.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: How to change FileOutputStream to class.getResource()

    The jar archive is meant for program resources - that is things that don't change and are read by the program. I don't think there is any standard way of the program changing the contents of the jar.

Similar Threads

  1. How to change class File
    By RajaKallam in forum New To Java
    Replies: 2
    Last Post: 03-16-2011, 02:39 PM
  2. Replies: 2
    Last Post: 02-16-2011, 11:35 AM
  3. FileOutputStream to GIF
    By dewitrydan in forum New To Java
    Replies: 12
    Last Post: 10-11-2010, 07:12 AM
  4. ClassLoader getResource Problem
    By sh4dyPT in forum New To Java
    Replies: 5
    Last Post: 04-01-2010, 08:47 PM
  5. change value in the in the creator class
    By livnihai in forum New To Java
    Replies: 8
    Last Post: 10-07-2009, 06:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •