Wednesday, 17 August 2016

File Handling Part-2

Android File

Create file in internal Storage.
************************************************************************
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:tools="http://schemas.android.com/tools"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
android:paddingBottom="@dimen/activity_vertical_margin"
   
android:paddingLeft="@dimen/activity_horizontal_margin"
   
android:paddingRight="@dimen/activity_horizontal_margin"
   
android:paddingTop="@dimen/activity_vertical_margin"
   
tools:context="sichlearning.example.com.fileio.MainActivity">

   <
Button
      
android:layout_width="wrap_content"
      
android:layout_height="wrap_content"
      
android:text="create file"
      
android:onClick="writeFile"
      
/>
</
LinearLayout>

MainActivity.java
package sichlearning.example.com.fileio;



import android.os.Environment;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Toast;



import java.io.BufferedWriter;

import java.io.File;

import java.io.FileWriter;

import java.io.IOException;



public class MainActivity extends AppCompatActivity {



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

    }



    public void writeFile(View view)

{

    
/*To save file in sdcard, use
File logFile = new File("/sdcard/log.txt");
instead of or remove below code
code - 
File f = Environment.getExternalStorageDirectory();

    File logFile = new File(f.getAbsolutePath()+"/", "log.txt");
*/
 


    File f = Environment.getExternalStorageDirectory();

    File logFile = new File(f.getAbsolutePath()+"/", "log.txt");

    if (!logFile.exists())

    {

        try

        {

            logFile.createNewFile();

            Toast.makeText(this,"File created, check internal storage",Toast.LENGTH_LONG).show();

        }

        catch (IOException e)

        {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

    } else

    {  Toast.makeText(this,"File already created-check internal storage",Toast.LENGTH_LONG).show();}

    try

    {

        //BufferedWriter for performance, true to set append to file flag

        //BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true));

        BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true));

        buf.write("Hello, Divakar");

        buf.append("text read from file");

        buf.newLine();

        buf.close();



    }

    catch (IOException e)

    {

        // TODO Auto-generated catch block

        e.printStackTrace();

    }

}
 
**********************************************************************************

reading  file from  internal Storage.
************************************************************************
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context="sichlearning.example.com.fileio.MainActivity">



  

   <TextView

       android:id="@+id/txtfile"

       android:layout_width="match_parent"

       android:layout_height="wrap_content"

       android:hint="Text file data"

       />

   <Button

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:text="read file"

       android:onClick="readFile"

       />

</LinearLayout>

MainActivity.java
package sichlearning.example.com.fileio;



import android.os.Environment;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.TextView;

import android.widget.Toast;



import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;



public class MainActivity extends AppCompatActivity {



    TextView textView;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);



        textView=(TextView)findViewById(R.id.txtfile);

    }



    

    public void readFile(View view)

    {

        

        File f = Environment.getExternalStorageDirectory();

        File logFile = new File(f.getAbsolutePath()+"/", "log.txt");

        if (!logFile.exists())

        {

            try

            {

                logFile.createNewFile();

                Toast.makeText(this,"File created, check internal storage",Toast.LENGTH_LONG).show();

            }

            catch (IOException e)

            {

                // TODO Auto-generated catch block

                e.printStackTrace();

            }

        }

        try

        {



            FileReader fr=new FileReader(logFile);

            BufferedReader buf = new BufferedReader(fr);

            String str=buf.readLine().toString();

            buf.close();

            textView.setText("");

            textView.setText(str);

        }

        catch (IOException e)

        {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }



    }

}

**********************************************************************************
Delete File
****************************************************************
activity_main.xml
<Button

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="Delete File"

    android:onClick="deleteFile"

    />

MainActivity.java
public void deleteFile(View view)

{

    File f = Environment.getExternalStorageDirectory();

    File logFile = new File(f.getAbsolutePath()+"/", "log.txt");



    logFile.delete();

}


**************************************************************************

No comments:

Post a Comment