編輯:關於android開發
1、文件的讀取
io流讀取文件,並且顯示
package com.helloword;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
public class ReadFile {
private String text = null;
private StringBuffer strbuf=null;
public void ReadFile(File file) {
// TODO Auto-generated constructor stub
//獲取文件輸出流
FileInputStream fis;
try {
fis = new FileInputStream(file);
//將字節流轉換為字符流
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
try {
text = br.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
strbuf.append(text);
System.out.println(text);
}
}
其中訪問android的"data/data/com.helloword/file"建立過程如下
打開file explore,即可看到Android 的文件
打開cmd,進入sdk platform-tool
>adb shell
$ su
# chmod 777 /data
# chmod 777 /data/data
public class MainActivity extends Activity {
public Button bt =null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt = (Button) findViewById(R.id.btcon);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
File file = new File("data/data/com.helloword/file");
ReadFile readfile = new ReadFile();
}
});
}
2、SD卡文件的讀寫操作
(1)在manifest.xml中注冊,獲得SD卡的讀寫權限
<!-- SDCard中創建與刪除文件權限 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <!-- 向SDCard寫入數據權限 --> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
(2) 接著在使用SDcard進行讀寫的時候 會用到Environment類下面的幾個靜態方法 :
1: getDataDirectory() 獲取到Android中的data數據目錄(sd卡中的data文件夾)
2:getDownloadCacheDirectory() 獲取到下載的緩存目錄(sd卡中的download文件夾)
3:getExternalStorageDirectory() 獲取到外部存儲的目錄 一般指SDcard(/storage/sdcard0)
4:getExternalStorageState() 獲取外部設置的當前狀態 一般指SDcard,比較常用的應該是 MEDIA_MOUNTED(SDcard存在並且可以進行讀寫)還有其他的一些狀態,可以在文檔中進行查找.
/**
* 判斷SDCard是否存在 [當沒有外掛SD卡時,內置ROM也被識別為存在sd卡]
*
* @return
*/
public static boolean isSdCardExist() {
return Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED);
}
* 使用api獲得sd卡的真實路徑,部分手機品牌會更改sd卡的路徑
Environment.getExternalStorageDirectory();
讀取SD卡的內容
//讀取SD卡內容
//使用FileInputStream讀取文件
public String ReadFlieInputString(String FileName) throws IOException
{
String result = null;
File file = new File(FileName);
try {
FileInputStream isfile = new FileInputStream(file);
byte[] b = new byte[isfile.available()];
isfile.read(b);
result = new String(b);
System.out.print("讀取成功:"+ result);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
//使用BufferRead 讀取文件
public String FileBufferRead(String FileName) throws IOException {
String result = null;
try {
BufferedReader bReader = new BufferedReader(new FileReader(FileName));
String oneline = "";
StringBuffer sb = new StringBuffer();
while ((oneline = bReader.readLine()) != null) {
sb.append(oneline);
}
result = sb.toString();
bReader.close();
System.out.println("讀取成功");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
向SD卡中寫入文件
//寫入SD卡
//使用FileOutputStream寫入文件
public Boolean writeSDFile(String FileName, String content){
boolean result = false;
try {
File file = new File(Environment.getExternalStorageDirectory(), FileName);
//獲得輸出流
FileOutputStream fos = new FileOutputStream(file);
fos.write(content.getBytes());
fos.close();
System.out.println("寫入成功:");
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
//使用buffread寫入SD卡
public Boolean BufferWriteFile(String FileName, String content){
boolean result = false;
try {
File file = new File(Environment.getExternalStorageDirectory(),
FileName);
//第二個參數意義是說是否以append方式添加內容
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(content);
bw.flush();
System.out.println("寫入成功");
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
以上內容純屬自己練著玩!基本都是參照http://blog.csdn.net/mad1989/article/details/37568667
Android動畫效果生動有趣的通知NiftyNotification(Android Toast替代品),androidnotification
Android動畫效果生動有趣的通知NiftyNotification(Android Toast替代品),androidnotificationNiftyNotific
Android TextView兩端對齊
Android TextView兩端對齊 Android中的TextView控件默認是做不到兩端對齊的,都是左對齊。可能的原因是安卓默認數字、字母不能為第一行以後每行
Android 代碼動態改變View的屬性
Android 代碼動態改變View的屬性 設置Android View的長寬和位置我們平時都會在Layout的XML中定義,那麼什麼時候需要動態在代碼中設置View
android學習筆記之ImageView的scaleType屬性
android學習筆記之ImageView的scaleType屬性 我們知道,ImageView有一個屬性叫做scaleType,它的取值一共有八種,分別是:matrix