# Trouble writing String to text file on external storage



## SkullOne (Jul 9, 2011)

Greetings,

In my app I'm adding a way for users to send me an error report. This report pulls some bits of information from the device. My goal is to have 3 or 4 small text files that get zipped and inserted into an e-mail. I want the files on the sdcard (they get overwritten if reports are run again) so users can see exactly what is being sent. Sure they can open the zip file but leaving the files on the sdcard makes it far easier for them as well.

I use a root shell with a Callback method to get the interface information using "ls /sys/class/net". That populates a new StringBuilder. That works great and a Log.d (TAG, info.toString()) prints that information out into the Logcat correctly. My issue is I cannot for the life of me get that to correctly dump into a text file on external storage.

The file is created properly but I get zero data. I've tried many different implementations and threw this up on Stackoverflow as well. I've gotten nowhere with any of it so I figured I'd toss this question up here as well as I've gotten great help here before.

So any help pointing me in the right direction of turning that StringBuilder into a file is greatly appreciated.

Thanks!


----------



## yarly (Jun 22, 2011)

Did you add proper read/write permissions for external storage?

Also, no code, can't help.


----------



## SkullOne (Jul 9, 2011)

All permissions are correct. If I hard code the string the file is generated properly and is populated. It's just trying to insert the big string that fails as the file is generated. The file is just empty. The Log.d displays the interface names correctly so something has to be wrong in my try/catch but I can't figure out what.

Thanks for the Help!

final StringBuilder ifacelist = new StringBuilder();


```
String names = ifacelist.toString();
Log.d(TAG, ifacelist.toString());

try {
     FileOutputStream fout = new FileOutputStream(file);
     OutputStreamWriter output = new OutputStreamWriter(fout);
     output.write(names,0,names.length());
     output.close();
     fout.close();
} catch (IOException e) {
     Log.e(TAG, "File write failed: " + e.toString());
}
```


----------



## yarly (Jun 22, 2011)

SkullOne said:


> All permissions are correct. If I hard code the string the file is generated properly and is populated. It's just trying to insert the big string that fails as the file is generated. The file is just empty. The Log.d displays the interface names correctly so something has to be wrong in my try/catch but I can't figure out what.
> 
> Thanks for the Help!
> 
> ...


----------



## SkullOne (Jul 9, 2011)

Thanks for that Yarly. That pointed me in the right direction. The issue was actually in the callback from the root shell. The string was proper but when passing it to the other method it became empty for some reason. So I re-wrote how I get the string and the callback and combined with your code it's all working properly.

Many thanks!


----------

