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

2019/3/21

read file always catch IOException

mrbrook mrbrook

2019/3/21

#
so im relatively new to code,particularly to this io problems, here is my code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import java.io.*;

/**
 * Write a description of class Score here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Score extends Actor
{   
int t = 0;
    int minute,second;
    int currentscore,highscore,savedscore;
    String TotalScore,csaved;
    boolean countstart = false;
    GreenfootImage score;
    List <Integer> scorelist = new ArrayList<Integer>();

private void readfile()
    {
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader("highscore.txt"));
            String r = br.readLine();

            while(r  != null)
            {
                scorelist.add(Integer.parseInt(r));
            }
        } catch (IOException ex) {
            ex.printStackTrace();}

        
    }
}
actually all im trying to do is just to read a file,and add each line to my existing list that named scorelist,but the problem is i dont know where to store the files because i just dropped my files on my project folders where this score.java located, is that any problem?or am i missed something from my code? any help will be appreciated thanks!
danpost danpost

2019/3/21

#
I do see one issue (right off) in that you only read one line. The while loop would be processing the same line infinitely (if one was read). As you have not stated the exact nature of your IOException, I cannot say if this was the cause.
mrbrook mrbrook

2019/3/21

#
oh thanks that must be my next issue lol, but for now I just can't get access from this file the error said : java.io.FileNotFoundException: highscore.txt (The system cannot find the file specified) at java.io.FileInputStream.open0(Native Method) at java.io.FileInputStream.open(FileInputStream.java:195) at java.io.FileInputStream.<init>(FileInputStream.java:138) at java.io.FileInputStream.<init>(FileInputStream.java:93) at java.io.FileReader.<init>(FileReader.java:58) at Score.readfile(Score.java:31) at Score.act(Score.java:110) at greenfoot.core.Simulation.actActor(Simulation.java:567) at greenfoot.core.Simulation.runOneLoop(Simulation.java:530) at greenfoot.core.Simulation.runContent(Simulation.java:193) at greenfoot.core.Simulation.run(Simulation.java:183)
danpost danpost

2019/3/21

#
Check the spelling, including upper/lower cases, of the filename/extension.
mrbrook mrbrook

2019/3/21

#
I'm sure with this case-sensitive issues, I am using greenfoot standalone version right now, is there something wrong with this version? i just put another copy of text file on greenfoot folder aswell
danpost danpost

2019/3/21

#
mrbrook wrote...
I'm sure with this case-sensitive issues, I am using greenfoot standalone version right now, is there something wrong with this version? i just put another copy of text file on greenfoot folder aswell
The version of greenfoot should not make a difference. The file placement should be as you have indicated (along with the .JAVA, .CLASS and .CTXT files). Rename the highscore.txt file to hs.txt and create a new text file at the same location, explicitly naming it highscore.txt. See if that helps.
mrbrook mrbrook

2019/3/21

#
I got it, it just the problem within filename, so I just need to rename the filename, actually it was basic mistakes xD u don't have to write your own file extension because greenfoot can read it automatically, i.e I changed my filename from "highscore.txt" to just "highscore" and save it to the folder that you pointed, thanks it helps! have a nice day
You need to login to post a reply.