0

I am trying to save values in a string to a file on my local system by using the org.apache.commons.io.FileUtils static method writeStringToFile.

So, first I downloaded the commons-io-2.4.jar, along with its javadocs and source and imported it into my Eclipse project through the Java Build Path. Everything compiles just fine.

However, when I add the simple line:

org.apache.commons.io.FileUtils.writeStringToFile(new java.io.File(Environment.getExternalStorageDirectory().toString() + "/logt.txt"), rslt.toString()); 

The program crashes. But, it doesn't crash anywhere near that statement. Instead, it crashes at the constructor of the Object which contains the method which calls this function.

In other words

  • Earlier in my code I create an object TranslateTask,
    • Which happens to contain a function call doTranslate().
    • The writeStringToFile call is made within the doTranslate() call,
  • However the actual crash takes place when I instantiate a TranslateTask object.
  • But, when I comment out the call to writeStringToFile(), the crash never happens
    • Even though the crash doesn't take place in the doTranslate() call...
    • So, just the mere mention of writeStringToFile() makes my program crash when I instantiate an object which contains it.

To make it more eary, I instantiate the object within a Try, catch (RejectedExecutionException e) block but instead the program crashes at this part:

PathClassLoader.findClass(String) line: 243 **Last call in the stack** PathClassLoader(ClassLoader).loadClass(String, boolean) line: 573 PathClassLoader(ClassLoader).loadClass(String) line: 532 Translate$4.run() line: 159 **Crash happens here where I instantiate a TranslateTask object** 

So PathClassLoader... Not sure how to approach debugging this. All I know is that if I commend out the org.apache.commons.io.FileUtils.writeStringToFile() call, the error never happens and the code runs fine everywhere.

Running this on the API8/10 of Android, using Eclipse Indigo.

EDIT- Logcat:

08-07 18:40:11.409: I/System.out(1395): debugger has settled (1363)

08-07 18:40:14.399: W/KeyCharacterMap(1395): No keyboard for id 0

08-07 18:40:14.399: W/KeyCharacterMap(1395): Using default keymap: /system/usr/keychars/qwerty.kcm.bin

Don't think it gives any hint, but this is all that logcat gives after the debugger has settled.

EDIT2 - For good measure, I just added <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> But still no go

EDIT3 - I am starting to think the apache commons-io-2.4.jar doesn't work with Android inherently. Could that be it?

EDIT4 - In case anyone wants to take a crack at it, here is my code. It is made in Windows 7, Eclipse Indigo. The code is based on the "Hello Android" translate section. All I am trying to do is extract some JSON into a string and save it into my sdcard. But, it has evolved into this mystery here... https://dl.dropbox.com/u/10790286/Translate.zip (See TranslateTask.java line 111)

EDIT5 - Interesting update. When I manually put FileUtils.writeStringToFile(new java.io.File("/mnt/sdcard/logt.txt"), "test"); into the eclipse expressions box during a debug, I get the following error

An exception occurred: java.lang.NoSuchMethodError

I don't understand why I would get this error because 1) the WriteStringToFile() function is in the commons-io source. 2) I can instantiate a FileUtils object 3) I imported it into my program.

Whats going on here?

1
  • In the future post any relevant information from Android logcat. Commented Aug 7, 2012 at 18:31

2 Answers 2

1

Writing to the path "c:\\temp\\logt.txt" isn't going to work on Android. You can use something like Environment.getExternalStorageDirectory().toString() + "/log.txt" to write to the SD card.

EDIT: The follow worked on my Galaxy Nexus:

try { FileUtils.writeStringToFile(new File(Environment.getExternalStorageDirectory(),"log2.txt"), "HelloNow"); } catch (IOException e) { Log.e(TAG,e.getMessage()) } 
Sign up to request clarification or add additional context in comments.

3 Comments

Oh right, I needed to go use /mnt/sdchard/logt.txt. However, I still get the same crash with the same stack. Good call on the saving location though.
Seems to work on ICS. You could also try version 2.2 of commons.io.
Version 2.2 didn't help, same issues... This is becoming quite a puzzling mystery :(
0

Though the programme runs succesfully in complier ,the programme crashes while it runs in android device .Because during the run time it checks over jar files within android dependancies only. I too had similar type of problem .I got it solved when in preferences/properties -> java build .Tick the external jar file .

2 Comments

I did add the apache JAR file to the Java Build path. I'm not sure if I see an exact External Jar option I can tick. All I know is I added it to the build path...
I think I found the tick mark you are referring to at Java Build Path -> Order and Export -> Tick commons-io-2.4.jar. However the crash still happens.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.