0

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?

Community
  • 1
  • 1
ikhebgeenaccount
  • 353
  • 6
  • 20

5 Answers5

1

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");
Reimeus
  • 158,255
  • 15
  • 216
  • 276
0

You have no method called openFileInput in that class but you are trying to call it

InputStream instream = openFileInput("Storyline.txt");

This method should be defined in your class, in a class that you extends or you'll need to qualify it by prefixing it with the class instance that it is contained in

 classInstance.openFileInput("file");

If you are doing Android development and you are talking about the method contained in Context then you will find more information here. Otherwise the other answers on this page will do what you want.

Community
  • 1
  • 1
Ross Drew
  • 8,163
  • 2
  • 41
  • 53
  • Yes, but that should come with the import right. Just like I imported the BufferedReader, etc. – ikhebgeenaccount Dec 05 '13 at 15:06
  • Where is it? You need to qualify it if it's from ourside your class, e.g. `myExternalClass.openFileInput("");` – Ross Drew Dec 05 '13 at 15:07
  • Which one should I get then? According to this http://stackoverflow.com/questions/6030744/android-reading-from-file-openfileinput I don't have to do this... Even when I copy that code I get errors. I might have missed an import, but can't find the necessary import for `openFileInput()`. – ikhebgeenaccount Dec 05 '13 at 15:13
  • are you developing in Android? – Ross Drew Dec 05 '13 at 15:16
  • No, I read that indeed. That got rid of the error, with using `FileInputStream()`, but I now get an error of an unhandled IOException. – ikhebgeenaccount Dec 05 '13 at 15:19
  • then you need to surround it with a `try { /*goes here*/} catch(IOException e) {}` but that's another issue. – Ross Drew Dec 05 '13 at 15:23
0

Use this line

BufferedReader buffreader = new BufferedReader(new FileReader("Storyline.txt"));

instead of..

InputStream instream = openFileInput("Storyline.txt");
InputStreamReader inputreader = new InputStreamReader(instream);
BufferedReader buffreader = new BufferedReader(inputreader);
subash
  • 3,116
  • 3
  • 18
  • 22
0

You dont have defined you onpenFileInput methond you should add some thing like this

public InputStream openFileInput(String filePath) throws FileNotFoundException{

return new FileInputStream(new File(filePath));

}
MB.One
  • 110
  • 1
  • 2
0

You may need to wrap your calls to BufferedReader and InputStream with try catch blocks. I believe you need to catch IOException and FileNotFoundException but I may be slightly off.