Load/save Int Map Data : Stream « File « Android






Load/save Int Map Data

    
//package com.akjava.lib.android.map;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class MapIO {

  public static final int[][] loadIntMapData(InputStream input) throws  IOException{
    BufferedReader reader=new BufferedReader(new InputStreamReader(input));
    String line;
    ArrayList<int[]> list=new ArrayList<int[]>();
    while((line=reader.readLine())!=null){
      if(line.equals("")){
        //ignore
      }else{
        String[] vs=line.split(",");
        int ints[]=new int[vs.length];
        for (int i = 0; i < ints.length; i++) {
          ints[i]=Integer.parseInt(vs[i]);
        }
        list.add(ints);
      }
      
    }
    int[][] map=new int[list.size()][];
    for (int i = 0; i < map.length; i++) {
      map[i]=list.get(i);
    }
    return map;
  }
  
public static final void writInteMapData(int[][] map,File file) throws IOException{
    StringBuffer output=new StringBuffer();
    for(int i=0;i<map.length;i++){
    
      for(int j=0;j<map[i].length;j++){
        output.append(Integer.toString(map[i][j]));
        if(j!=map[i].length-1){
          output.append(",");
        }
      }
      
      if(i!=map.length-1){
        output.append("\r\n");
      }
    }
    
    
      BufferedWriter writer=new BufferedWriter(new FileWriter(file));
      writer.write(output.toString());
      writer.close();
    
    
  }
}

   
    
    
    
  








Related examples in the same category

1.Playing Back Audio Streams
2.Stream Proxy
3.Copy Stream
4.Ini File Stream Reader
5.Read InputStream fully to a string
6.Read Stream to byte array
7.read String From Stream
8.Read stream Fully
9.Read InputStream with ByteArrayOutputStream
10.get Resource As Stream, Loads the resource from classpath
11.InputStream to byte array, copy Reader and Writer,
12.InputStream that notifies listeners of its progress.
13.String to InputStream
14.Context.getFileStreamPath
15.stream To String
16.stream To Bytes
17.Write InputStream into a StringBuilder