Question

I'm very new at coding java and I'm having a lot of difficulty. I'm suppose to write a program using bufferedreader that reads from a file, that I have already created named "scores.txt". So I have a method named processFile that is suppose to set up the BufferedReader and loop through the file, reading each score. Then, I need to convert the score to an integer, add them up, and display the calculated mean.

I have no idea how to add them up and calculate the mean, but I'm currently working on reading from the file. It keeps saying that it can't fine the file, but I know for sure that I have a file in my documents named "scores.txt".

This is what I have so far...it's pretty bad. I'm just not so good at this :( Maybe there's is a different problem?

public static void main(String[] args) throws IOException,
        FileNotFoundException {

String file = "scores.txt";
processFile("scores.txt");
//calls method processFile
}


public static void processFile (String file)
throws IOException, FileNotFoundException{
String line;
//lines is declared as a string


   BufferedReader inputReader =
           new BufferedReader (new InputStreamReader
    (new FileInputStream(file)));

   while  (( line = inputReader.readLine()) != null){
   System.out.println(line);

  }    
  inputReader.close();
   }
Was it helpful?

Solution

There are two main options available

  1. Use absolute path to file (begins from drive letter in Windows or slash in *.nix). It is very convenient for "just for test" tasks.

    Sample Windows - D:/someFolder/scores.txt, *.nix - /someFolder/scores.txt

  2. Put file to project root directory, in such case it will be visible to class loader.

OTHER TIPS

Place the scores.txt in the root of your project folder, or put the full path to the file in String file.

The program won't know to check your My Documents folder for scores.txt

If you are using IntelliJ, create an input.txt file in your package and right click the input.txt file and click copy path. You can now use that path as an input parameter.

Example:

in = new FileInputStream("C:\\Users\\mda21185\\IdeaProjects\\TutorialsPointJava\\src\\com\\tutorialspoint\\java\\input.txt");

Take the absolute path from the local system if you'r in eclipse then right-click on the file and click on properties you will get the path copy it and put as below this worked for me In maven project keep the properties file in src/main/resources `

private static Properties properties = new Properties();

    public Properties simpleload() {
        
        String filepath="C:/Users/shashi_kailash/OneDrive/L3/JAVA/TZA/NewAccount/AccountConnector/AccountConnector-DEfgvf/src/main/resources/sample.properties";
        
        try(FileInputStream fis = new FileInputStream(filepath);) {
            //lastModi = propFl.lastModified();
            properties.load(fis);           
        } catch (Exception e) {
            System.out.println("Error loading the properties file : sample.properties");
            e.printStackTrace();
        }
        return properties;
    }`
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top