font
论坛元老
 
初级会员
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);
}
}
|  我木有积分我木有 金钱我木有喜悦币我木有威望我木有人脉我木有头衔 |
|