/** * 在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标签 } };
Android 文件读写
最新推荐文章于 2021-05-25 23:31:24 发布