文件读写

来源:互联网 发布:最简单的编程软件 编辑:程序博客网 时间:2024/05/11 20:55

1.sdcard文件、自己应用包内部的文件和别的应用包的文件的读取


    /*******************************************************************
     * 0获取别的apk内部的文件
     * 1.获取sdcard的文件
     * 2.获取自己内部的文件
     * 
     * *******************************************************************/
    private String readFileData(String fileName,int num){ 


        String res=null; 


        try{ 
         FileInputStream fin=null;
        
         if(num==0)
        {
         File file = new File(fileName);
        // FileInputStream fin = openFileInput(file); 
          fin = new FileInputStream(file);
          Log.v(m_log,"read ID from other apk inside files\n");
        }
          else if(num==1)
          {
          fin = new FileInputStream(fileName);
          Log.v(m_log,"read ID from SDCARD files\n");
          
          }
          else if (num==2)
          {
          
          fin = localcontext.openFileInput(fileName);
          Log.v(m_log,"read ID from APK inside  files\n");
          
          }
          else
          {
          /*****其他的情况************/
          Log.v(m_log,"not support this type  read file \n");
          }
        
         if(fin==null)
         return null;
        
         int length = fin.available(); 


         byte [] buffer = new byte[length]; 


         fin.read(buffer);     


         res = EncodingUtils.getString(buffer, "UTF-8"); 


         fin.close();    
        
         Log.v(m_log,"read  files sucess\n");
        


        catch(Exception e){ 
         Log.v(m_log,"error in read  files \n");
         //e.printStackTrace(); 


        } 
        
       String data=decodeAndcheck(res);
        
        return data; 


    } 

2.写sd卡文件和写文件到应用自己的内部包


   /*******************************************************************
     * 0写到apk内部的文件
     * 1.写到sdcard的文件
     * 
     * *******************************************************************/
 private static boolean writeFileData(Context context,String fileName,String message,int num)
 { 


     boolean res=false; 
     String encode_message=null;
     String writemessage=null;
    
if(message==null)
return false;
 
// int Stringhash=message.hashCode();
 
// encode_message=encodedata(message);
// writemessage=encode_message+":"+Stringhash;


         try{ 
          FileOutputStream fout=null; 
          if(num==0)
           {
                 fout =context.openFileOutput(fileName, Context.MODE_WORLD_READABLE);
                 Log.v(m_log,"write file to inside apk package \n");
           }
          else if(num==1)
          {
          
               fout = new FileOutputStream(fileName);
               Log.v(m_log,"write file to sdcard \n");
          
          } 
          else
          {
          /*************************/
          Log.v(m_log,"not support this type  write file to sdcard \n");
          }
          
          if(fout==null)
          return false;
          
                byte [] bytes = message.getBytes(); 


                fout.write(bytes); 


                 fout.close(); 
                 res=true;
                 Log.v(m_log,"write file sucess \n");
                 
                } 


               catch(Exception e){ 
                
              Log.v(m_log,"error in write file to apk \n");
                e.printStackTrace(); 


               } 


  return res; 
    }  
    

3.System.loadLibrary的失败被接受的时间是UnsatisfiedLinkError;