工具类,文件及字符串操作的常用

来源:互联网 发布:怎么应聘淘宝客服 编辑:程序博客网 时间:2024/05/22 07:40


/******************字符串的异或************************/
 private static String encrypt(String strOld, String strKey)
 {
        byte[] data = strOld.getBytes();
        byte[] keyData = strKey.getBytes();
        int keyIndex = 0 ;
        for(int x = 0 ; x < strOld.length() ; x++)
        {
            data[x] = (byte)(data[x] ^ keyData[keyIndex]);
            if (++keyIndex == keyData.length)
            {
                keyIndex = 0;
            }
        }
        
        return new String(data);
        
    } 

    /***************************************
     * 查找特定字符
     * s1文件内容,s2要查找的字符串
     * **************************************/
        public static String strFindAndReplace(String text,String s2)  
        {
            StringBuilder ss=new StringBuilder(text);   
            int poit=ss.indexOf("NewStringUTF");//
            
            int startpoit=ss.indexOf("\"",poit);
            
            int endpoit=ss.indexOf("\"",startpoit+1);
            ss.replace(startpoit+1, endpoit, s2);
            return ss.toString();
        }
        
        /***************************************
         * 在manifest插入meta-data字段
         * text文件内容,s2要插入的字符串
         * 
         * **************************************/
            public String metadataFindAndReplace(String text,String s2)  
            {
                StringBuilder ss=new StringBuilder(text);   
                int poit=ss.indexOf("<application");//
                
                int startpoit=ss.indexOf("<activity",poit);
                ss.insert(startpoit-1, s2);
                return ss.toString();
            }  
        
        
            /***************************************
             * 查找特定字符对应的值
             * xmldata文件内容,findString要查找的字符串,返回找到对应的值
             * 如:android:name=“xxxxxxxxxxxxx”
             * 返回xxxxxxxxxxxxx
             * **************************************/
            public String GetStringValue(String xmldata,String findString)  
                {
                    StringBuilder ss=new StringBuilder(xmldata);   
                    int poit=ss.indexOf(findString);//
                    if(poit==-1)
                        return null;
                    int startpoit=ss.indexOf("\"",poit);
          
                   int endpoit=ss.indexOf("\"",startpoit+1);
                    String substring=ss.substring(startpoit+1, endpoit);
                 
                    return substring.trim();
                }  
            
         /***************************************
            * 修改value值
            * xmldata文件内容,findString要修改的字符串key 
            * 替换掉特定字符串,没有找到的就在最后插入
            * 如:android:name=“xxxxxxxxxxxxx”
            * 替换掉整行
           * **************************************/
            
         public static String modifyStringValue(String xmldata,String findString,String value)  
           {
              StringBuilder ss=new StringBuilder(xmldata);   
              int poit=ss.indexOf(findString);//
              if(poit==-1)
              {
                            
                ss.insert(ss.length(), "\r\n"+value);
                                
              }
             else
            {
                            
                            
             int startpoit=ss.indexOf("\"",poit);
                            
             int endpoit=ss.indexOf("\"",startpoit+1);


             ss.replace(poit, endpoit+1, value);
            }
              
            return ss.toString();
       }  
                    
                    
      /***************读取文件******************************/  
        public String readfile(String path)
        {
            FileInputStream fin=null;
            String returnstring=null;
           // File file = new File(path);
            try {
                fin = new FileInputStream(path);
             //   if(fin==null)
                  //  return null;
                
                int length = fin.available(); 


                byte [] buffer = new byte[length]; 


                fin.read(buffer);     




                fin.close();    
            
                 returnstring=new String(buffer,"UTF-8");

            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 
            return returnstring;
            
        }
        
    /***************保存文件******************************/  
        public static boolean writeFileData(String fileName,String message)
        { 


               boolean res=false; 




                try{ 
                    FileOutputStream fout=null; 


                        
                         fout = new FileOutputStream(fileName);


                        
     
              //      if(fout==null)
                    //    return false;
                    
                       byte [] bytes = message.getBytes(); 


                       fout.write(bytes); 


                        fout.close(); 
                        res=true;
                     
                        
                       } 


                      catch(Exception e){ 
                       e.printStackTrace(); 


                      } 


             return res; 
           }  
        
/*********************拷贝文件*************************************/
    public boolean copyfile(String srcpath,String destpath)
    {
        
        FileInputStream fin=null;
        boolean res=false;
        FileOutputStream fout=null; 


        /*************读文件***********************/
        try {
            fin = new FileInputStream(srcpath);


            int length = fin.available(); 


            byte [] buffer = new byte[length]; 


            fin.read(buffer);     
            fin.close();    
        
      /*************** 写文件***********************/      
            fout = new FileOutputStream(destpath);
 
            fout.write(buffer); 


             fout.close();    
             res=true;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        
        return res;
    }
  
    /*****************删除文件*****************************************/
    public void deletefile(String path)
    {  
        File f = new File( path );
        f.delete();
        
    }


    /*****************重命名文件的名字*****************************************/
    public boolean renamefile(String oldpath,String newpath)
    {  
        boolean res=false;
        File oldf = new File( oldpath );
        File newf = new File( newpath );
        if(oldf.renameTo(newf))   
        {   
        System.out.println("修改成功!");   
        res=true;
        }   
        else   
        {   
        System.out.println("修改失败");   
        } 
     
        return res;
    } 
    
    /****************将反斜杠替换成正斜杠**********************************/
    public static String backlashReplace(String myStr){
        final StringBuilder result = new StringBuilder();
        final StringCharacterIterator iterator = new StringCharacterIterator(myStr);
        char character =  iterator.current();
        while (character != CharacterIterator.DONE ){
         
          if (character == '\\') {
             result.append("/");
          }
           else {
            result.append(character);
          }


          
          character = iterator.next();
        }
        return result.toString();
      }
    /********************获取系统时间************************************/    
    public static String getsystemtime()
    {    
    
    DateFormat df = new SimpleDateFormat("yyyyMMdd");
    String time=df.format(new Date());
    System.out.println(time);
    return  time;
    
    }
    
  /***********************创建文件夹*************************************************/  
    public static  boolean mkdir(String mkdirName)
    {
        boolean res=false;
        try
        {
            File  dirFile = new File(mkdirName);     //mkdirName为传建文件夹路径
            boolean bFile   = dirFile.exists();
            if( bFile == true )
            {
               System.out.println("The folder exists.");
               res=true;
            }
            else
            {
               System.out.println("The folder do  not exist,now trying to create a one...");


               bFile = dirFile.mkdir();
               if( bFile == true )
               {
                  System.out.println("Create successfully!");


                  res=true;
               }
               else
               {
                   System.out.println("Disable to make the folder,please check the disk is full or not.");
//                 System.exit(1);
               }
            }
        }
        catch(Exception err)
        {
            System.err.println("ELS - Chart : 文件夹创建发生异常");
            err.printStackTrace();
            res=true;
        }
        return res;
    }


/***************************************

*******************************************

********************************************/

java获取当前目录

  Properties properties = System.getProperties();

        System.out.println(properties.getProperty("user.dir"));

swt选择文件的窗口:

                JFileChooser chooser=new JFileChooser();
                if(IDfilePath!=null)
                {    
                    chooser.setCurrentDirectory(new File(IDfilePath));
                }
                chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
                chooser.showOpenDialog(null);
              
                if(chooser.getSelectedFile()==null)
                    return ;


swt选择文件夹的窗口:

                JFileChooser chooser=new JFileChooser();
                chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                chooser.showOpenDialog(null);
                if(apkprojPath!=null)
                {    
                    chooser.setCurrentDirectory(new File(apkprojPath));
                }
                
                
                if(chooser.getSelectedFile()==null)
                    return ;
                System.out.println(chooser.getSelectedFile());
                apkprojPath=chooser.getSelectedFile().toString();

将字符串按某些分隔:

clientname_no_textdata.split("\r\n");


判断字符串中是否是数字和字符的组成

if(s[i].matches("[0-9A-Za-z]*")==false)

查找字符串最后一次出现的特定字符串:

buildapkPath.lastIndexOf('/');

swt简单弹出框

 JOptionPane.showMessageDialog(null,errormessage);


java代码执行cygwin用工具ndk来生成so

                       //执行cygwin生成so

String cmdSoproj ="cd "+sotemp+"\n";
                       try {
                           Process proc = null;


                           File workDir = new File(“d:/cygwin/”);
                           Runtime rt = Runtime.getRuntime();
                           
                           String cmds[]= {"cmd.exe","/c","cygwin.bat"};
                           proc = rt.exec(cmds,null,workDir);
                           


                           DataOutputStream outStream = new DataOutputStream(proc.getOutputStream());
                           outStream.writeBytes(cmdSoproj);
                           outStream.writeBytes("$NDK/ndk-build");


                           outStream.flush();
                           outStream.close();  


                         // proc.waitFor() ;
                          InputStream fis = proc.getInputStream(); 
                          BufferedReader br = new BufferedReader(new InputStreamReader(fis)); 
                          String line = null; 
                          StringBuffer cmdout = new StringBuffer(); 
                          while ((line = br.readLine()) != null) { 
                                   cmdout.append(line); 
                           } 


                          br.close();
                         System.out.println("output :"+cmdout);
                         
                         if(!cmdout.toString().contains("libumuserdata1.soInstall"))
                         {
                            
                             errorPOP("亲,so编译出错了~~~");
                             return;
                         }

                   
                       } catch (Exception e1) {
                           System.out.println("error..Exception.");
                           errorPOP("亲,so编译出错了~~~");
                           
                           return;
                       }




    

原创粉丝点击