ساخت فایل متنی در برنامه نویسی اندروید
سلام دوستان در این سری از آموزش برنامه نویسی اندروید به آموزش ساخت فایل متنی در برنامه نویسی اندروید می پردازیم برای اینکه یک فایل متنی (Text) ایجاد کنیم لازم است با کلاس File و FileWriter کار کنیم در ادامه با ما همراه باشید تا نحوه ساخت فایل متنی در برنامه نویسی اندروید را یاد گیرید.
برای اینکه فایل متنی ایجاد کنیم (ساخت فایل متنی در برنامه نویسی اندروید) لازم است تا RunTimePermission را به پروژه اضافه کنیم برای اینکه کد شلوغ نشود از آموزش های قبلی جهت اضافه کردن RunTimePermission اقدام کنید (در صورتی که از این آموزش استفاده نکنید برنامه کرش می کند) باید دسترسی استفاده از حافظه را ( WRITE_EXTERNAL_STORAGE ) را اضافه کنید.
آموزش های دسترسی هنگام اجرا
دسترسی AndroidManifest.xml را فراموش نکنید !
1 | <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> |
بعد از اینکه دسترسی را اضافه کردید در layout اصلی که در اینجا نام آن activity_main.xml است کدهای زیر را قرار دهید.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | <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:background="#222222" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="10dp" android:text="Programchi.ir :D" android:textColor="#FF8866" android:textSize="25sp" android:textStyle="bold" android:typeface="monospace" /> <EditText android:id="@+id/contenttxt" android:layout_width="match_parent" android:layout_height="300dp" android:layout_gravity="center" android:layout_marginBottom="10dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:background="#ffffff" android:textColor="#000000" android:layout_marginTop="10dp" android:gravity="top|left" android:scrollHorizontally="false" android:singleLine="false" android:text="Hi" /> <Button android:id="@+id/exportbtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="10dp" android:background="#FF6644" android:gravity="center" android:paddingBottom="10dp" android:paddingLeft="10dp" android:paddingRight="10dp" android:paddingTop="10dp" android:text="EXPORT TO SDCARD" android:textColor="#FFFFFF" android:textSize="20sp" /> <TextView android:id="@+id/resulttxt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="10dp" android:text="" android:textColor="#ffffff" android:textSize="20sp" android:textStyle="bold" android:typeface="monospace" /> </LinearLayout> |
در بالا یک EditText برای دریافت متن قرار گرفته است که محتویات فایل متنی را شامل می شود.
یک دکمه برای ذخیره داده یا انجام عمل قرار گرفته است و TextView هم برای نمایش نام فایل ساخته شده خواهد بود.
در بخش MainActivity.java کدهای زیر را قرار دهید.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | package ir.programchi; /** * Created by JFP on 17-Jun-15. */ import android.app.Activity; import android.os.Bundle; import android.os.Environment; import android.text.format.DateFormat; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import java.io.File; import java.io.FileWriter; import java.io.IOException; public class NotePad extends Activity { EditText ed; Button btn; TextView result; String h; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ed = (EditText) findViewById(R.id.contenttxt); result = (TextView) findViewById(R.id.resulttxt); btn = (Button) findViewById(R.id.exportbtn); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { try { h = DateFormat.format("MM-dd-yyyyy-h-mmssaa", System.currentTimeMillis()).toString(); File root = new File(Environment.getExternalStorageDirectory(), "Notes"); // if external memory exists and folder with name Notes if (!root.exists()) { root.mkdirs(); // this will create folder. } File filepath = new File(root, h + ".txt"); // file path to save FileWriter writer = new FileWriter(filepath); writer.append(ed.getText().toString()); writer.flush(); writer.close(); String m = "File generated with name " + h + ".txt"; result.setText(m); } catch (IOException e) { e.printStackTrace(); result.setText(e.getMessage().toString()); } } }); } } |
در بالا با استفاده از DateFormat یک نام برای فایل txt ایجاد کردیم که برابر با تاریخ و ساعت همان روز خواهد بود فایل ساخته شده در پوشه Notes خواهد بود.
این آموزش هم به پایان رسید.
موفق و پیروز باشید.
یک پاسخ به “ساخت فایل متنی در برنامه نویسی اندروید”