设为首页 加入收藏

TOP

Android数据存取之file (文件存取操作)(一)
2014-11-24 07:29:26 来源: 作者: 【 】 浏览:4
Tags:Android 数据 存取 file 文件 操作

总体按照MVC设计模式


1、javabean (M)


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" >
http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/filename"
android:id="@+id/filenameLabel"
android:textSize="20px"
android:paddingTop="10px"
/>
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"
/>

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/content"
/>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:lines="6"
android:maxLines="10"
android:id="@+id/content"
/>
http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
android:layout_width="80px"
android:layout_height="wrap_content"
android:text="@string/save"
android:id="@+id/save"
/>
android:layout_width="80px"
android:layout_height="wrap_content"
android:text="@string/read"
android:layout_toRightOf="@id/save"
android:id="@+id/read"
/>

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/readContent"
/>


3.Activity (C)


package com.lybeen.android;


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


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
imp

首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Java中Date的使用 下一篇Android toy program 之打电话发..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Redis 分布式锁全解 (2025-12-25 17:19:51)
·SpringBoot 整合 Red (2025-12-25 17:19:48)
·MongoDB 索引 - 菜鸟 (2025-12-25 17:19:45)
·What Is Linux (2025-12-25 16:57:17)
·Linux小白必备:超全 (2025-12-25 16:57:14)