Structure Query Language, C programming, Java, Servlet, Jsp, Unix

Thursday, 18 October 2012

Create an application to read file from asset folder and copy it in memory card.

Pro16Activity.java
package ps.pro16;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class Pro16Activity extends Activity {
    /**
     *  www.master-gtu.blogspot.com
     *  pankaj sharma(8460479175),
     *  chavda vijay(8460420769) 
     */
 TextView tvmsgfromasset,tvmsgfromsdcard;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        try 
        {
         tvmsgfromasset=(TextView) findViewById(R.id.textView2);
            tvmsgfromsdcard=(TextView) findViewById(R.id.textView4);
            
   //--------reading from asset folder-----------
         
         InputStream is1=null;     
   is1=getResources().getAssets().open("hello.txt");
   
   if(is1!=null)
   {

    Toast.makeText(this, "File Exists", Toast.LENGTH_LONG).show();
    
    String myMsg1="";
    while(is1.available()>0)
    {
     myMsg1=myMsg1+(char)is1.read();
    }
    is1.close();
    
    tvmsgfromasset.setText(myMsg1);
    
   //--------writing to sdcard-----------
    
    byte b[]=myMsg1.getBytes();
    File myFile = new File("/sdcard/hello.txt");
    OutputStream os=new FileOutputStream(myFile);
    os.write(b);
    os.close();
    Toast.makeText(this, "write Success.", Toast.LENGTH_LONG).show();
    
   //--------read file from sdcard--------
    
    InputStream is2=null;
    is2=new FileInputStream(myFile);
    String myMsg2="";
    while(is2.available()>0)
    {
     myMsg2=myMsg2+(char)is2.read();
    }
    is2.close();
    tvmsgfromsdcard.setText(myMsg2+"");
   }
  } 
        catch (IOException e) 
        {
   Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
  }
    }
}

No comments:

Post a Comment