0

I wrote a large chunk of code with reading text files located on the SD card in mind.
I just realized that I'm going to need to read text files located in the Assets folder as well.
Check out the code I posted below.
If it were possible to do that, my problem would be solved.
Unfortunately, using an IF statement in such a manner is apparently not permitted.

At the moment, my only option is to make a duplicate of all of the file reading code and put it in a separate AsyncTask thread (my file-reading code is currently in an AsyncTask background thread) but it's about 250 lines of code so it would be better if I didn't have to duplicate it.
Any suggestions would be appreciated.

if (switchToAsset == 1);
{
InputStream myStream = getAssets().open(currentFilePath);
}
else
{
FileInputStream myStream = new FileInputStream(currentFilePath);
}
DataInputStream in = new DataInputStream(myStream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
Burkhard
  • 14,596
  • 22
  • 87
  • 108
Eric Glass
  • 141
  • 3
  • 12

1 Answers1

1
 InputStream myStream;
 if (switchToAsset == 1) { // having a semicolon here is BAD BAD BAD
   myStream = getAssets().open(currentFilePath);
 } else {
   myStream = new FileInputStream(currentFilePath);
 }

...or even more concisely...

 InputStream myStream = (switchToAsset == 1)
   ? getAssets().open(currentFilePath)
   : new FileInputStream(currentFilePath)
Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
  • Oops, didn't see that semicolon :) ... it wasn't there when I was testing. Thank you so much for the awesome answer! Works perfectly! Huge relief that I don't have to make a duplicate of all that code. :) – Eric Glass Jun 19 '13 at 20:51