Search code examples
javatextbufferedreader

Error at openFileInput()


I'm trying to read a text file. I've got the code so far, using BufferReader. But where I use openFileInput("Storyline.txt"), it gives an error: The method openFileInput(String) is undefined for the type Game. Now, I read something about Activity, here: Android File I/O openFileInput() undefined. But I must admit I don't quite understand it.

Here's the code I wrote:

package game;

import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.*;

public class Game {

public static void Start(){
    InputStream instream = openFileInput("Storyline.txt");
    InputStreamReader inputreader = new InputStreamReader(instream);
    BufferedReader buffreader = new BufferedReader(inputreader);

    int numberOfLines, numberOfActions;
    String[] parts;

    String line = null;

    while((line=buffreader.readLine())!=null){
        parts=line.split(";");
        numberOfLines++;
    }
    numberOfActions=numberOfLines;

    while(numberOfActions-numberOfLines < numberOfLines){
        int intOne = Integer.parseInt(parts[0]);
        int intTwo = Integer.parseInt(parts[1]);
        String strLine = parts[2];
    }

}

Could someone help me?


Solution

  • The method openFileInput is defined for the Android Context class but not in the Game class.

    It appears that you're not doing Android development here. In that case you can use:

    InputStream instream = new FileInputStream("Storyline.txt");