(原)Eclipse的java中文件读写

1 在<uses-sdk…/>下面添加permission

<uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="16" />
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
 <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />

2 在onCreate函数的下面添加如下代码:

 1     //写SD中的文件
 2     public void writeFileSdcardFile(String fileName,byte [] bytes) throws IOException
 3     {   
 4         try
 5         {   
 6             FileOutputStream fout = new FileOutputStream(fileName);   
 7             fout.write(bytes);   
 8             fout.close();   
 9         }  
10         catch(Exception e)
11         {   
12             e.printStackTrace();   
13         }   
14     }   
15                   
16     //读SD中的文件   
17     public void readFileSdcardFile(String fileName, byte [] bytes) throws IOException
18     {    
19         try
20         {   
21             FileInputStream fin = new FileInputStream(fileName);   
22             fin.read(bytes);       
23             fin.close();       
24         }   
25         catch(Exception e)
26         {   
27             e.printStackTrace();   
28         }      
29     }

3 调用处:

 1         byte [] buffer = new byte[width*height];   
 2 
 3         try  //读文件
 4         {
 5             readFileSdcardFile("/mnt/sdcard/2.raw", buffer);
 6         } 
 7         catch (IOException e) 
 8         {
 9             // TODO Auto-generated catch block
10             e.printStackTrace();
11         }
12     
13         //your program...
14      
15         byte[] arraydst2 = new byte [width*height];
16         try  //写文件
17         {
18             writeFileSdcardFile("/mnt/sdcard/2out.raw",arraydst2);
19         } 
20         catch (IOException e) 
21         {
22             // TODO Auto-generated catch block
23             e.printStackTrace();
24         }

 

4 主要参考:

http://blog.csdn.net/ztp800201/article/details/7322110

 

posted on 2014-11-04 12:03  darkknightzh  阅读(1088)  评论(0编辑  收藏  举报

导航