public class Io03 extends Activity {
    private Button b1, b2, b3;
    private EditText et1;
    //private static final String FNAME = "test.txt";
    private static final String FNAME 
       = "aa/bb/SoosJava201104_utf8.java";
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        b1 = (Button)findViewById(R.id.Button01);
        b2 = (Button)findViewById(R.id.Button02);
        b3 = (Button)findViewById(R.id.Button03);
        
        et1 = (EditText)findViewById(R.id.EditText01);
        
        View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
if(v == b1){
readAssets();
}else if(v == b2){
readRaw();
}else{
reset();
}
}
} ;
        b1.setOnClickListener(listener);
        b2.setOnClickListener(listener);
        b3.setOnClickListener(listener);
    }
    private void readAssets(){
    InputStream is = null;
    AssetManager am = getAssets();
    try{
       is = am.open(FNAME, MODE_PRIVATE);
       String result = readModul(is);
       et1.setText(result);
    }catch(IOException ie){
    }finally{
    try{
    if(is != null) is.close();
    }catch(IOException ie){}
    }
    }
    private void readRaw() {
    InputStream is = null;
    Resources r = getResources();
    is = r.openRawResource(R.raw.soosadvans_utf8);
    String result = readModul(is);
    et1.setText(result);
    try{
    if(is != null) is.close();
    }catch(IOException ie){}
    }
    private String readModul(InputStream is){
    BufferedReader br = null;
    String line = "";
    StringBuffer sb = new StringBuffer();
    try{
       br = new BufferedReader(new InputStreamReader(is));
       while((line = br.readLine()) != null){
        sb.append(line + "\n");
       }
       return sb.toString();
    }catch(IOException ie){
    return "";
    }finally{
    try{
    if(br != null) br.close();
    }catch(IOException ie){}
    }
    }
    private void reset(){
    et1.setText("");
    }
}

+ Recent posts