package com.sdmadik;
import java.io.*;
import android.app.Activity;
import android.os.Bundle;
import android.view.*;
import android.view.View.OnClickListener;
import android.widget.*; public class FileUse extends Activity {
// GUI controls
EditText txtData;
Button btnWriteSDFile;
Button btnReadSDFile;
Button btnClearScreen;
Button btnClose; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// bind GUI elements with local controls
txtData = (EditText) findViewById(R.id.txtData);
txtData.setHint("Enter some lines of data here...");
//setHint寫法與setText相似。 btnWriteSDFile = (Button) findViewById(R.id.btnWriteSDFile);
btnWriteSDFile.setOnClickListener(new OnClickListener() { public void onClick(View v) {
// write on SD card file data in the text box
try {
File myFile = new File("/sdcard/mysdfile.txt");
myFile.createNewFile();
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter =
new OutputStreamWriter(fOut);
myOutWriter.append(txtData.getText());
myOutWriter.close();
fOut.close();
Toast.makeText(getBaseContext(),
"Done writing SD 'mysdfile.txt'",
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(),
Toast.LENGTH_SHORT).show();
}
}// onClick
}); // btnWriteSDFile btnReadSDFile = (Button) findViewById(R.id.btnReadSDFile);
btnReadSDFile.setOnClickListener(new OnClickListener() { public void onClick(View v) {
// write on SD card file data in the text box
try {
File myFile = new File("/sdcard/mysdfile.txt");
FileInputStream fIn = new FileInputStream(myFile);
BufferedReader myReader = new BufferedReader(
new InputStreamReader(fIn));
String aDataRow = "";
String aBuffer = "";
while ((aDataRow = myReader.readLine()) != null) {
aBuffer += aDataRow + "\n";
}
txtData.setText(aBuffer);
myReader.close();
Toast.makeText(getBaseContext(),
"Done reading SD 'mysdfile.txt'",
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(),
Toast.LENGTH_SHORT).show();
}
}// onClick
}); // btnReadSDFile btnClearScreen = (Button) findViewById(R.id.btnClearScreen);
btnClearScreen.setOnClickListener(new OnClickListener() { public void onClick(View v) {
// clear text box
txtData.setText("");
}
}); // btnClearScreen btnClose = (Button) findViewById(R.id.btnClose);
btnClose.setOnClickListener(new OnClickListener() { public void onClick(View v) {
// clear text box
finish();
}
}); // btnClose }// onCreate }// AndSDcard
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/widget28"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ff0000ff"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<EditText
android:id="@+id/txtData"
android:layout_width="fill_parent"
android:layout_height="180px"
android:textSize="18sp" /> <Button
android:id="@+id/btnWriteSDFile"
android:layout_width="143px"
android:layout_height="44px"
android:text="1. Write SD File" /> <Button
android:id="@+id/btnClearScreen"
android:layout_width="141px"
android:layout_height="42px"
android:text="2. Clear Screen" /> <Button
android:id="@+id/btnReadSDFile"
android:layout_width="140px"
android:layout_height="42px"
android:text="3. Read SD File" /> <Button
android:id="@+id/btnClose"
android:layout_width="141px"
android:layout_height="43px"
android:text="4. Close" /> </LinearLayout>

安卓初級教程(2):SD創建file,儲存與讀寫的方法(1)的更多相关文章

  1. 安卓初級教程(1):@Database(1)

    package com.example.android.db01; import android.app.Activity; import android.content.ContentValues; ...

  2. 安卓初級教程(4):sqlite建立資料庫

    2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 ...

  3. 安卓初級教程(5):TabHost的思考

    package com.myhost; import android.os.Bundle; import android.view.LayoutInflater; import android.wid ...

  4. 安卓初級教程(3):ContentProvider的運用原理

    package com.example.android.provider; import java.util.ArrayList; import java.util.HashMap; import j ...

  5. 安卓中級教程(10):@InjectView

    package com.example.android.db01; import android.app.Activity; import android.content.ContentValues; ...

  6. 安卓中級教程(3):ScrollView

    以上是scrollview的圖例,可見srollview是一種滑動功能的控件,亦是非常常見的控件. 一般寫法如下: package com.mycompany.viewscroller; import ...

  7. 安卓中級教程(6):annotation的基本用法

    package com.example.ele_me.activity; import android.annotation.SuppressLint; import android.app.Acti ...

  8. 安卓中級教程(1):@InjectView

    package com.mycompany.hungry; import android.annotation.SuppressLint; import android.app.Activity; i ...

  9. 安卓中級教程(11):深入研究餓了麼的各個java檔運作關係(1)

    package com.example.ele_me.activity; import android.annotation.SuppressLint; import android.app.Acti ...

随机推荐

  1. javascript中的冒泡排序

    冒泡排序:就是将一个数组中的元素按照从大到小或者从小到大的顺序进行排列. var array=[9,8,7,6,5,4,3,2,1]; 第一轮比较:8,7,6,5,4,3,2,1,9      交换了 ...

  2. css3径向渐变详解-遁地龙卷风

    (-1)写在前面 我用的是chrome49,如果你用的不是.可以尝试换下浏览器前缀.IE在这方面的实现又特例独行了.不想提及-,这篇是为后续做准备. (0)快速使用 background-image: ...

  3. c++笔记整理

    一:导读 假设编写了一个C++程序,如何让他允许起来呢,这取决于计算机环境和所使用的C++编译器. 1.使用文本编辑器编写程序,并将其保存在文档中,====此就是源代码 2.编译源代码,编译过程就意味 ...

  4. linux——常用命令与脚本

    linux常用命令 --文件管理pwd --查看当前目录cd --切换当前目录ls --列出当前目录下的所有文件touch --创建文件mkdir --建立目录rmdir --删除空目录rm --删除 ...

  5. 如何在github下载开源项目到本地(Coding iOS 客户端为例)

    一.前言 以 Coding iOS 客户端 为例讲解如何在github下载开源项目到本地 github地址:https://github.com/Coding/Coding-iOS 二.分析 根据项目 ...

  6. Hotaru's problem(hdu 5371)

    题意:给出一个数字串,询问最长的子串,满足以下要求:将子串平均分为三部分,一三部分相等,一二部分对衬. /* 在manachar的基础上,枚举回文串的中心,再找第三部分. */ #include< ...

  7. centos下查看最大Socket连接数

    使用命令ulimit -a查看,其中open files就是最大连接数,一般情况下web服务器最大连接数的设置不能超过它 修改最大连接数:vi /etc/security/limits.conf 文件 ...

  8. [译]:Orchard入门——Orchard控制面板概览

    原文链接:Getting Around the Dashboard 文章内容基于Orchard 1.8版本 Orchard控制面板用于管理网站.改变外观.添加内容以及控制Orchard功能可用性.成功 ...

  9. LeetCode——Single Number II(找出数组中只出现一次的数2)

    问题: Given an array of integers, every element appears three times except for one. Find that single o ...

  10. windows消息钩子注册底层机制浅析

    标 题: [原创]消息钩子注册浅析 作 者: RootSuLe 时 间: 2011-06-18,23:10:34 链 接: http://bbs.pediy.com/showthread.php?t= ...