Android数据存取之file (文件存取操作)

总体按照MVC设计模式

1、javabean    (M)

package com.lybeen.Android;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class FileService {
    /**
     * 把数据写入文件中
     * @param outputStream
     * @param content
     * @throws IOException
     */
    public static void save(OutputStream outputStream , String content) throws IOException{
        outputStream.write(content.getBytes());
        outputStream.close();
    }
    /**
     * 从文件中读取数据
     * @param inputStream
     * @return
     * @throws IOException
     */
    public static String read(InputStream inputStream) throws IOException{
        int len=-1;
        byte[] b = new byte[1024];
        ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
        while( ( len = inputStream.read(b) ) != -1){
            arrayOutputStream.write(b, 0, len);
        };
//        return arrayOutputStream.toByteArray().toString();
        return new String(arrayOutputStream.toByteArray());
    }
}

2.界面设计   (V)


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <RelativeLayout xmlns:android=""
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
      >
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/filename"
            android:id="@+id/filenameLabel"
            android:textSize="20px"
            android:paddingTop="10px"
        />
        <EditText
            android:layout_width="200px"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/filenameLabel"
            android:layout_alignTop="@id/filenameLabel"
            android:layout_marginLeft="10px"
            android:text="lybeen.txt"
            android:id="@+id/filename"
        />
    </RelativeLayout>
<TextView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/content"
    />
<EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:lines="6"
    android:maxLines="10"
    android:id="@+id/content"
 />
    <RelativeLayout xmlns:android=""
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
          >
            <Button
                android:layout_width="80px"
                android:layout_height="wrap_content"
                android:text="@string/save"
                android:id="@+id/save"
            />
            <Button
                android:layout_width="80px"
                android:layout_height="wrap_content"
                android:text="@string/read"
                android:layout_toRightOf="@id/save"
                android:id="@+id/read"
            />
    </RelativeLayout>
<TextView 
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:id="@+id/readContent"
   />
</LinearLayout>

3.Activity (C)

package com.lybeen.android;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwsggy.html