1. 程式人生 > >Android 檔案讀寫

Android 檔案讀寫

/**
 * SD卡上建立【IMSI】資料夾
 */
public void CreateSDCardDir() {
    if (Environment.MEDIA_MOUNTED.equals(Environment
            .getExternalStorageState())) {
        // 建立一個資料夾物件,賦值為外部儲存器的目錄
        String sdcardDir = Environment.getExternalStorageDirectory().toString();
        // 得到一個路徑,內容是sdcard的資料夾路徑和名字
path = sdcardDir + "/IMSI"; File path1 = new File(path); if (!path1.exists()) { // 若不存在,建立目錄,可以在應用啟動的時候建立 path1.mkdirs(); } } else { return; } } public void CreateFile() { File outputPhotoImage = new File(path, "IMSINumber" + ".imsi"
); try { if (outputPhotoImage.exists()) { outputPhotoImage.delete(); } outputPhotoImage.createNewFile(); } catch (IOException e) { e.printStackTrace(); } }
//向已建立的檔案中寫入資料
public void SetFile(String str) {
    FileWriter fw = null;
    BufferedWriter bw = null;
String datetime = ""; try { SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd" + " " + "hh:mm:ss"); datetime = tempDate.format(new java.util.Date()).toString(); fw = new FileWriter( Environment.getExternalStorageDirectory().toString()+ "/IMSI"+"/IMSINumber.imsi", true);// // 建立FileWriter物件,用來寫入字元流 bw = new BufferedWriter(fw); // 將緩衝對檔案的輸出 String myreadline = datetime + "[]" + str; bw.write(myreadline + "\n"); // 寫入檔案 bw.newLine(); bw.flush(); // 重新整理該流的緩衝 bw.close(); fw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); try { bw.close(); fw.close(); } catch (IOException e1) { // TODO Auto-generated catch block } } }
public String GetFile(){
                File file = new File(Environment.getExternalStorageDirectory(),
                        "IMSINumber.imsi");

 
		/* FileInputStream is = new FileInputStream(file);
 		InputStream inputStream = is;
 		byte[] b = new byte[inputStream.available()];
		 is.read(b);//全部讀取
 		String result = new String(b);*/
BufferedReader bf= new BufferedReader( new FileReader(file)) ; String s = null; while (( s = bf.readLine())!= null ){ // 使用 readLine 方法,一次讀一行
 			textview.setText(s.toString());
} bf.close() ; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace() ; } //GetPhoneNum(); } } ; return null; }
/**
 *  獲取網頁內容
 */
public class Url_InputStreamReader {
    /*
    * 得到字元流前需先有位元組流
    */
    public String getStream(String url){
        try {
            //得到位元組流
            InputStream in = new URL(url).openStream();
            //將位元組流轉化成字元流,並指定字符集
            InputStreamReader isr = new InputStreamReader(in,"UTF-8");
            String results = "";
            int tmp;
            while((tmp = isr.read()) != -1){
                results += (char)tmp;
            }
            return results;

        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }


}

 

 
new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                String GetString = "http://a.10086.cn//pams2//mmtestnum.jsp?";
                String URL = GetString+"460021928244470";
                Url_InputStreamReader test = new Url_InputStreamReader();
                Bundle bundle = new Bundle();
                Message message = new Message();
                bundle.putString("name",test.getStream(URL));
                message.setData(bundle);
                handler.sendMessage(message);
                //Log.e("2333333",len+"");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }).start();
}

 
handler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        Bundle msgData = msg.getData();
        String phonedate = msgData.getString("url");
        String date = phonedate.substring(phonedate.indexOf("")+1);//擷取字串
        textview.setText(Html.fromHtml(date));//去除HTML標籤
    }
};