<p>This article is about how to read a file from the assets folder in android in both languages Java/Kotlin.</p>
<p><amp-youtube layout="responsive" width="1200" height="675" data-videoid="nJzWkv9CgqQ" title="Android read a file from assets in java/Kotlin"><a placeholder href="https://youtu.be/nJzWkv9CgqQ"><img src="https://i.ytimg.com/vi/nJzWkv9CgqQ/hqdefault.jpg" layout="fill" object-fit="cover" alt="Android read a file from assets in java/Kotlin"></a></amp-youtube></p>
<h4><strong><span style="color: #000080;">Creating Assets folder and a file</span></strong></h4>
<p class="p2">1. To create an <span style="color: #008000;"><b>asset</b></span> folder in Android studio open your project in <strong><span style="color: #008000;">Android</span></strong> mode. Go to the <span style="color: #008000;"><b>app >; right-click >; New >; Folder >; Assets Folder</b></span>.</p>
<p><img class="wp-image-2748 aligncenter" src="https://c1ctech.com/wp-content/uploads/2021/07/Screen-Shot-2021-07-22-at-3.21.44-PM-1024x608.png" alt="" width="637" height="378" /></p>
<p> ;</p>
<p class="p1">2. Android Studio will open a dialog box. Under the<span style="color: #008000;"><strong> Target Source</strong> <strong>Set</strong></span>, option <span style="color: #0000ff;"><b>main</b></span> should be selected, and click on the <span style="color: #0000ff;"><b>finish </b></span>button.</p>
<p class="p1">3. Right-click on the <strong><span style="color: #008000;">assets</span></strong> folder, select <span style="color: #008000;"><strong>New >;>; file (myText.txt),</strong></span> and add your text.</p>
<p><span style="color: #0000ff;"><strong>myText.txt</strong></span></p>
<pre>"All our dreams can come true if we have the courage to pursue them"</pre>
<h4><strong><span style="color: #000080;">Creating the XML file</span></strong></h4>
<p>4 . The below layout file consists of a Button and a TextView.</p>
<p><strong><span style="color: #0000ff;">activity_main.xml</span></strong></p>
<pre><;?xml version="1.0" encoding="utf-8"?>;
<;androidx.constraintlayout.widget.ConstraintLayout 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"
 tools:context=".MainActivity">;

 <;Button
 android:id="@+id/btn_reading_text"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_marginTop="30dp"
 android:background="@color/colorPrimaryDark"
 android:padding="15dp"
 android:text="Reading text from Assets"
 android:textColor="@android:color/white"
 app:layout_constraintEnd_toEndOf="parent"
 app:layout_constraintHorizontal_bias="0.5"
 app:layout_constraintStart_toStartOf="parent"
 app:layout_constraintTop_toTopOf="parent" />;

 <;TextView
 android:id="@+id/tv_text"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_marginTop="20dp"
 android:gravity="center"
 android:padding="15dp"
 android:textColor="@color/colorPrimaryDark"
 android:textSize="25sp"
 android:textStyle="bold"
 app:layout_constraintEnd_toEndOf="@+id/btn_reading_text"
 app:layout_constraintStart_toStartOf="@+id/btn_reading_text"
 app:layout_constraintTop_toBottomOf="@+id/btn_reading_text" />;

<;/androidx.constraintlayout.widget.ConstraintLayout>;</pre>
<h4><span style="color: #000080;"><strong>Implementing code in Activity</strong></span></h4>
<p>5 . In MainActivity, on-click of the button, we will read the text file (myText.txt) from the assets folder and convert it into a String, and then finally set it in a textview.</p>
<!-- WP QUADS Content Ad Plugin v. 2.0.98.1 -->
<div class="quads-location quads-ad2" id="quads-ad2" style="float:none;margin:0px;">

</div>

<h4><strong><span style="color: #000080;">Java code:</span></strong></h4>
<p><strong><span style="color: #0000ff;">MainActivity.java</span></strong></p>
<pre>package com.c1ctech.readassetsfileexp;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.io.IOException;
import java.io.InputStream;

public class MainActivity extends AppCompatActivity {

 Button btnReadingText;
 TextView tvText;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main2);

 tvText = findViewById(R.id.tv_text);

 btnReadingText = findViewById(R.id.btn_reading_text);
 btnReadingText.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View view) {

 readFromAsset();
 }
 });

 }

 <strong><span style="color: #008000;">//read file myText.txt from assets folder</span></strong>
 private void readFromAsset() {
 String string = "";

 try {
 InputStream inputStream = getAssets().open("myText.txt");
 int size = inputStream.available();
 byte[] buffer = new byte[size];
 inputStream.read(buffer);
 string = new String(buffer);
 } catch (IOException e) {
 e.printStackTrace();
 }

 tvText.setText(string);
 }

}</pre>
<h4><strong><span style="color: #000080;">Kotlin code:</span></strong></h4>
<p><strong><span style="color: #0000ff;">MainActivity.kt</span></strong></p>
<pre>package com.c1ctech.readassetsfileexp

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
import java.io.IOException

class MainActivity : AppCompatActivity() {

 override fun onCreate(savedInstanceState: Bundle?) {
 super.onCreate(savedInstanceState)
 setContentView(R.layout.activity_main)

 btn_reading_text.setOnClickListener {
 readFromAsset()
 }
 }

 <strong><span style="color: #008000;">//read file myText.txt from assets folder</span></strong>
 private fun readFromAsset() {
 var string = ""
 try {
 val inputStream = assets.open("myText.txt")
 val size: Int = inputStream.available()
 val buffer = ByteArray(size)
 inputStream.read(buffer).toString()

 string = String(buffer)

 } catch (e: IOException) {
 e.printStackTrace()
 }
 tv_text.text = string
 }
}</pre>
<p>Run the app and click on the button. You can see the file text displayed in the textview.</p>
<p><img class="alignnone wp-image-2749" src="https://c1ctech.com/wp-content/uploads/2021/07/Screenshot_1626949009-576x1024.png" alt="" width="327" height="581" /> <img class="alignnone wp-image-2750" src="https://c1ctech.com/wp-content/uploads/2021/07/Screenshot_1626948979-576x1024.png" alt="" width="323" height="574" />

