1

I was curious if I could read a text file from my computer on an android phone using FileReader and Buffered Reader class. Is the text file loaded onto the android phone from my desktop when the application begins?

This is the only relevant section of my code. I know the catch part is not there.

public void TestSeizureDetected() throws FileNotFoundException { try { FileReader fr = new FileReader("C:/Users/desiyosh/Desktop/patient1.txt"); BufferedReader textReader = new BufferedReader(fr); String[] temp = new String[7681]; double[] convert = new double[7681]; for(int z= 0; z<7861; z++) { temp[z] = textReader.readLine(); } textReader.close(); } } 
4
  • 1
    Your Android phone cannot read files on your computer. Period. Commented Mar 31, 2014 at 21:15
  • Is there an alternative? Should I just copy and paste the text file into one terribly lengthy string? Commented Mar 31, 2014 at 21:17
  • put it in raw or in assets. Commented Mar 31, 2014 at 21:18
  • Ask your question in a new post. Commented Mar 31, 2014 at 21:18

1 Answer 1

3

No, this will not work.

The phone and the computer are two separate computer devices, and the file system of your computer is not automatically mounted to your phone. Even when connected through USB cable.

Furthermore, even if it was, the Android OS is based on Linux, so it does not understand the Windows OS volumes like C:, and as such the path you've provided will be invalid path.

Also, you want to put the file name in parentheses, like this - "C:/Users/desiyosh/Desktop/patient1.txt", or the Java compiler will yell at you.

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

6 Comments

Is there a way to import a text file onto Android?
yes: adb push <local file name> <path to file location>
If you want a file to be always available to your application, you may want to package it as an "asset" however if you do that it will not be a literal file but rather file-like, and it will be read-only.
How to make a file accessible to your Android app depends on two things - 1) do you need the file to be read-only or read-write; and 2) does the file content change.
If the file content does not change and you need it as read-only, package it as an asset. Check for examples of using AssetManager class on how to do that.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.