This articles talks about how we can read files and can create one if it doesn't exists.
This post is particularly about reading file or creating one in current project's package.
1. Create a new android project.
2. Open activity_main.xml and add two buttons and a TextView


This post is particularly about reading file or creating one in current project's package.
1. Create a new android project.
2. Open activity_main.xml and add two buttons and a TextView
3. Open MainActivity.java and add the following code-<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center" tools:context="com.example.gunjan.androidcodemode.MainActivity"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginBottom="25dp"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btnFileSize" android:text="Get File Size" android:textSize="25sp" android:layout_marginRight="30dp"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btnReadFile" android:text="Read File" android:textSize="25sp"/> </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="30sp" android:id="@+id/tvFileSize"/> </LinearLayout>
package com.example.gunjan.androidcodemode; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; public class MainActivity extends AppCompatActivity implements View.OnClickListener{ // Declare all controls Button btn_file_size,btn_read_file; TextView tv_file_size; // File name which is to be accessed or created String fileName="testFil.txt"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Initialize controls btn_file_size=(Button)findViewById(R.id.btnFileSize); btn_read_file=(Button)findViewById(R.id.btnReadFile); tv_file_size=(TextView)findViewById(R.id.tvFileSize); // Set onClickListener on button to enable them to be clicked btn_file_size.setOnClickListener(this); btn_read_file.setOnClickListener(this); } // This method will be called when a button is clicked @Override public void onClick(View view) { // Create file object with full path to file File file=new File("data/data/com.example.gunjan.androidcodemode/"+fileName); switch (view.getId()){ case R.id.btnFileSize: try{ // If file exists, get file size // Set file size to text view // Else, create file at the given path if(file.exists()){ long fileSize=file.length(); tv_file_size.setText("File size: "+String.valueOf(fileSize)+" Bytes"); }else{ tv_file_size.setText("File does not exists!!\nCreate new file"); // This function creates file in the current's project package file.createNewFile(); } }catch (Exception e){ e.printStackTrace(); } break; case R.id.btnReadFile: try{ // If file exists, read file content using buffered reader // Append file's content to text view // Else, create file at the given path if(file.exists()){ BufferedReader br=new BufferedReader(new FileReader(file)); String line=""; tv_file_size.setText(""); // Clear text view in case it has been set with text earlier while ((line=br.readLine())!=null){ tv_file_size.append(line); tv_file_size.append("\n"); } }else{ tv_file_size.setText("File does not exists!!\nCreate new file"); file.createNewFile(); } }catch (Exception e){ e.printStackTrace(); } break; } } }4. Run the project.
Comments
Post a Comment