android:layout_width="fill_parent"android:layout_..._android context.openfileoutput 单线程判断文件是否下载成功">
当前位置:   article > 正文

android文件的读取和写入文件格式,【Android】android文件的写入与读取---简单的文本读写context.openFileInput() context.openFileOutput...

android context.openfileoutput 单线程判断文件是否下载成功

80e81e942a2d2872d91cfc3b09a6b623.gif

最终效果图,点击save会保存到文件中,点击show会从文件中读取出内容并显示。

bee7b902695efe5e606e95a4959532b5.gif

main.xml

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="请您输入要保存的内容:"

/>

android:id="@+id/addText"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:hint="请您在此处输入文件内容!"

/>

android:id="@+id/addButton"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="save"

/>

android:id="@+id/showButton"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="show"

/>

android:id="@+id/showText"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

/>

activity代码

package cn.com.file;

import java.io.ByteArrayOutputStream;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

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;

import android.widget.TextView;

import android.widget.Toast;

public class FileTest extends Activity {

private EditText editText;

private TextView showTextView;

// 要保存的文件名

private String fileName = "chenzheng_java.txt";

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

// 获取页面中的组件

editText = (EditText) findViewById(R.id.addText);

showTextView = (TextView) findViewById(R.id.showText);

Button addButton = (Button) this.findViewById(R.id.addButton);

Button showButton = (Button) this.findViewById(R.id.showButton);

// 绑定单击事件

addButton.setOnClickListener(listener);

showButton.setOnClickListener(listener);

}

// 声明监听器

private View.OnClickListener listener = new OnClickListener() {

public void onClick(View v) {

Button view = (Button) v;

switch (view.getId()) {

case R.id.addButton:

save();

break;

case R.id.showButton:

read();

break;

}

}

};

/**

*@author chenzheng_Java

*保存用户输入的内容到文件

*/

private void save() {

String content = editText.getText().toString();

try {

/* 根据用户提供的文件名,以及文件的应用模式,打开一个输出流.文件不存系统会为你创建一个的,

* 至于为什么这个地方还有FileNotFoundException抛出,我也比较纳闷。在Context中是这样定义的

* public abstract FileOutputStream openFileOutput(String name, int mode)

* throws FileNotFoundException;

* openFileOutput(String name, int mode);

* 第一个参数,代表文件名称,注意这里的文件名称不能包括任何的/或者/这种分隔符,只能是文件名

* 该文件会被保存在/data/data/应用名称/files/chenzheng_java.txt

* 第二个参数,代表文件的操作模式

* MODE_PRIVATE 私有(只能创建它的应用访问) 重复写入时会文件覆盖

* MODE_APPEND 私有 重复写入时会在文件的末尾进行追加,而不是覆盖掉原来的文件

* MODE_WORLD_READABLE 公用 可读

* MODE_WORLD_WRITEABLE 公用 可读写

* */

FileOutputStream outputStream = openFileOutput(fileName,

Activity.MODE_PRIVATE);

outputStream.write(content.getBytes());

outputStream.flush();

outputStream.close();

Toast.makeText(FileTest.this, "保存成功", Toast.LENGTH_LONG).show();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

/**

* @author chenzheng_java

* 读取刚才用户保存的内容

*/

private void read() {

try {

FileInputStream inputStream = this.openFileInput(fileName);

byte[] bytes = new byte[1024];

ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();

while (inputStream.read(bytes) != -1) {

arrayOutputStream.write(bytes, 0, bytes.length);

}

inputStream.close();

arrayOutputStream.close();

String content = new String(arrayOutputStream.toByteArray());

showTextView.setText(content);

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

}

其他的都为默认。

关于文件保存的路径可以通过ADT携带的File Explorer工具进行查看。如何调出File Explorer工具呢;我们可以通过Windows--showView--others-android下面看到File Explorer。这里是我的一个截图。

20e76c88cc2bb96e4b9add192ec95219.gif

对于这个程序,基本上没什么难点,就是纯粹的java流知识。唯一不同的就是context为我们提供了两个方法来获取输入输出流。简单、方便、快捷啊。

文章转载,版权归原作者所有,尊重版权,支持原创

Android 下载文件及写入SD卡

Android 下载文件及写入SD卡,实例代码 <?xml  version="1.0" encoding="utf-8"?>

INI文件的写入与读取

INI文件的写入与读取 [节名]         '[]中的节名对应此API的第一参数 Name=内容      'Nmae对应此API的第二参数 API的第三参数是没有取到匹配内容时返回的字符串; ...

JavaIO流——简单对文件的写入及读取(三)

已经讲了写入和读取了,那么想要把一个文件的内容复制到另一个文件呢 不说太多,直接见代码 public static void copyFile(String srcFilename, String d ...

java 文件操作 写入和读取(小结一)

参考了这篇博客并优化,谢谢:http://blog.sina.com.cn/s/blog_99201d890101b4le.html 功能:  实现通过两个类完成先写入文件,再读取数据计算显示 pac ...

[转]VC++中对文件的写入和读取

本文转自:http://blog.csdn.net/fanghb_1984/article/details/7425705 本文介绍两种方法对文件进行读取和写入操作:1.采用fstream类:2.采用 ...

第十七章,txt文件的写入和读取数据结合练习(C++)

#include #include int main(int argc, char** argv) { std::string str ...

Java file文件的写入和读取及下载

File文件的写入 一.FileWriter 和BufferedWriter 结合写入文件 FileWriter是字符流写入字符到文件.默认情况下,它会使用新的内容代替文件原有的所有内容,但是,当指定 ...

随机推荐

显示和隐藏Mac下的 隐藏文件

显示:defaults write com.apple.finder AppleShowAllFiles -bool true隐藏:defaults write com.apple.finder Ap ...

JavaScript中的继承模式总结

一.总结: //js中的几种继承 //原型链的问题,包含引用类型的原型属性会被实例共享,子类型无法给超类型传递参数 function SuperType() { this.colors = [&quo ...

String.indexOf()

int indexOf(int ch) 返回指定字符在此字符串中第一次出现处的索引. int indexOf(int ch, int fromIndex) 从指定的索引开始搜索,返回在此字符串中第一次 ...

log4net 动态设定日志文件名

参考文章: http://blog.csdn.net/haoxiaozigang1/article/details/16343303 通过这个篇文章的方法,只能修改文件的路径,文件名并没有修改 参考文 ...

git初探

1 Linux下Git和GitHub环境的搭建 第一步: 安装Git,使用命令 "sudo apt-get install git" 第二步: 到GitHub上创建GitHub帐号 ...

win10 uwp 隐藏实时可视化

新的vs有个功能,实时可视化 但是他会挡我们界面,想要隐藏 点击转到实时可视化,就是点击横线看到,接着就可以看到下面的选项 点击在应用程序中显示运行时,就是不选中 很简单就看到,没有那个 本作品采用知 ...

Java IO基础总结

Java中使用IO(输入输出)来读取和写入,读写设备上的数据.硬盘文件.内存.键盘......,根据数据的走向可分为输入流和输出流,这个走向是以内存为基准的,即往内存中读数据是输入流,从内存中往外写是 ...

Shiro入门 - 通过ini文件进行认证

导入依赖 org.apache.shiroshiro-core ...

《剑指offer》第十八题(删除链表中重复的结点)

// 面试题18(二):删除链表中重复的结点 // 题目:在一个排序的链表中,如何删除重复的结点?例如,在图3.4(a)中重复 // 结点被删除之后,链表如图3.4(b)所示. #include &l ...

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号