خواندن یک فایل text (با پسوند txt) موجود در پوشه assets و ذخیره متن درون آن در متغیری از جنس رشته (S
فرض کنید یک فایل با نام myFile.txt را در پوشه assets از پروژه اندروید قرار داده ایم که متن زیر (یک متن دلخواه) در آن ذخیره شده است :
1http://berozbooks.blogfa.com/
2http://berozbooks.blogfa.com/
ابتدا یک روش (method) به صورت زیر تعریف می کنیم :
public String ReadFromfile(String fileName, Context context) {
StringBuilder returnString = new StringBuilder();
InputStream fIn = null;
InputStreamReader isr = null;
BufferedReader input = null;
try {
fIn = context.getResources().getAssets()
.open(fileName, Context.MODE_WORLD_READABLE);
isr = new InputStreamReader(fIn);
input = new BufferedReader(isr);
String line = "";
while ((line = input.readLine()) != null) {
returnString.append(line);
}
} catch (Exception e) {
e.getMessage();
} finally {
try {
if (isr != null)
isr.close();
if (fIn != null)
fIn.close();
if (input != null)
input.close();
} catch (Exception e2) {
e2.getMessage();
}
}
return returnString.toString();
}
سپس برای خواندن فایل myFile.txt و ذخیره متن آن در یک متغیر از جنس رشته (String)، کد زیر را اجرا می کنیم :
String str = ReadFromfile("myFile.txt", this);
در کد بالا فرض کرده ایم که کد در یک Activity اجرا می شود و this نیز به Context مربوط به Activity اشاره می کند.
به عنوان مثال، کدهایی که در Activity می نویسیم، مشابه کد زیر می باشد :
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String str = ReadFromfile("myFile.txt", this);
}
public String ReadFromfile(String fileName, Context context) {
StringBuilder returnString = new StringBuilder();
InputStream fIn = null;
InputStreamReader isr = null;
BufferedReader input = null;
try {
fIn = context.getResources().getAssets()
.open(fileName, Context.MODE_WORLD_READABLE);
isr = new InputStreamReader(fIn);
input = new BufferedReader(isr);
String line = "";
while ((line = input.readLine()) != null) {
returnString.append(line);
}
} catch (Exception e) {
e.getMessage();
} finally {
try {
if (isr != null)
isr.close();
if (fIn != null)
fIn.close();
if (input != null)
input.close();
} catch (Exception e2) {
e2.getMessage();
}
}
return returnString.toString();
}
}
متن درون فایل، در رشته ای با نام str ذخیره می شود.
نکته
با اجرای کد بالا، متن درون رشته (String) به صورت زیر خواهد بود (مثلا با نمایش آن در یک TextView) :
1http://berozbooks.blogfa.com/2http://berozbooks.blogfa.com/
مشاهده می کنید که رفتن به خط جدید (line break) در رشته ذکر نشده است. بنابراین چنانچه بخواهیم رفتن به خط جدید (line break) نیز در رشته ذکر شود، باید کدهای روش (method) را اندکی تغییر دهیم :
public String ReadFromfile(String fileName, Context context) {
StringBuilder returnString = new StringBuilder();
InputStream fIn = null;
InputStreamReader isr = null;
BufferedReader input = null;
try {
fIn = context.getResources().getAssets()
.open(fileName, Context.MODE_WORLD_READABLE);
isr = new InputStreamReader(fIn);
input = new BufferedReader(isr);
String line = "";
boolean firstRow = true;
while ((line = input.readLine()) != null) {
if(firstRow){ // first row
returnString.append(line);
firstRow = false;
}else{
returnString.append("\n"+line);
}
}
} catch (Exception e) {
e.getMessage();
} finally {
try {
if (isr != null)
isr.close();
if (fIn != null)
fIn.close();
if (input != null)
input.close();
} catch (Exception e2) {
e2.getMessage();
}
}
return returnString.toString();
}
بنابراین این بار اگر متن درون رشته را در خروجی (مثلا در یک TextView) نمایش بدهیم، نتیجه به صورت زیر خواهد بود :
http://berozbooks.blogfa.com/
http://berozbooks.blogfa.com/








الهی !