[android-beginners] Re: what is the fast way to save/load string from/in an array?
Instead of using Strings, try to use StringBuffer. Normally the usage of StringBuffer's is more efficient than normal Strings. By the way for analyzing your code to find out where you spend most of the time, use the traceview tool. -- Roman Baumgaertner Sr. SW Engineer-OSDC ·T· · ·Mobile· stick together The views, opinions and statements in this email are those of the author solely in their individual capacity, and do not necessarily represent those of T-Mobile USA, Inc. On Aug 25, 11:25 am, Liviu Ungureanu smartli...@gmail.com wrote: Thank you for reply,, I want to save the history for my application in a file on sdcard. First i tried to save it as xml and parse it to load...but these actions take a lot of time..(almost 8 seconds for 20 strings on a HTC Hero device); this is the code: package com.app.lookitup2; import java.io.BufferedInputStream; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import org.apache.http.util.ByteArrayBuffer; import android.content.Context; import android.os.Environment; import android.util.Log; public class HistoryManager { private ArrayListHistoryItem history_items; private Context context; private final String WHAT_TAG_START = what; private final String WHAT_TAG_END = /what; private final String WHERE_TAG_START = where; private final String WHERE_TAG_END = /where; private final String DATE_TAG_START = date; private final String DATE_TAG_END = /date; private final String ITEM_TAG_START = item; private final String ITEM_TAG_END = /item; public HistoryManager(Context ctx) { context = ctx; history_items = new ArrayListHistoryItem(); } public void addHistoryItem(HistoryItem h_item) { history_items.add(h_item); } public HistoryItem getHistoryItem(int position) { return history_items.get(position); } public boolean saveHistory() { String data; int i; File root = Environment.getExternalStorageDirectory(); File history_file = new File(root,history_lookitup.xml); if(history_file.length() = 1048576) { Log.e(SAVE_HISTORY,History is full!Please empty!); return false; } data = new String(); for(i = 0; i history_items.size(); i++) { data = data + ITEM_TAG_START + WHAT_TAG_START + history_items.get(i).getWhat().getText().toString() + WHAT_TAG_END + WHERE_TAG_START + history_items.get(i).getWhere().getText().toString() + WHERE_TAG_END + DATE_TAG_START + history_items.get(i).getDate().toString() + DATE_TAG_END + ITEM_TAG_END; } try { if(root.canWrite()) { FileWriter history_writer = new FileWriter(history_file,true); BufferedWriter out = new BufferedWriter(history_writer); out.write(data); out.close(); Log.e(SAVE HISTORY,History is saved); return true; } else { Log.e(SAVE HISTORY,no sd card or no permission to write on it); return false; } } catch (IOException e) { Log.e(WRITE FILE,Could not write file + e.getMessage()); return false; } } public boolean loadHistory() throws IOException { String myString = ; ArrayListHistoryItem items; FileInputStream is; try { is = new FileInputStream(/sdcard/history_lookitup.xml); BufferedInputStream bis = new BufferedInputStream(is); ByteArrayBuffer baf = new ByteArrayBuffer(50); int current = 0; while((current = bis.read()) != -1) baf.append((byte)current); myString = new String(baf.toByteArray()); if(myString.equals( ) == false) { HistoryParser parser = new HistoryParser(?xml version='1.0' encoding='UTF-8'?main + myString + /main,context); items = new ArrayListHistoryItem(); items = parser.getItemList(); Log.e(FIRST_HISTORY, items.get(0).getDate().toString()); Log.e(DATA,myString); history_items = items; Log.e(history_items = ,Integer.toString(history_items.size())); return true; } else return false; } catch (FileNotFoundException e) { e.printStackTrace(); return
[android-beginners] Re: what is the fast way to save/load string from/in an array?
Thank you very much!i will try tonight.. Thanks! On Aug 26, 2009 7:31 PM, Roman ( T-Mobile USA) roman.baumgaert...@t-mobile.com wrote: Instead of using Strings, try to use StringBuffer. Normally the usage of StringBuffer's is more efficient than normal Strings. By the way for analyzing your code to find out where you spend most of the time, use the traceview tool. -- Roman Baumgaertner Sr. SW Engineer-OSDC ·T· · ·Mobile· stick together The views, opinions and statements in this email are those of the auth... On Aug 25, 11:25 am, Liviu Ungureanu smartli...@gmail.com wrote: Thank you for reply,, I wan... --~--~-~--~~~---~--~~ You received this message because you are subs... --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Android Beginners group. To post to this group, send email to android-beginners@googlegroups.com To unsubscribe from this group, send email to android-beginners-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en -~--~~~~--~~--~--~---
[android-beginners] Re: what is the fast way to save/load string from/in an array?
Can you post the code that takes a long time? How long does it take, for how many strings? Yusuf Saib Android ·T· · ·Mobile· stick together The views, opinions and statements in this email are those of the author solely in their individual capacity, and do not necessarily represent those of T-Mobile USA, Inc. On Aug 25, 2:08 am, Liviu Ungureanu smartli...@gmail.com wrote: Hi! I want to save an array with strings. I try to write in a file and to save it on sdcard but the loading process get so much time... Thank you! --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Android Beginners group. To post to this group, send email to android-beginners@googlegroups.com To unsubscribe from this group, send email to android-beginners-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en -~--~~~~--~~--~--~---
[android-beginners] Re: what is the fast way to save/load string from/in an array?
Thank you for reply,, I want to save the history for my application in a file on sdcard. First i tried to save it as xml and parse it to load...but these actions take a lot of time..(almost 8 seconds for 20 strings on a HTC Hero device); this is the code: package com.app.lookitup2; import java.io.BufferedInputStream; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import org.apache.http.util.ByteArrayBuffer; import android.content.Context; import android.os.Environment; import android.util.Log; public class HistoryManager { private ArrayListHistoryItem history_items; private Context context; private final String WHAT_TAG_START = what; private final String WHAT_TAG_END= /what; private final String WHERE_TAG_START = where; private final String WHERE_TAG_END = /where; private final String DATE_TAG_START = date; private final String DATE_TAG_END= /date; private final String ITEM_TAG_START = item; private final String ITEM_TAG_END= /item; public HistoryManager(Context ctx) { context = ctx; history_items = new ArrayListHistoryItem(); } public void addHistoryItem(HistoryItem h_item) { history_items.add(h_item); } public HistoryItem getHistoryItem(int position) { return history_items.get(position); } public boolean saveHistory() { String data; int i; File root = Environment.getExternalStorageDirectory(); File history_file = new File(root,history_lookitup.xml); if(history_file.length() = 1048576) { Log.e(SAVE_HISTORY,History is full!Please empty!); return false; } data = new String(); for(i = 0; i history_items.size(); i++) { data = data + ITEM_TAG_START + WHAT_TAG_START + history_items.get(i).getWhat().getText().toString() + WHAT_TAG_END + WHERE_TAG_START + history_items.get(i).getWhere().getText().toString() + WHERE_TAG_END + DATE_TAG_START + history_items.get(i).getDate().toString() + DATE_TAG_END + ITEM_TAG_END; } try { if(root.canWrite()) { FileWriter history_writer = new FileWriter(history_file,true); BufferedWriter out = new BufferedWriter(history_writer); out.write(data); out.close(); Log.e(SAVE HISTORY,History is saved); return true; } else { Log.e(SAVE HISTORY,no sd card or no permission to write on it); return false; } } catch (IOException e) { Log.e(WRITE FILE,Could not write file + e.getMessage()); return false; } } public boolean loadHistory() throws IOException { String myString = ; ArrayListHistoryItem items; FileInputStream is; try { is = new FileInputStream(/sdcard/history_lookitup.xml); BufferedInputStream bis = new BufferedInputStream(is); ByteArrayBuffer baf = new ByteArrayBuffer(50); int current = 0; while((current = bis.read()) != -1) baf.append((byte)current); myString = new String(baf.toByteArray()); if(myString.equals( ) == false) { HistoryParser parser = new HistoryParser(?xml version='1.0' encoding='UTF-8'?main + myString + /main,context); items = new ArrayListHistoryItem(); items = parser.getItemList(); Log.e(FIRST_HISTORY, items.get(0).getDate().toString()); Log.e(DATA,myString); history_items = items; Log.e(history_items = ,Integer.toString(history_items.size())); return true; } else return false; } catch (FileNotFoundException e) { e.printStackTrace(); return false; } } public int getCount() { return history_items.size(); } public ArrayListHistoryItem getHistoryList() { return history_items; } public void clearHistory() throws IOException { File root = Environment.getExternalStorageDirectory(); File history_file = new File(root,history_lookitup.xml); if(root.canWrite()) { FileWriter history_writer = new FileWriter(history_file); BufferedWriter out = new BufferedWriter(history_writer); out.write( ); out.close(); Log.e(Clear HISTORY,History is empty now);