Reputation: 153
I am an android newby so apologies if this seems trivial, and the lengthy post. I have googled etc but the only android references I can find seem to refer to
InputStream is = getAssets().open("read_asset.txt");
int size = is.available();
// Read the entire asset into a local byte buffer.
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
I tried using this in my class but even after importing java.io.InputStream; it errored on the getAssets().
I am trying to use the rowId of a listview click to; open a text file (depends on rowId value), read the file line by line, generate a string array of the first 16 characters, etc then use the array to populate the next activity's listview.
String[] sectID = null; //to be loaded in listview
switch (rowId){
case 0://custom, go to section input screen
case 1:
readSectionFile s = new readSectionFile("Sect_US.dat");
sectID=s.arrayShapesAll();
My readSectionFile class (extract) is;
public readSectionFile(String FileName) {
//Count the number of section records in the data file
String line = null; // String that holds current file line
int recordcount = 0; // Line number of count
try{
BufferedReader buf = new BufferedReader(new FileReader(FileName));
// Read file to count records
while ((line = buf.readLine()) != null){ //null = EOF
line = buf.readLine();
if (line.substring(0, 1).equals("*") || line.length() == 0) { //== comment or blank line
//do nothing
}else{
recordcount++;
}
}//while
buf.close();
}catch (IOException x){
x.printStackTrace();
}//end try
// Now read file to load array
mSectionIDArray = new String[recordcount + 1];
mSectionIdx = new int[recordcount + 1][2];
mData = new double[recordcount + 1][15];
int c=0;
String sectdata = null; // String that holds current file line
try {
BufferedReader buf = new BufferedReader(new FileReader(FileName));
while ((sectdata = buf.readLine()) != null){ //null = EOF
sectdata = buf.readLine();
The code doesn't work and crashes at readSectionFile s = new readSectionFile("Sect_US.dat");
also, in the readSectionFile code, the second instance of buf generates an Eclipse error asking for a try, catch block, whereas the first instance is accepted.
My questions are, Am I going about opening this text file (in /assets) correctly? What is wrong with the second buf use?
Upvotes: 1
Views: 1306
Reputation: 28470
You wrote:
Am I going about opening this text file (in /assets) correctly?
One option is to place text files in a directory named raw
under res
, and read them into a String
using openRawResource(...)
.
Assuming you have a text file called my_text_file
in the raw
directory,
String myText = readTextFileFromResource(context, R.raw.my_text_file);
where
private String readTextFileFromResource(Context context, int resourceId){
StringBuilder fileContents = new StringBuilder();
try{
InputStream inputStream = context.getResources().openRawResource(resourceId);
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String nextLine;
while((nextLine = bufferedReader.readLine()) != null){
fileContents.append(nextLine);
fileContents.append('\n');
}
}catch(IOException e){
//handle
}catch(Resources.NotFoundException nfe){
//handle
}
return fileContents.toString();
}
Upvotes: 0
Reputation: 4246
getAssets() is a method in Activity. If you are trying to call getAssets() from a different class, pass an activity's context to the class where you want to call the method and then call context.getAssets().
Upvotes: 1