编写一下Android界面的项目
使用默认的Android清单文件
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.itheima28.writedata" android:versionCode="1" android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" />
<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.itheima28.writedata.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
</manifest> |
Android布局文件
<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:orientation="vertical" tools:context=".MainActivity">
<Button android:id="@+id/btn_read_private" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="读私有文件" />
<Button android:id="@+id/btn_write_private" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="写私有文件" />
<Button android:id="@+id/btn_read_readable" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="读可读文件" />
<Button android:id="@+id/btn_write_readable" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="写可读文件" />
<Button android:id="@+id/btn_read_writeable" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="读可写文件" />
<Button android:id="@+id/btn_write_writeable" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="写可写文件" />
<Button android:id="@+id/btn_read_readable_writeable" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="读可读可写文件" />
<Button android:id="@+id/btn_write_readable_writeable" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="写可读可写文件" />
</LinearLayout> |
4 Android中的写文本文件的代码
package com.itheima28.writedata;
import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStreamReader;
import android.content.Context; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.View; import android.view.View.OnClickListener; import android.widget.Toast;
/** * 读写文件 * 注意可以将写文件和写文件的两个功能分别写到不同的项目中进行测试 * @author toto */ public class MainActivity extends ActionBarActivity implements OnClickListener{ //这个路径是文件所在路径 private String basicPath = "/data/data/com.itheima28.writedata/files/";
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
// 写数据 // 私有文件 writeToLocal("private.txt", Context.MODE_PRIVATE); // 可读文件 writeToLocal("readable.txt", Context.MODE_WORLD_READABLE); // 可写文件 writeToLocal("writeable.txt", Context.MODE_WORLD_WRITEABLE); // 可读可写文件 writeToLocal("readable_writeable.txt", Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE);
findViewById(R.id.btn_read_private).setOnClickListener(this); findViewById(R.id.btn_write_private).setOnClickListener(this);
findViewById(R.id.btn_read_readable).setOnClickListener(this); findViewById(R.id.btn_write_readable).setOnClickListener(this);
findViewById(R.id.btn_read_writeable).setOnClickListener(this); findViewById(R.id.btn_write_writeable).setOnClickListener(this);
findViewById(R.id.btn_read_readable_writeable).setOnClickListener(this); findViewById(R.id.btn_write_readable_writeable).setOnClickListener(this); }
/** * 写文件 * @param fileName * @param mode */ private void writeToLocal(String fileName, int mode) { try { FileOutputStream fos = openFileOutput(fileName, mode); fos.write(("第一个程序写的数据:" + fileName).getBytes()); fos.flush(); fos.close(); } catch (Exception e) { e.printStackTrace(); } }
/** * 哪一个控件被点击, v对象就代表被点击的对象 */ @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_read_private: readFile("private.txt"); break; case R.id.btn_write_private: writeFile("private.txt"); break; case R.id.btn_read_readable: readFile("readable.txt"); break; case R.id.btn_write_readable: writeFile("readable.txt"); break; case R.id.btn_read_writeable: readFile("writeable.txt"); break; case R.id.btn_write_writeable: writeFile("writeable.txt"); break; case R.id.btn_read_readable_writeable: readFile("readable_writeable.txt"); break; case R.id.btn_write_readable_writeable: writeFile("readable_writeable.txt"); break; default: break; } }
/** * 读文件 * @param fileName */ private void readFile(String fileName) { try { String path = basicPath + fileName;
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(path))); String text = reader.readLine(); reader.close(); Toast.makeText(this, "读取成功: " + text, 0).show(); } catch (Exception e) { e.printStackTrace(); Toast.makeText(this, "读取失败: " + fileName, 0).show(); } }
/** * 写文件 * @param fileName */ private void writeFile(String fileName) { try { String path = basicPath + fileName;
FileOutputStream fos = new FileOutputStream(path);
fos.write("哈哈, 被我给黑了".getBytes());
fos.flush();
fos.close(); Toast.makeText(this, "写入成功: " + fileName, 0).show(); } catch (Exception e) { e.printStackTrace(); Toast.makeText(this, "写入失败: " + fileName, 0).show(); } } } |