我们可以将一些数据直接以文件的形式保存在设备中。例如,一些文本文件,PDF文件,音频文件,视频文件和图片等。Android提供了文件读写的方法。
通过Context.openFileInput()方法获得标准JAVA文件输入流(FileInputStream),通过Context.openFileOutput()方法获得标准的java文件输出流(FileOutputStream)。使用Resources.openRawResource(R.raw.DataFile)方法返回InputStream。
接下来通过一个小例子来掩饰File读写文件。创建一个Activity,在其中放置两个EditText和连个Button,第一个EditText和Button将EditText的内容写到文件temp.txt文件中。第二个EditText和Button将内容从文件temp.txt中读取出来显示。问价被默认保存在/data/data/package/files下面。
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText
android:id="@+id/edit1"
android:layout_width="fill_parent"
android:layout_height="60dip"
/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="写入数据"
/>
<EditText
android:id="@+id/edit2"
android:layout_width="fill_parent"
android:layout_height="60dip"
/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="读取数据"
/>
</LinearLayout>
FileTest.java
package org.hualang.file;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class FileTest extends Activity {
/** Called when the activity is first created. */
private Button read,write;
private EditText text1,text2;
private static final String FILE_NAME="temp.txt";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
write=(Button)findViewById(R.id.button1);
read=(Button)findViewById(R.id.button2);
text1=(EditText)findViewById(R.id.edit1);
text2=(EditText)findViewById(R.id.edit2);
//设置按钮事件监听器
write.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
//写内容
write(text1.getText().toString());
}
});
read.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
//读内容
text2.setText(read());
}
});
}
//读方法
private String read()
{
try
{
//实例化文件输入流对象
FileInputStream fis=openFileInput(FILE_NAME);
//定义缓存数组
byte[] buffer=new byte[fis.available()];
//读取到缓冲区
fis.read(buffer);
return new String(buffer);
}catch(Exception e)
{
e.printStackTrace();
}
return null;
}
//写方法
private void write(String content)
{
try
{
//实例化文件输入流
FileOutputStream fos=openFileOutput(FILE_NAME,MODE_APPEND);
//写内容
fos.write(content.getBytes());
//关闭文件流
fos.close();
}catch(Exception e)
{
e.printStackTrace();
}
}
}
运行结果如下:
- 大小: 13.2 KB
- 大小: 13.8 KB
分享到:
相关推荐
Android-FileBrowser-FilePicker一个Android文件浏览和选择控件
Android-android-file-transfer-linux.zip,Android Linux文件传输,安卓系统是谷歌在2008年设计和制造的。操作系统主要写在爪哇,C和C 的核心组件。它是在linux内核之上构建的,具有安全性优势。
android-sdk\platforms\android-21
android linux交叉编译工具aarch64-linux-android-4.9版本!需要的可以下载!
Multipart file uploads with no additional third party libraries Tiny size overhead to your application, only 60kb for everything Automatic smart request retries optimized for spotty mobile connections...
宝利通rpm-android-phone-release-3.7-64806移动应用程序是一个易于使用的视频系统,提供优质的音频和视频体验。与专有应用程序不同,RealPresence移动应用程序是基于标准的,可扩展您组织的视频网络,使您在任何...
File Loader Android library for downloading, saving/caching and retrieving any type of files ( image, video, pdf, apk etc ) easily. Download Gradle: repositories { mavenCentral() maven { url '...
all-file-to-all-file-converter.exe
适用于Linux的Android文件传输(FreeBSD和Mac OS X!... MacOSX Homebrew: brew cask install whoozle-android-file-transfer或brew cask install whoozle-android-file-transfer-nightly 支持我 如果您想帮助我进
Android-Skin 介绍 Android-Skin:极简单的android换肤框架,无缝支持第三方控件,一行代码即可换肤,集成成本极低, 提供丰富的API支持拓展,对业务代码几乎无入侵。 AndroidSkin.getInstance().saveSkinAndApply...
android-ndk-r10e-darwin-x86_64 第二个卷 cat x* > file.zip 第一卷地址 http://download.csdn.net/detail/hejisan/9803776 解压 http://blog.csdn.net/hejisan/article/details/53725955
替换sdk路径下/platforms/...解决替换as后报错:Failed to transform file 'android.jar' to match attributes {artifactType=android-mockable-jar, returnDefaultValues=false} using transform MockableJarTransform
替换sdk路径下/platforms/...解决替换后报错:Failed to transform file 'android.jar' to match attributes {artifactType=android-mockable-jar, returnDefaultValues=false} using transform MockableJarTransform
Android的文件浏览器/文件选择器,可以集成到应用程序中,从内部/外部存储中浏览/选择文件。
android-location-tracker Android Simple Location Tracker is an Android library that helps you get user location with a object named LocationTracker Installation Add this to your build.gradle file ...
android-gif-drawable Views and Drawable for animated GIFs in Android. Overview Bundled GIFLib via JNI is used to render frames. This way should be more efficient than WebView or Movie classes. ...
Android-PdfMyXml convert your android XML layouts into PDF document, works on all versions of Android. Dependencies pdfjet How to use Option 1: Simply fork or download the project, you can also ...
Android-FileDownloader.zip,多任务、多线程(多连接)、断点恢复、高并发、简单易用、单进程/非单进程,安卓系统是谷歌在2008年设计和制造的。操作系统主要写在爪哇,C和C 的核心组件。它是在linux内核之上构建的,...
github下载的android 内核编译器。arm64位cpu版本。 google无法下载,github超级慢
Android-Timeline-View Android timeline to display horizontal sliding cards in recycler view, group by Day, Month or Year. Demo Video Apps using the library: Gourmet (http://thegourmet.app) Play...