1

So this is a pretty embarrassing question, but i have a text file and java will read all of the words in it and add it to a array, i don't know where to put the text file, like what folder so the comp can go get it? could someone tell me. my code works in a regular java application, so it should work on android.

3 Answers 3

1

you can use

<your-context>.getAssets(); 

to return an AssetsManager object.

AssetsManager assets = context.getAssets(); 

You can then open an input stream with the open() method.

InputStream inputStream = assets.open("filename"); 

The InputStream object is a standard Java object from the IO package. You can decorate this stream with an object decorator you wish (Reader, BufferedReader, etc).

If you wish to move this file out of the APK (that is not inflated) to the phone you can just copy the bytes of the file from the input stream using an output stream. Note you will have to have permissions in your write directory (you can do this if your phone is rooted and you have created a shell interface to run native shell commands through JNI).

UPDATE

try { InputStream inputStream = this.getAssets().open("test.txt"); BufferedReader buffer = new BufferedReader(new Reader(inputStream)); String line; while((line = buffer.readLine()) != null) { tots.add(line); } } catch(IOException e) { e.printStackTrace(); } 

Haven't tested it, but I think this is what you want.

Sign up to request clarification or add additional context in comments.

4 Comments

Could u explain the first thing like what goes in the < > the file name is test.txt
You need a context object to pull the assets from, IE the getAssets() method is a context method. So if your in an Activity or anything that inherits from ContextWrapper you can just say "this.getAssets()". Otherwise you will need to pass a context reference to the function/method that is attempting to get the Assets Manager.
try { FileInputStream textfl = (FileInputStream) getAssets().open("test.txt"); DataInputStream is = new DataInputStream(textfl); BufferedReader r = new BufferedReader(new InputStreamReader(is)); String strLine; while ((strLine = r.readLine()) != null) { tots.add(strLine); //tots is the array list } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } i have that right now and it doesn't work.... do u know a quick fix from that?
Try using InputStream instead of FileInputStream. Drop the DataInputStream as you are not using any methods in its decoration, instead use Reader.
0

You can put the file to assets folder and use

InputStream stream = getAssets().open(filename); 

to get the input stream

1 Comment

i did this, but now i am getting a error when the code works. that is a link to my new question maybe check it out?stackoverflow.com/questions/9676773/read-a-text-file-android
0

I created new raw folder in res folder and put chapter0.txt in here.

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.induction); wordss = new Vector<String>(); TextViewEx helloTxt = (TextViewEx) findViewById(R.id.test); helloTxt.setText(readTxt()); } private String readTxt() { InputStream inputStream = getResources().openRawResource(R.raw.chapter0); // getResources().openRawResource(R.raw.internals); System.out.println(inputStream); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); int i; try { i = inputStream.read(); while (i != -1) { byteArrayOutputStream.write(i); i = inputStream.read(); } inputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return byteArrayOutputStream.toString(); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.