Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1165233
  • 博文数量: 1310
  • 博客积分: 3980
  • 博客等级: 中校
  • 技术积分: 8005
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-09 22:05
文章分类

全部博文(1310)

文章存档

2011年(1)

2008年(1309)

我的朋友

分类:

2008-05-24 08:29:45

程序代码 程序代码
/**
 * Java对文件的基本操作
 */
package file;

import java.io.*;

public class FileClass {
    //private String sFileName;
    //private String sFolderName;
    String sPath;
    String sFilePath;
    String sContent;
    
        /********************
         * 新建目录
         *******************/
        public boolean newFolder(String s)
        {
            sPath = s;
            sPath = sPath.toString();//中文转换
            //创建一个File(文件)对象
            File myFilePath = new File(sPath);
            if(!myFilePath.exists())
            {
                try
                {    
                    myFilePath.mkdirs();
                    return true;
                }catch(Exception e)
                {
                    e.printStackTrace();
                    return false;
                }
            }
            else return false;
        }
        
        /********************
         * 新建文件
         *******************/
        public boolean newFile(String s1,String s2)
        {
            sFilePath = s1;
            sContent = s2;
            sFilePath = sFilePath.toString();
            File myFilePath = new File(sFilePath);
            if(!myFilePath.exists())
            {
                try
                {    
                    myFilePath.createNewFile();
                    FileWriter resultFile=new FileWriter(myFilePath);
                    PrintWriter myFile=new PrintWriter(resultFile);
                    String sContent = s2.toString();
                    myFile.println(sContent);
                    myFile.close();
                    resultFile.close();
                    return true;
                }catch(Exception e)
                {
                    e.printStackTrace();
                    return false;
                }
            }
            else return false;
        }
        
        /********************
         * 删除文件
         *******************/
        public boolean delFile(String s)
        {
            sFilePath = s;
            sFilePath = sFilePath.toString();
            File dFile = new File(sFilePath);
            if(dFile.exists())
            {
                try
                {    
                    dFile.delete();
                    return true;
                }catch(Exception e)
                {
                    e.printStackTrace();
                    return false;
                }
            }
            else 
            {
                System.out.print("文件:" + s + "不存在!");
                return false;
            }
        }
        
        /********************
         * 复制文件
         *******************/
        public boolean copyFile(String s1,String s2)
        {
            int bytesum=0;
            int byteread=0;
            //file:读到流中
            try
            {
                InputStream inStream=new FileInputStream(s1);
                FileOutputStream fs=new FileOutputStream(s2);
                byte[]  buffer =new  byte[1444];
                while ((byteread=inStream.read(buffer))!=-1)
                {
                   System.out.println("--  "+byteread+" --");
                   bytesum+=byteread;
                   System.out.println(bytesum);
                   fs.write(buffer,0,byteread);
                } 
                inStream.close(); 
                fs.close();
                return true;
            }catch(Exception e)
            {      
                return false;
                }
        }
        
        /********************
         * 复制文件夹
         *******************/
        public boolean copyFolder(String s1,String s2)
        {
        try{
            (new File(s2)).mkdirs();
            File[] file=(new File(s1)).listFiles();
            for(int i=0;i            {
                if(file[i].isFile()){
                    file[i].toString();
                    FileInputStream input=new FileInputStream(file[i]);
                    FileOutputStream output=new FileOutputStream(s2+"/"+(file[i].getName()).toString());
                    byte[] b=new byte[1024*5];
                    int len;
                    while((len=input.read(b))!=-1){
                        output.write(b,0,len);
                    }
                    output.flush();
                    output.close();
                    input.close();
                }
            }
            return true;
         }catch(IOException e){
             e.printStackTrace();
             return false;
             }
        }
        
        /********************
         * 得到文本文件的内容
         *******************/
        public String getFile(String s1)
        {
        try{
            StringBuffer sb = new StringBuffer();
            BufferedReader in = new BufferedReader(new FileReader(s1));
            while(in.readLine()!=null){
                sb.append(in.readLine()+"\n\r");
            }
            return sb.toString();
         }catch(IOException e){
             e.printStackTrace();
             return null;
             }
        }

        
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO 自动生成方法存根
        String s1="";
        String s2="";
        FileClass file1 = new FileClass();
        //目录创建测试
        s2="F:/Test/Test1";
        if (file1.newFolder(s2))
            System.out.println("创建目录 " + s2 + " 成功!");
        else
            System.out.println("创建目录 " + s2 + " 失败!!");
        
        //文件创建测试
        s1="F:/Test/Test1/Test.txt";
        s2="这是一个测试文件!";
        file1.delFile(s1);
        if (file1.newFile(s1,s2))
            System.out.println("创建文件 " + s1 + " 成功!");
        else
            System.out.println("创建文件 " + s1 + " 失败!!");
        System.out.print("\n"+s2);
        
        //文件删除测试
        s1="F:/Test/Test1/Test.txt";
        if (file1.delFile(s1))
            System.out.println("删除文件 " + s1 + " 成功!");
        else
            System.out.println("删除文件 " + s1 + " 失败!!");
        
        //复制文件测试
        s1="F:/Test/Test1/Test.txt";
        s2="F:/Test/Test1/Test1.txt";
        if (file1.copyFile(s1,s2)) 
            System.out.print("把文件 "+s1+"成功复制到 "+s2);
        else System.out.print("复制文件失败!");
        
        //复制目录测试
        s1="F:/Test/Test1";
        s2="F:/Test/Test2";
        if (file1.copyFolder(s1,s2)) 
            System.out.print("把文件夹 "+s1+" 成功复制到文件夹 "+s2);
        else System.out.print("复制文件夹失败!");
        
        //得到文件内容测试
        System.out.print(file1.getFile("F:/Test/Test1/Test1.txt"));
        
    }
}
阅读(251) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~