1 2 package com.github.mikephil.charting.utils; 3 4 import android.content.res.AssetManager; 5 import android.os.Environment; 6 import android.util.Log; 7 8 import com.github.mikephil.charting.data.BarEntry; 9 import com.github.mikephil.charting.data.Entry; 10 11 import java.io.BufferedReader; 12 import java.io.BufferedWriter; 13 import java.io.File; 14 import java.io.FileReader; 15 import java.io.FileWriter; 16 import java.io.IOException; 17 import java.io.InputStreamReader; 18 import java.util.ArrayList; 19 import java.util.List; 20 21 /** 22 * Utilities class for interacting with the assets and the devices storage to 23 * load and save DataSet objects from and to .txt files. 24 * 25 * @author Philipp Jahoda 26 */ 27 public class FileUtils { 28 29 private static final String LOG = "MPChart-FileUtils"; 30 31 /** 32 * Loads a an Array of Entries from a textfile from the sd-card. 33 * 34 * @param path the name of the file on the sd-card (+ path if needed) 35 * @return 36 */ loadEntriesFromFile(String path)37 public static List<Entry> loadEntriesFromFile(String path) { 38 39 File sdcard = Environment.getExternalStorageDirectory(); 40 41 // Get the text file 42 File file = new File(sdcard, path); 43 44 List<Entry> entries = new ArrayList<Entry>(); 45 46 try { 47 @SuppressWarnings("resource") 48 BufferedReader br = new BufferedReader(new FileReader(file)); 49 String line; 50 51 while ((line = br.readLine()) != null) { 52 String[] split = line.split("#"); 53 54 if (split.length <= 2) { 55 entries.add(new Entry(Float.parseFloat(split[0]), Integer.parseInt(split[1]))); 56 } else { 57 58 float[] vals = new float[split.length - 1]; 59 60 for (int i = 0; i < vals.length; i++) { 61 vals[i] = Float.parseFloat(split[i]); 62 } 63 64 entries.add(new BarEntry(Integer.parseInt(split[split.length - 1]), vals)); 65 } 66 } 67 } catch (IOException e) { 68 Log.e(LOG, e.toString()); 69 } 70 71 return entries; 72 73 // File sdcard = Environment.getExternalStorageDirectory(); 74 // 75 // // Get the text file 76 // File file = new File(sdcard, path); 77 // 78 // List<Entry> entries = new ArrayList<Entry>(); 79 // String label = ""; 80 // 81 // try { 82 // @SuppressWarnings("resource") 83 // BufferedReader br = new BufferedReader(new FileReader(file)); 84 // String line = br.readLine(); 85 // 86 // // firstline is the label 87 // label = line; 88 // 89 // while ((line = br.readLine()) != null) { 90 // String[] split = line.split("#"); 91 // entries.add(new Entry(Float.parseFloat(split[0]), 92 // Integer.parseInt(split[1]))); 93 // } 94 // } catch (IOException e) { 95 // Log.e(LOG, e.toString()); 96 // } 97 // 98 // DataSet ds = new DataSet(entries, label); 99 // return ds; 100 } 101 102 /** 103 * Loads an array of Entries from a textfile from the assets folder. 104 * 105 * @param am 106 * @param path the name of the file in the assets folder (+ path if needed) 107 * @return 108 */ loadEntriesFromAssets(AssetManager am, String path)109 public static List<Entry> loadEntriesFromAssets(AssetManager am, String path) { 110 111 List<Entry> entries = new ArrayList<Entry>(); 112 113 BufferedReader reader = null; 114 try { 115 reader = new BufferedReader( 116 new InputStreamReader(am.open(path), "UTF-8")); 117 118 String line = reader.readLine(); 119 120 while (line != null) { 121 // process line 122 String[] split = line.split("#"); 123 124 if (split.length <= 2) { 125 entries.add(new Entry(Float.parseFloat(split[1]), Float.parseFloat(split[0]))); 126 } else { 127 128 float[] vals = new float[split.length - 1]; 129 130 for (int i = 0; i < vals.length; i++) { 131 vals[i] = Float.parseFloat(split[i]); 132 } 133 134 entries.add(new BarEntry(Integer.parseInt(split[split.length - 1]), vals)); 135 } 136 line = reader.readLine(); 137 } 138 } catch (IOException e) { 139 Log.e(LOG, e.toString()); 140 141 } finally { 142 143 if (reader != null) { 144 try { 145 reader.close(); 146 } catch (IOException e) { 147 Log.e(LOG, e.toString()); 148 } 149 } 150 } 151 152 return entries; 153 154 // String label = null; 155 // List<Entry> entries = new ArrayList<Entry>(); 156 // 157 // BufferedReader reader = null; 158 // try { 159 // reader = new BufferedReader( 160 // new InputStreamReader(am.open(path), "UTF-8")); 161 // 162 // // do reading, usually loop until end of file reading 163 // label = reader.readLine(); 164 // String line = reader.readLine(); 165 // 166 // while (line != null) { 167 // // process line 168 // String[] split = line.split("#"); 169 // entries.add(new Entry(Float.parseFloat(split[0]), 170 // Integer.parseInt(split[1]))); 171 // line = reader.readLine(); 172 // } 173 // } catch (IOException e) { 174 // Log.e(LOG, e.toString()); 175 // 176 // } finally { 177 // 178 // if (reader != null) { 179 // try { 180 // reader.close(); 181 // } catch (IOException e) { 182 // Log.e(LOG, e.toString()); 183 // } 184 // } 185 // } 186 // 187 // DataSet ds = new DataSet(entries, label); 188 // return ds; 189 } 190 191 /** 192 * Saves an Array of Entries to the specified location on the sdcard 193 * 194 * @param entries 195 * @param path 196 */ saveToSdCard(List<Entry> entries, String path)197 public static void saveToSdCard(List<Entry> entries, String path) { 198 199 File sdcard = Environment.getExternalStorageDirectory(); 200 201 File saved = new File(sdcard, path); 202 if (!saved.exists()) 203 { 204 try 205 { 206 saved.createNewFile(); 207 } catch (IOException e) 208 { 209 Log.e(LOG, e.toString()); 210 } 211 } 212 try 213 { 214 // BufferedWriter for performance, true to set append to file flag 215 BufferedWriter buf = new BufferedWriter(new FileWriter(saved, true)); 216 217 for (Entry e : entries) { 218 219 buf.append(e.getY() + "#" + e.getX()); 220 buf.newLine(); 221 } 222 223 buf.close(); 224 } catch (IOException e) 225 { 226 Log.e(LOG, e.toString()); 227 } 228 } 229 loadBarEntriesFromAssets(AssetManager am, String path)230 public static List<BarEntry> loadBarEntriesFromAssets(AssetManager am, String path) { 231 232 List<BarEntry> entries = new ArrayList<BarEntry>(); 233 234 BufferedReader reader = null; 235 try { 236 reader = new BufferedReader( 237 new InputStreamReader(am.open(path), "UTF-8")); 238 239 String line = reader.readLine(); 240 241 while (line != null) { 242 // process line 243 String[] split = line.split("#"); 244 245 entries.add(new BarEntry(Float.parseFloat(split[1]), Float.parseFloat(split[0]))); 246 247 line = reader.readLine(); 248 } 249 } catch (IOException e) { 250 Log.e(LOG, e.toString()); 251 252 } finally { 253 254 if (reader != null) { 255 try { 256 reader.close(); 257 } catch (IOException e) { 258 Log.e(LOG, e.toString()); 259 } 260 } 261 } 262 263 return entries; 264 265 // String label = null; 266 // ArrayList<Entry> entries = new ArrayList<Entry>(); 267 // 268 // BufferedReader reader = null; 269 // try { 270 // reader = new BufferedReader( 271 // new InputStreamReader(am.open(path), "UTF-8")); 272 // 273 // // do reading, usually loop until end of file reading 274 // label = reader.readLine(); 275 // String line = reader.readLine(); 276 // 277 // while (line != null) { 278 // // process line 279 // String[] split = line.split("#"); 280 // entries.add(new Entry(Float.parseFloat(split[0]), 281 // Integer.parseInt(split[1]))); 282 // line = reader.readLine(); 283 // } 284 // } catch (IOException e) { 285 // Log.e(LOG, e.toString()); 286 // 287 // } finally { 288 // 289 // if (reader != null) { 290 // try { 291 // reader.close(); 292 // } catch (IOException e) { 293 // Log.e(LOG, e.toString()); 294 // } 295 // } 296 // } 297 // 298 // DataSet ds = new DataSet(entries, label); 299 // return ds; 300 } 301 } 302