1

I understand this question has probably been asked many times before, but I cannot find an example that caters towards my needs. I have asked questions on StackOverflow a few times before, and there are occasionally gents who ask me to "go read a book" or "do some research" before asking for help here. Well, I have done that, and I didn't find enough information - so if you're one of those people, I would really appreciate it if you could point me to a resource so I know where to dig.

Anyway, I finally finished writing an application that calculates bandwidth, link speed, and other attributes of the device. The bandwidth gets refreshed every 5 seconds, and the link speed and data connection refreshes every second. Currently, the information is displayed on the screen.

I'm trying to create a greater-schemed project that involves another computer (a command center) monitoring the connectivity of the device, and to do this, the device has to save these values to a text file that can be SSH'd by the command center. This question is trivial; how do I go about writing data to a text file? I know how to do this in Java; I could use FileOutputStream, or combine FileWriter + PrintWriter to achieve a similar effect. However, I don't know how directories work in Android, and I'm not sure whether there are any precautions I should be aware of while writing to files in Android. I notice that I need to state something like:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

in the manifest. Is there anything else that I should be doing? Also, for my needs, can anyone recommend a way to write to the output effectively? Is FileOutputStream the way to go, as it seems to be fairly easy? Should I tokenize my data a specific way, such as splitting it with a tab? I'm new to programming and I'm still learning, but I would appreciate some help or recommendations of useful readings.

This website, which is presumably the go-to reading, is currently down. If anyone could provide me some snippets of examples, it would be greatly appreciated.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Yuen Hsi
  • 143
  • 1
  • 4
  • 11
  • 2
    You need `WRITE_EXTERNAL_STORAGE` if you're going to write to `/sdcard`. You don't need it for the app-private data area, e.g. `/data/data//files`. You can get the latter from `Context.getFilesDir()`; note your Activity is a Context. – fadden Jul 11 '13 at 19:22

2 Answers2

1

Android directories work similar to PC directories, the only exception being that you can have different 'root' directories, such as internal or external storage. To make it a little more confusing, some phones have an internal SD card - so writing to an SD card may not be the external SD card. You're probably going to want to be flexible when you write the text file - if the user doesn't have an SD card (external storage) then you need to change your game plan. See this SO post for more info about detecting an SD card.

As for the actual writing of the text file, it's pretty trivial:

EDIT: You should use getExternalFilesDir(null) to get the absolute external path for your app. You must be inside of an activity to call this (or have access to a Context object).

   File file = new File(getExternalFilesDir(null), "stats.txt");
   try
   {
      BufferedWriter buf = new BufferedWriter(new FileWriter(file)); 
      buf.append(text);
      buf.newLine();
      buf.close();
   }
   catch (IOException e)
   {
      // TODO Auto-generated catch block
      e.printStackTrace();
   }

Note that there are dozens of ways to do file I/O in Android, and this is just one of them. Here is another blog post detailing the issue.

To store your data, I would do one of two things:

First, you could store your data as a CSV (comma-seperated value) string, like this:

12,345,6,789

Then when you parse it you know that the first number is bandwidth, the second is speed, etc. I would rather use a comma for a delimiter than a tab because I'm not a huge fan of using whitespaces as a delimiter.

Secondly, you could use a key/value pair setup, like this:

bandwidth=12
speed=34

Then in your parsing code, you can split on the = character. The first string is the key and the second is the value.

Community
  • 1
  • 1
crocboy
  • 2,887
  • 2
  • 22
  • 25
  • Instead of using a fixed string path, rather use you app's external data directory - you can get it using: activity.getExternalFilesDir(null); This way, the data does not remain once the app is uninstalled :) – free3dom Jul 11 '13 at 22:41
  • That's a good point, I was just trying to make a simple example. I'll edit my answer – crocboy Jul 12 '13 at 13:53
  • @crocboy would it be easier to store the text file in an internal directory? i see that files saved here are only accessed by my app by default, by is there a way to change the settings so the command center i'm building around also has permission to access these files? – Yuen Hsi Jul 12 '13 at 15:16
  • sorry, i just noticed that in my OP i said specifically that id like to write to the external storage. that was due to my misunderstanding of how writing to text files work - if internal files can be accessed / ssh'd by other machines, i would rather write internally, so to lessen the requirements of using this app. – Yuen Hsi Jul 12 '13 at 15:23
  • Files stored in the internal directory can only be accessed through your app, unless you root your device. Writing to external storage means that anyone can access it. – crocboy Jul 12 '13 at 15:33
0

At time of writing your link appears to be for a different language, and that version seems to be down. The English version is still up and can be found here: http://developer.android.com/training/basics/data-storage/files.html

In the meantime, I believe you have to access the raw directory which contains your text file (create a raw directory within the res directory if you don't already have one and put the text file there). The code to access the text file is getResources().openRawResource(resourceName) (as stated in this question: https://stackoverflow.com/a/2856501/555544) - I believe resourceName is the id of your text file as stated in your R file (e.g.: R.raw.foo) - this is how Android can find files in your directories. Then, just do your file I/O as you normally do.

Let me know how things go - I'm also learning Android myself but hopefully this points in the right direction.

Community
  • 1
  • 1
  • i get a 404 file not found error when opening your link... i wonder if it's my computer being silly :/ i've tried multiple browsers – Yuen Hsi Jul 11 '13 at 18:20
  • Strange... seems to work for me (though your link gave me 404) – Nondeterministic narwhal Jul 11 '13 at 18:21
  • interesting, works when i copy paste the link but not when i click on it - when i click on it i get redirected to the following webpage: http://developer.android.com/intl/zh-tw/training/basics/data-storage/files.html well, now that it works, i'll give it a good read. i'm new to programming, and new to android, but i'll update you regarding how it goes. thanks for your response! – Yuen Hsi Jul 11 '13 at 18:23