android数据写入
来源:互联网 发布:怎么修改淘宝号地区 编辑:程序博客网 时间:2024/06/03 03:39
完成效果:在文本框中输入数字或文字,点击保存按钮然后再按读取按钮,输入内容显示到按钮下方。
在res下新建raw文件,点击读取raw按钮,输入内容显示到按钮下方。
新建一个assets,点击读取assets按钮,输入内容显示到按钮下方。
点击删除按钮,删除输入内容。
布局1:
完成效果:在文本框中输入数字或文字,点击保存按钮然后再按读取按钮,输入内容显示到按钮下方。
在res下新建raw文件,点击读取raw按钮,输入内容显示到按钮下方。
新建一个assets,点击读取assets按钮,输入内容显示到按钮下方。
点击删除按钮,删除输入内容。
布局1:
[html] view plain copy
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context="com.example.administrator.jreduch09.InnerIOActivity">
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="请输入内容"
- android:id="@+id/content"
- />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="保存"
- android:id="@+id/save"
- android:layout_below="@+id/content"
- android:layout_alignParentEnd="true" />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="读取"
- android:id="@+id/read"
- android:layout_below="@+id/content"
- android:layout_alignParentStart="true" />
- <TextView
- android:layout_width="match_parent"
- android:layout_height="50dp"
- android:layout_below="@+id/read"
- android:id="@+id/show"
- />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/delete"
- android:text="删除"
- android:layout_below="@+id/content"
- android:layout_centerHorizontal="true" />
- </RelativeLayout>
[html] view plain copy
- package com.example.administrator.jreduch09;
- import android.os.Bundle;
- import android.os.Environment;
- import android.support.v7.app.AppCompatActivity;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- public class SaveToSdCardActivity extends AppCompatActivity {
- private Button save,read,delete;
- private EditText content;
- private TextView show;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_inner_io);
- save= (Button) findViewById(R.id.save);
- read= (Button) findViewById(R.id.read);
- delete= (Button) findViewById(R.id.delete);
- show= (TextView) findViewById(R.id.show);
- content = (EditText) findViewById(R.id.content);
- save.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- saveFile();
- }
- });
- read.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- show.setText(readFile());
- }
- });
- delete.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- removeFile();
- }
- });
- }
- //保存文件到SDcard中
- public void saveFile(){
- FileOutputStream fos=null;
- String state= Environment.getExternalStorageState();
- //获取SD卡状态
- if(!state.equals(Environment.MEDIA_MOUNTED)){
- //判断SD卡是否就绪
- Toast.makeText(getBaseContext(),"请检查SD卡",Toast.LENGTH_SHORT).show();
- return;
- }
- File file=Environment.getExternalStorageDirectory();
- //取得SD卡根目录
- try {
- Log.d("===SD卡根目录:",file.getCanonicalPath()+"sd.txt");
- fos=new FileOutputStream(file.getCanonicalPath()+"/sd.txt",true);
- String str=content.getText().toString();
- fos.write(str.getBytes());
- Toast.makeText(getBaseContext(),"保存成功",Toast.LENGTH_SHORT).show();
- } catch (IOException e) {
- e.printStackTrace();
- }finally {
- if(fos!=null){
- try {
- fos.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- public String readFile(){
- BufferedReader reader =null;
- FileInputStream fis=null;
- //从SD卡读取文件
- StringBuilder sdb=new StringBuilder();
- String state=Environment.getExternalStorageState();
- if(!state.equals(Environment.MEDIA_MOUNTED)){
- Toast.makeText(getBaseContext(),"SD卡未就绪",Toast.LENGTH_SHORT).show();
- return "";
- }
- File root=Environment.getExternalStorageDirectory();
- try {
- fis=new FileInputStream(root+"/sd.txt");
- reader=new BufferedReader(new InputStreamReader(fis));
- String row="";
- while ((row=reader.readLine())!=null){
- sdb.append(row);
- }
- } catch (FileNotFoundException e) {
- Toast.makeText(this,"文件不存在",Toast.LENGTH_SHORT).show();
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }finally {
- if(reader!=null){
- try {
- reader.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- return sdb.toString();
- }
- public void removeFile(){
- String state=Environment.getExternalStorageState();
- if(!state.equals(Environment.MEDIA_MOUNTED)){
- Toast.makeText(getBaseContext(),"SD卡未就绪",Toast.LENGTH_SHORT).show();
- return;
- }
- File file=Environment.getExternalStorageDirectory();
- File myFile=new File(file,"sd.txt");
- if(myFile.exists()){
- myFile.delete();
- Toast.makeText(this,"文件已删除",Toast.LENGTH_SHORT).show();
- }else{
- Toast.makeText(this,"文件不存在",Toast.LENGTH_SHORT).show();
- }
- }
- }
布局2:
[html] view plain copy
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context="com.example.administrator.jreduch09.ReadRawAndAssetsActivity">
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/raw"
- android:text="读取raw"
- />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/assets"
- android:text="读取assets"
- android:layout_alignParentTop="true"
- android:layout_alignParentEnd="true" />
- <TextView
- android:layout_width="match_parent"
- android:layout_height="50dp"
- android:id="@+id/show"
- android:layout_below="@+id/assets"
- />
- </RelativeLayout>
[html] view plain copy
- package com.example.administrator.jreduch09;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- public class ReadRawAndAssetsActivity extends AppCompatActivity {
- private TextView show;
- private Button raw,assets;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_read_raw_and_assets);
- raw= (Button) findViewById(R.id.raw);
- assets= (Button) findViewById(R.id.assets);
- show= (TextView) findViewById(R.id.show);
- raw.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- show.setText(readRaw());
- }
- });
- assets.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- show.setText(readAssets());
- }
- });
- }
- public String readRaw(){
- StringBuilder sbd=new StringBuilder();
- InputStream is=null;
- BufferedReader reader=null;
- is=getResources().openRawResource(R.raw.settings);
- String row="";
- try {
- reader=new BufferedReader(new InputStreamReader(is));
- while ((row=reader.readLine())!=null) {
- sbd.append(row);
- }
- } catch (IOException e) {
- e.printStackTrace();
- }finally {
- if(reader!=null){
- try {
- reader.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- return sbd.toString();
- }
- public String readAssets(){
- StringBuilder sdb=new StringBuilder();
- BufferedReader reader=null;
- try {
- InputStream is=getResources().getAssets().open("city");
- reader=new BufferedReader(new InputStreamReader(is));
- String row="";
- while ((row=reader.readLine())!=null){
- sdb.append(row);
- }
- } catch (IOException e) {
- e.printStackTrace();
- }finally {
- if(reader!=null){
- try {
- reader.close();
- } catch (IOException e) {
- }
- }
- }
- return sdb.toString();
- }
- }
1.大家好我是VAE。2.那是一条神奇的天路啊啊啊。3.我愿意为你,我愿意为你。4.如果你对天空向往。assets中的city的内容:
北京上海广州
效果展示:
阅读全文
0 0
- android数据写入
- android的SDCard数据写入
- android sdcard 写入数据权限的问题
- android 向SD卡写入数据
- excel在android读取,写入,更新数据
- Android录音程序之写入数据
- Android往sdcard写入读取数据
- android 大量数据写入数据库的优化
- android 向SD卡写入数据
- 往Android SDCard中写入数据
- Android 数据的读取与写入
- Android 数据的读取与写入2
- Android 将数据写入到word中
- Android基础之写入、读取数据
- android实现创建文件并写入数据
- Android创建文件夹及文件并写入数据
- Android创建文件夹及文件并写入数据
- android创建文件夹以及向文件写入数据
- 2014年第五届蓝桥杯C/C++程序设计本科B组决赛 生物芯片(编程大题)
- 封装 继承 抽象类和接口的理解
- 终端命令别名设置
- poj 2155 Matrix
- matlab 阶乘函数
- android数据写入
- JAVA使用JDBC连接MySQL数据库
- 如何在VS中进行单元测试
- Maven 项目构建错误解决 之 Type Failure to transfer org.codehaus.plexus:plexus-io:pom:2.0.2
- Linux目录结构详细介绍
- 卡特兰数的详解
- c语言之交换一个指针数组的值
- Java面试题
- 项目管理总结