本示例演示了如何在 Android 应用中实现内部文件读写、外部文件读写以及 XML 序列化功能。

  1. 在布局文件中添加两个 EditText 和五个 Button:
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="输入内容1"/>

    <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="输入内容2"/>

    <Button
        android:id="@+id/btnInternalRead"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="内部文件读取"/>

    <Button
        android:id="@+id/btnInternalWrite"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="内部文件写入"/>

    <Button
        android:id="@+id/btnExternalRead"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="外部文件读取"/>

    <Button
        android:id="@+id/btnExternalWrite"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="外部文件写入"/>

    <Button
        android:id="@+id/btnXml"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="XML 序列化"/>

</LinearLayout>
  1. 在 Activity 中添加按钮点击事件处理:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText editText1, editText2;

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

        editText1 = findViewById(R.id.editText1);
        editText2 = findViewById(R.id.editText2);

        Button btnInternalRead = findViewById(R.id.btnInternalRead);
        Button btnInternalWrite = findViewById(R.id.btnInternalWrite);
        Button btnExternalRead = findViewById(R.id.btnExternalRead);
        Button btnExternalWrite = findViewById(R.id.btnExternalWrite);
        Button btnXml = findViewById(R.id.btnXml);

        btnInternalRead.setOnClickListener(this);
        btnInternalWrite.setOnClickListener(this);
        btnExternalRead.setOnClickListener(this);
        btnExternalWrite.setOnClickListener(this);
        btnXml.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnInternalRead:
                readInternalFile();
                break;
            case R.id.btnInternalWrite:
                writeInternalFile();
                break;
            case R.id.btnExternalRead:
                readExternalFile();
                break;
            case R.id.btnExternalWrite:
                writeExternalFile();
                break;
            case R.id.btnXml:
                xmlSerialize();
                break;
            default:
                break;
        }
    }

    private void readInternalFile() {
        try {
            FileInputStream fis = openFileInput('my_file.txt');
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            String content = new String(buffer);
            editText1.setText(content);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void writeInternalFile() {
        String content = editText1.getText().toString();
        try {
            FileOutputStream fos = openFileOutput('my_file.txt', MODE_PRIVATE);
            fos.write(content.getBytes());
            fos.close();
            Toast.makeText(this, '文件写入成功', Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void readExternalFile() {
        File file = new File(getExternalFilesDir(null), 'my_file.txt');
        try {
            FileInputStream fis = new FileInputStream(file);
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            String content = new String(buffer);
            editText1.setText(content);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void writeExternalFile() {
        String content = editText1.getText().toString();
        File file = new File(getExternalFilesDir(null), 'my_file.txt');
        try {
            FileOutputStream fos = new FileOutputStream(file, true);
            fos.write(content.getBytes());
            fos.close();
            Toast.makeText(this, '文件写入成功', Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void xmlSerialize() {
        String name = editText1.getText().toString();
        String age = editText2.getText().toString();
        try {
            FileOutputStream fos = openFileOutput('my_xml.xml', MODE_PRIVATE);
            XmlSerializer serializer = Xml.newSerializer();
            serializer.setOutput(fos, 'UTF-8');
            serializer.startDocument('UTF-8', true);
            serializer.startTag(null, 'person');
            serializer.startTag(null, 'name');
            serializer.text(name);
            serializer.endTag(null, 'name');
            serializer.startTag(null, 'age');
            serializer.text(age);
            serializer.endTag(null, 'age');
            serializer.endTag(null, 'person');
            serializer.endDocument();
            fos.close();
            Toast.makeText(this, 'XML 序列化成功', Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

以上代码实现了内部文件读写、外部文件读写和 XML 序列化功能,可以根据实际需求进行修改和优化。

Android 文件读写和 XML 序列化示例:内部文件、外部文件、XML

原文地址: https://www.cveoy.top/t/topic/nvxm 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录