【一、定时】
1 Android中的Timer一般像下面这样写: 2 3 4 5 Timer timer = new Timer( ); 6 7 TimerTask task = new TimerTask( ) { 8 9 public void run ( ) { 10 11 Message message = new Message( ); 12 13 message.what = 1; 14 15 handler.sendMessage(message); 16 17 } 18 19 }; 20 21 final Handler handler = new Handler( ) { 22 23 public void handleMessage(Message msg) { 24 25 switch (msg.what) { 26 27 case 1: 28 29 log.e("Timer","Timer"); 30 31 update( ); 32 33 break; 34 35 } 36 37 super.handleMessage(msg); 38 39 } 40 41 }; 42 43 protected void onDestroy ( ) { 44 45 if (timer != null) { 46 47 timer.cancel( ); 48 49 timer = null; 50 51 } 52 53 super.onDestroy( ); 54 55 } 56 57 protected void onCreate (Bundle savedInstanceState) { 58 59 super.onCreate(savedInstanceState); 60 61 this.setContentView(R.layout.main); 62 63 timer.schedule(task,1000,5000); 64 65 }
1 private Handler handler = new Handler( ); 2 3 private Runnable runnable = new Runnable( ) { 4 5 public void run ( ) { 6 7 update( ); 8 9 handler.postDelayed(this,1000); //postDelayed(this,1000)方法安排一个Runnable对象到主线程队列中 10 11 } 12 13 }; 14 15 handler.postDelayed(runnable,1000); // 开始Timer 16 17 handler.removeCallbacks(runnable); //停止Timer
【二、读写文件】
1 //写文件在./data/data/com.tt/files/下面 2 3 public voidwriteFileData(String fileName,String message){ 4 5 try{ 6 7 FileOutputStream fout =openFileOutput(fileName, MODE_PRIVATE); 8 9 byte [] bytes = message.getBytes(); //可在此处进行转码设置 10 11 fout.write(bytes); 12 13 fout.close(); 14 15 } 16 17 catch(Exception e){ 18 19 e.printStackTrace(); 20 21 } 22 23 } 24 25 //------------------------------------------------------- 26 27 //读文件在./data/data/com.tt/files/下面 28 29 public String readFileData(String fileName){ 30 31 String res=""; 32 33 try{ 34 35 FileInputStream fin = openFileInput(fileName); 36 37 int length = fin.available(); 38 39 byte [] buffer = new byte[length]; 40 41 fin.read(buffer); 42 43 res = EncodingUtils.getString(buffer, "UTF-8"); 44 45 fin.close(); 46 47 } 48 49 catch(Exception e){ 50 51 e.printStackTrace(); 52 53 } 54 55 return res; 56 57 }
1 /** 2 * 描述:追加内容到文件末尾 3 * @author Administrator 4 * 5 */ 6 public class WriteStreamAppend { 7 /** 8 * 追加文件:使用FileOutputStream,在构造FileOutputStream时,把第二个参数设为true 9 * 10 * @param fileName 11 * @param content 12 */ 13 public static void method1(String file, String conent) { 14 BufferedWriter out = null; 15 try { 16 out = new BufferedWriter(new OutputStreamWriter( 17 new FileOutputStream(file, true))); 18 out.write(conent); 19 } catch (Exception e) { 20 e.printStackTrace(); 21 } finally { 22 try { 23 out.close(); 24 } catch (IOException e) { 25 e.printStackTrace(); 26 } 27 } 28 } 29 30 /** 31 * 追加文件:使用FileWriter 32 * 33 * @param fileName 34 * @param content 35 */ 36 public static void method2(String fileName, String content) { 37 try { 38 // 打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件 39 FileWriter writer = new FileWriter(fileName, true); 40 writer.write(content); 41 writer.close(); 42 } catch (IOException e) { 43 e.printStackTrace(); 44 } 45 } 46 47 /** 48 * 追加文件:使用RandomAccessFile 49 * 50 * @param fileName 51 * 文件名 52 * @param content 53 * 追加的内容 54 */ 55 public static void method3(String fileName, String content) { 56 try { 57 // 打开一个随机访问文件流,按读写方式 58 RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw"); 59 // 文件长度,字节数 60 long fileLength = randomFile.length(); 61 // 将写文件指针移到文件尾。 62 randomFile.seek(fileLength); 63 randomFile.writeBytes(content); 64 randomFile.close(); 65 } catch (IOException e) { 66 e.printStackTrace(); 67 } 68 } 69 70 public static void main(String[] args) { 71 System.out.println("start"); 72 method1("c:/test.txt", "追加到文件的末尾"); 73 System.out.println("end"); 74 } 75 76 }
参考:
http://www.cnblogs.com/freeliver54/archive/2011/09/16/2178910.html
http://blog.csdn.net/jincf2011/article/details/6603916