3

Hello i got a quite annoying problem i'd like to read text files similar to this

KEY
0 1 2 3 4 5 6 7
KEYEND

i tried to use scanner class, because it can return results as strings, decimals, whatever

   public static void LoadStuff(String Name) {
      Scanner reader = null;
      try {
         reader = new Scanner(new File(Name));
      } catch (Exception e) {
         Log.d("damn", "FAIL");
      }
         if(reader != null)
            Load(reader);
   }


private static void Load(Scanner reader) {
      while (reader.hasNext()) {
         String result = reader.next();
         if (result == "KEY") {   // may be  result.equalsignorecase
            while (result != "KEYEND") {
               int index = reader.nextInt();
               Log.d("Index", String.valueOf(index));
            }
         }
      }
            reader.close();
   }

i cant do above, cause scanner cant find the file, parsing like "file.txt" doesnt work, tried also with path like this "res/data/file.txt" also doesnt work where should i put the file and how to get the directory to make it work thanks

6 Answers6

5

This code uses getAssets() method of your class. You need to put the text file in the assets folder of your Android project. The getAssets method returns a AssetManager object. The open(String.format("filename.txt")) method returns a InputStream. The InputStream is the parameter for DataInputStream. Then you use that as the input to the Scanner.

try {
    DataInputStream textFileStream = new DataInputStream(getAssets().open(String.format("filename.txt")));
    Scanner sc = new Scanner(textFileStream);
    while (sc.hasNextLine()) {
        String aLine = sc.nextLine();
        System.out.println(aLine);
    }
        sc.close();
} catch (IOException e) {
        e.printStackTrace();
}
kbunarjo
  • 1,277
  • 2
  • 11
  • 27
klisenko
  • 51
  • 1
  • 2
1

The code that I have always used to access files inside my current project (also text files) is:

textFileStream = new DataInputStream(getAssets().open(String.format("myFile.txt")));

You can always fill in the String.format portion with your own filled string.

The key has always been the

getAssets()

part.

So in your case, you may have something that looks as follows:

reader = new Scanner(new File(getAssets().open(String.format("myFile.txt")))); 

OR

reader = new Scanner(new File(getAssets().open(Name)));

The File constructor can take in a InputStream, the getAssets().open will return an InputStream.

trumpetlicks
  • 7,033
  • 2
  • 19
  • 33
0

Put your text file in the assets folder, then read it using getAssets().open("file.txt").

Example

nhaarman
  • 98,571
  • 55
  • 246
  • 278
0
  1. Try giving path like this "/sdcard/file.txt"

  2. And please always you "equals" when comparing objects. Never use "==" or "!=" with Objects. (String is an object too in Java)

    eg:

     if (result.equals("KEY"))` {       // equals is used
        `while (!(result.equals("KEYEND")))` {  // !equals is used
    
           int index = reader.nextInt();
           Log.d("Index", String.valueOf(index));
        }
     }
    
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
0

To write a file to internal storage use openFileOutput. According to Android Data Storage doc:

String FILENAME = "hello_file";
String string = "hello world!";

FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();

To use Scanner to read the file:

File f = new File( getFilesDir(), FILENAME );
Scanner s = new Scanner( f );

Note: getFilesDir() returns the absolute path to the directory on the filesystem where files created with openFileOutput(String, int) are stored.

To see what you output to your file then check out: is it possible to see application data from adb shell the same way I see it mounting SD card?.

Other Storage Options.

Community
  • 1
  • 1
worbel
  • 6,509
  • 13
  • 53
  • 63
0

You can do something like this:

File data = new File("yourfile"); 
Scanner sc = new Scanner(data);
int i = 0;
while(sc.hasNextLine){
    String Lines = sc.nextLine;
    System.out.println(Lines);
    i++;
}
Striezel
  • 3,693
  • 7
  • 23
  • 37