ورود

View Full Version : خواندن فایل txt در اندروید



nargesjooon
دوشنبه 18 آبان 1394, 20:10 عصر
سلام :لبخندساده:
بنده چون کار با دیتابیس خارجی رو بلد نیستم برا همین خاطر
مطالبم رو در فایل txt می نویسم.:لبخند:
حالا مشکل اینجا ک من میخام بعضی از تکست ویو های برنامه م رو ب این فایل های متنی ارجاع بدم
اما هرکاری میکنم ب مشکل بر میخورم:ناراحت:
شما دوستان عزیز می تونید ی کد بم معرفی کنید؟:افسرده:

slr560
دوشنبه 18 آبان 1394, 20:31 عصر
//Find the directory for the SD Card using the API //*Don't* hardcode "/sdcard"
File sdcard = Environment.getExternalStorageDirectory();

//Get the text file
File file = new File(sdcard,"file.txt");

//Read text from file
StringBuilder text = new StringBuilder();

try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;

while ((line = br.readLine()) != null) {
text.append(line);
text.append('\n');
}
br.close();
}
catch (IOException e) {
//You'll need to add proper error handling here
}

//Find the view by its id
TextView tv = (TextView)findViewById(R.id.text_view);

//Set the text
tv.setText(text.toString());

nargesjooon
دوشنبه 18 آبان 1394, 20:57 عصر
//Find the directory for the SD Card using the API //*Don't* hardcode "/sdcard"
File sdcard = Environment.getExternalStorageDirectory();

//Get the text file
File file = new File(sdcard,"file.txt");

//Read text from file
StringBuilder text = new StringBuilder();

try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;

while ((line = br.readLine()) != null) {
text.append(line);
text.append('\n');
}
br.close();
}
catch (IOException e) {
//You'll need to add proper error handling here
}

//Find the view by its id
TextView tv = (TextView)findViewById(R.id.text_view);

//Set the text
tv.setText(text.toString());
خوب من فایل تکست م رو در پوشه ای بنام raw ذخیره کردم ن در sdcard:ناراحت:
حالا باید چیکار کنم؟:افسرده:

msroid
سه شنبه 19 آبان 1394, 00:43 صبح
خوب من فایل تکست م رو در پوشه ای بنام raw ذخیره کردم ن در sdcard:ناراحت:
حالا باید چیکار کنم؟:افسرده:


اینجوری میتونین عمل کنین:

//reading file by using inputStream


InputStream inputStream = getResources().openRawResource(R.raw.yourFileName) ;
InputStreamReader inputreader = new InputStreamReader(inputStream);
BufferedReader bufferedreader = new BufferedReader(inputreader);
String line;
StringBuilder myText = new StringBuilder();
try
{
while (( line = bufferedreader.readLine()) != null)
{
myText.append(line + "\n");
}
}
catch (IOException e)
{
e.printStackTrace();
}
textView.setText(myText);