喜悦国际村 
» 游客:  注册 | 登录 | 搜索 | 统计 | 喜悦证交所 | 帮助

RSS 订阅当前论坛  

招聘PHP开发人员(北京)

上一主题 下一主题
     
标题: [代码] 使用JAVA,封装了一些PHP常用的函数  
 
font
论坛元老
Rank: 8Rank: 8
初级会员


UID 43400
精华 0
积分 36361
帖子 260
金钱 36361 喜悦币
威望 0
人脉 0
阅读权限 90
注册 2004-11-1
状态 离线
[广告]: Enom域名自助付费 自助注册 自助PUSH 主流域名COM等一律57.99元年
使用JAVA,封装了一些PHP常用的函数

以下是文件操作类

/**
*
*/
package com.tao.global;

import java.io.*;
import java.net.*;
/**
* @author font
*
*/
public class files {

        /**
         * @param args
         */
       
        public static RandomAccessFile fopen(String filename , String mode){//打开文件
               
                RandomAccessFile raf = null;
               
                try{
                        raf = new RandomAccessFile(filename,mode);
                }
                catch (FileNotFoundException err)
                {
                        return null;
                }
               
                return raf;
        }
       
        public static boolean fwrite (String filename,String content){//写文件
                //没有则创建
               
                File myFile = new File(filename);
                FileWriter fw = null;
               
                if (!myFile.exists())
                {
                        //file not exists
                        try{
                                myFile.createNewFile();
                        }
                        catch(IOException err)
                        {
                                return false;
                        }
                        try {
                                fw = new FileWriter(filename);
                                fw.write(content);
                                fw.close();
                                return true;
                        }
                        catch (IOException err)
                        {
                                return false;
                        }
                }
                else
                {
                        //file exists;
                        try{
                                fw = new FileWriter(filename);
                                fw.write(content);
                                fw.close();
                        }
                        catch(IOException err)
                        {
                                return false;
                        }
                        return true;
                }
               
        }
       
       
       
        public static boolean addfwrite(RandomAccessFile fhandle,String content){//增长文件
               
                //没有则创建,有则追加
                try{
                        fhandle.seek(fhandle.length());
                       
                        fhandle.write(content.getBytes());
                        //fhandle.writeBytes(content);
                        //fhandle.writeChars(content);
                        fclose(fhandle);
                }
                catch (IOException err)
                {
                        return false;
                }
               
                return true;
               
        }
       
        public static String fread(RandomAccessFile fhandle, int length){//读取文件
               
                String content = "";
               
                try{
                        fhandle.seek(0);
                        fclose(fhandle);
                        return content;
                }
                catch(IOException err)
                {
                        return "";
                }
               
        }
       
        public static void fclose(RandomAccessFile fhandle){//关闭文件handle
                try{
                        fhandle.close();
                }
                catch(IOException err)
                {
                        //
                }
        }
       
        public static boolean file_exists(String filename){//文件是否存在
               
                File f = new File(filename);
               
                if (f.exists())
                        return true;
                else
                        return false;
               
        }
       
        public static boolean is_readable(String filename){//文件是否可读
               
                File f = new File(filename);
               
                if (f.exists()){
                        if (f.canRead())
                                return true;
                        else
                                return false;
                }
                else
                        return false;
               
        }
       
        public static boolean is_writeable(String filename){//本地文件是否可写
               
                File f = new File(filename);
               
                if (f.exists()){
                        if (f.canWrite())
                                return true;
                        else
                                return false;
                }
                else
                        return false;               
               
        }
       
        public static String file_get_contents(String ur){//获取网络上的文件
               
                URL url = null;
                URLConnection uc = null;
                BufferedReader reader = null;
                InputStream urlStream = null;
               
                //StringBuffer sTotalString=new StringBuffer();
                String sTotalString = "";
                String sCurrentLine = "";
               
                try{
                        url = new URL(ur);
                }
                catch(MalformedURLException err)
                {
                        return "";
                }
               
                try{
                       
                        uc = url.openConnection();
                        uc.connect();
                        urlStream = uc.getInputStream();
                        reader = new BufferedReader(new InputStreamReader(urlStream));
                       
                        while ((sCurrentLine = reader.readLine()) != null)
                        {
                                if (sCurrentLine.length()>0)
                                        sTotalString = sTotalString + sCurrentLine.trim();
                                        //sTotalString.append(sCurrentLine);
                        }
                        String tmpStr = new String(sTotalString.getBytes("GB2312"));
                        sTotalString = tmpStr;
                       
                }
                catch(IOException err)
                {
                        return "";
                }
               
               
                //return sTotalString.toString();
                return sTotalString;
               
        }
       
        public static boolean is_file(String filename){//是否是文件
               
                File file = new File(filename);
            
            boolean isFile = file.isFile();
            
            return isFile;
            
        }
       
        public static boolean is_dir(String dirname){//是否是目录
            
                File dir = new File(dirname);
            
            boolean isDir = dir.isDirectory();
            
            return isDir;
            
        }
       
        public static long filesize(String filename){//获得文件大小
               
            File file = new File(filename);
            
            long length = 0;
            
            if (is_file(filename))
                    length = file.length();
            
            return length;
            
        }
       
        public static boolean unlink(String filename){//删除一个文件
               
                boolean success = false;
               
                if (is_file(filename))
                        success = (new File(filename)).delete();
               
                return success;
               
        }
       
        public static long filemtime(String filename){//获取一个文件的最后修改时间
               
                if (is_file(filename) || is_dir(filename))
                {
                        File file = new File(filename);
                       
                        long modifiedTime = file.lastModified();
                       
                        return modifiedTime;
                }
                else
                {
                        return 0;
                }
               
        }
       
        public static boolean touch(String filename){//修改一下文件的最新时间
               
                if (is_file(filename)){
                       
                        File file = new File(filename);
                       
                    long newModifiedTime = System.currentTimeMillis();
                    
                    boolean success = file.setLastModified(newModifiedTime);
                    
                    return success;
                       
                }
                else
                {
                        return false;
                }
               
        }
       
        public static void main(String[] args) {
                // TODO 自动生成方法存根
                String x = file_get_contents("http://www.163.com");
                String tmpStr = "";
                try{
                        tmpStr = new String(x.getBytes("GB2312"));
                }
                catch(Exception e)
                {
                }
                RandomAccessFile a = null;
                a = fopen("c:\\sina.txt","rw");
                addfwrite(a,tmpStr);
               
               
        }

}




我木有积分我木有 金钱我木有喜悦币我木有威望我木有人脉我木有头衔
2006-10-17 03:13 PM#1
查看资料  访问主页  Blog  发短消息  顶部
 
font
论坛元老
Rank: 8Rank: 8
初级会员


UID 43400
精华 0
积分 36361
帖子 260
金钱 36361 喜悦币
威望 0
人脉 0
阅读权限 90
注册 2004-11-1
状态 离线
[广告]: 代充Paypal帐号美元
模拟PHP的时间,一些简单函数

package com.tao.global;

import java.util.*;
import java.text.*;

public class time {

       
        public static long time(){
               
                   SimpleDateFormat myFormatter5 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
               
                   Calendar cal5  = Calendar.getInstance();
                  
                   Date mydate5 = null;
                  
                   Date date6 = null;
               
                   String date5=myFormatter5.format(cal5.getTime());
                  
                   try {
               
                           mydate5= myFormatter5.parse("1970-01-01 08:00:00");
                       
                           date6 = myFormatter5.parse(date5);
                          
                        }
                        catch(java.text.ParseException e)
                        {
                                //
                        }
                       
                   long  nowtime5=(date6.getTime()-mydate5.getTime())/(1000);
                          
                   return nowtime5;//模拟PHP里的time();//格林威治时间应该是有8个小时的时差
        }
       
        public static String date(String format){
               
                   //SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        SimpleDateFormat myFormatter = new SimpleDateFormat(format);       
                    
                        Calendar cal  = Calendar.getInstance();
               
                        String date3=myFormatter.format(cal.getTime());
                       
                        return date3;
               
        }
       
        public static void main(String args[])
        {
                System.out.println(date("yyyy-MM-dd HH:mm:ss"));
        }
       
       
}




我木有积分我木有 金钱我木有喜悦币我木有威望我木有人脉我木有头衔
2006-10-17 03:14 PM#2
查看资料  访问主页  Blog  发短消息  顶部
 
font
论坛元老
Rank: 8Rank: 8
初级会员


UID 43400
精华 0
积分 36361
帖子 260
金钱 36361 喜悦币
威望 0
人脉 0
阅读权限 90
注册 2004-11-1
状态 离线
[推荐阅读] 不知道是不是在耽人子弟
模拟PHP的MD5,网上的JAVABEAN对中文支持有问题,所以直接用JDK里面自己带的.

package com.tao.global;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class md5 {

        public static String md5(String s)
        {
                MessageDigest alg = null;
                StringBuffer s3 = new StringBuffer();               
                try{
                        alg = MessageDigest.getInstance("MD5");
                        alg.update(s.getBytes());
                        byte[] b1 = alg.digest();//16位长度
                        byte b2;
                        String s2 = "";

                for (int i1=0;i1<b1.length;i1++)
                {
                        b2 = b1[i1];
                        s2 = Integer.toHexString(b2);

                        s2=s2.replaceFirst("ffffff","");
                        if (s2.length()==1) s2="0"+s2;
                       
                        s3.append(s2);
                }
                
                }
                catch(NoSuchAlgorithmException err)
                {
                        return null;
                }
                return s3.toString();
        }
       
        /*
        public static void main(String args[])
        {
       
               
                try{
               System.out.println(md5("我按时反对按时法打算发生发按时法是电风扇上安放为撒旦发生发生地方"));       
                }
                catch(Exception e)
                {
                }
               

        }
        */
       
}




我木有积分我木有 金钱我木有喜悦币我木有威望我木有人脉我木有头衔
2006-10-17 03:15 PM#3
查看资料  访问主页  Blog  发短消息  顶部
 
font
论坛元老
Rank: 8Rank: 8
初级会员


UID 43400
精华 0
积分 36361
帖子 260
金钱 36361 喜悦币
威望 0
人脉 0
阅读权限 90
注册 2004-11-1
状态 离线
[推荐阅读] [杭州]招聘PHP开发与测试人员(长期有效)
模拟了PHP的BASE64,JAVA的BASE64需要做一点小处理.

/**
*
*/
package com.tao.global;

import sun.misc.*;

/**
* @author font
*
*/
public class base64 {

        /**
         * @param args
         */
        public static String base64_encode(String s){
               
                if (s == null) return null;
               
                String ss = new BASE64Encoder().encode(s.getBytes());
               
                ss = ss.replaceAll("\r","");
                ss = ss.replaceAll("\n","");
               
                return (ss);
        }
               
                // 将 BASE64 编码的字符串 s 进行解码
        public static String base64_decode(String s) {
                if (s == null) return null;
                        BASE64Decoder decoder = new BASE64Decoder();
                try {
                    byte[] b = decoder.decodeBuffer(s);
                    return new String(b);
                }
                catch (Exception e) {
                    return null;
                }
        }
       
        /*
        public static void main(String[] args) {
                // TODO 自动生成方法存根
                String ss = base64_encode("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
                String pp=base64_decode(ss);
                  System.out.println(ss+"\n"+pp);
        }
*/
}




我木有积分我木有 金钱我木有喜悦币我木有威望我木有人脉我木有头衔
2006-10-17 03:16 PM#4
查看资料  访问主页  Blog  发短消息  顶部
 
font
论坛元老
Rank: 8Rank: 8
初级会员


UID 43400
精华 0
积分 36361
帖子 260
金钱 36361 喜悦币
威望 0
人脉 0
阅读权限 90
注册 2004-11-1
状态 离线
[推荐阅读] 窗口问题
一些常用的JAVABEAN,没有模拟PHP什么东西

package com.tao.global;

public class convert {
       
        public String converts (String strin)
        {
                String strout = null;
                try
                {
                        strout = new String(strin.getBytes("ISO-8859-1"));
                }
                catch(java.io.UnsupportedEncodingException e){
                        //
                }
                catch(java.lang.NullPointerException err)
                {
                        //
                }
                return strout;
        }
       
}




我木有积分我木有 金钱我木有喜悦币我木有威望我木有人脉我木有头衔
2006-10-17 03:17 PM#5
查看资料  访问主页  Blog  发短消息  顶部
 
因扎吉
注册会员
Rank: 2


UID 75696
精华 0
积分 72
帖子 82
金钱 72 喜悦币
威望 0
人脉 0
阅读权限 20
注册 2006-6-29
状态 离线
[推荐阅读] 中英文混合生成图片的问题。
汗,果然有够强
2006-10-20 07:31 AM#6
查看资料  发短消息  顶部
     


  可打印版本 | 推荐给朋友 | 订阅主题 | 收藏主题 | 开通个人空间  


 




Powered by Discuz! 6.1.0  © 2001-2010 Comsenz Inc.
Processed in 0.024804 second(s), 6 queries

(冀ICP备05009913号) 管理员:sadly 邮箱/MSN: sadly@phpx.com QQ:824008(长隐) 清除 Cookies - - Archiver - WAP