14858

save and load from sdcard shows empty file

Question:

I have a layout where I have an Edit Text field where I enter my data. I have 2 buttons.Save and Plot.

When I press save I want to save the data (in xls format) from edittext field and the current date in sd card.

When I press the plot ,I want to plot them.

To save data:

case R.id.savebtn: savefunc(); break; ... public void savefunc(){ //saving File sdCard = Environment.getExternalStorageDirectory(); File directory = new File (sdCard, "MyFiles"); directory.mkdirs(); File file = new File(directory, filename); try { FileOutputStream fOut = new FileOutputStream(file); DataOutputStream os = new DataOutputStream(fOut); os.writeUTF(thedata); os.writeUTF(mydate); os.close(); } catch (FileNotFoundException e) { // handle exception } catch (IOException e) { // handle exception } }

For reading:

public void readfunc(){ File sdCard = Environment.getExternalStorageDirectory(); File directory = new File (sdCard, "MyFiles"); File file = new File(directory, filename); try{ FileInputStream fIn = new FileInputStream(file); DataInputStream is = new DataInputStream(fIn); String name = is.readUTF(); String content = is.readUTF(); is.close(); } catch (FileNotFoundException e) { // handle exception } catch (IOException e) { // handle exception } }

and :

case R.id.savebtn: savefunc(); break; case R.id.graphicsbtn: readfunc(); ...

But the xls file asks me format , I choose UTF8 and it is empty.

If I leave it shows chinese characters.

I am not sure about the "reading" part of the code.

Answer1:

Read this documentation here have more information about save data to external storage and read it is:

<a href="http://developer.android.com/training/basics/data-storage/files.html" rel="nofollow">Saving and Reading file from External storage</a>

Answer2:

Ok, I found <a href="http://www.mysamplecode.com/2012/05/java-read-file-bufferedinputstream.html" rel="nofollow">this</a> and works fine!

Recommend

  • Null pointer exception on null object reference
  • Loading array of display objects using shared objects in action script 3.0
  • Save XML file on Android device and read it
  • How to screen record iOS-real device with command?
  • Accessing root files (/system, /dev) from a native library in Android application
  • Android emulator sd card image already in use
  • How to export MS Access table into a csv file in Python using e.g. pypyodbc
  • Using python to determine if a timestamp is under daylight savings time
  • Uploading image using SpringMVC 4.0 Multipart
  • Teradata Database 3130 Response limit exceeded?
  • How to create MSBuild targets file to deploy files?
  • including Python.h in C++ file CDT
  • Representing intervals or ranges? [closed]
  • Visual Basic 6 on Win7 64bit: Will developed program run on 32-bit Win OS?
  • How to retrieve a specific string from a specific list from a file with JSON in Python
  • Read a file in “chunks” using PHP
  • How could I prohibit anonymous access to my NodeRed UI Dashboard on IBM Cloud(Bluemix)?
  • Hiding Directories Programatically in C#
  • Loading fixtures in sails tests
  • What is the use of a session store?
  • How to skip require in ruby?
  • Shouldn't else be indented in the below code
  • Saving image to sd with current date and time in name doesn't work
  • WPF - CanExecute dosn't fire when raising Commands from a UserControl
  • Yii2: Config params vs. const/define
  • sending/ receiving email in Java
  • 0x202A in filename: Why?
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • Cannot Parse HTML Data Using Android / JSOUP
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Load html files in TinyMce
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • JTable with a ScrollPane misbehaving
  • coudnt use logback because of log4j
  • How to stop GridView from loading again when I press back button?
  • unknown Exception android
  • EntityFramework adding new object to nested object collection
  • Checking variable from a different class in C#
  • failed to connect to specific WiFi in android programmatically
  • How can I use threading to 'tick' a timer to be accessed by other threads?