用Java操作文件

来源:互联网 发布:怎么用云解析解析域名 编辑:程序博客网 时间:2024/05/20 19:18

作者:梦想年华 日期:2006-09-27

一个用java实现对文件的操作。完成了如创建目录及文件,复制目录及文件以及删除等功能。
主要用到了java的File类,FileInputStream类,FileOutputStream,FileWriter,PrintWriter,InputStream,OutputStream ,BufferedReader和FileReader等相关类。
程序在某些方的实现可能存在一些问题,欢迎各位提出指正。
程序代码 程序代码

/**
 * Java对文件的基本操作
 * @author 梦想年华
 * Copyright (C) 2005 自由时代 All right reserved.
 * Email:fanwsp@126.com
 */

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<file.length;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"));
        
    }
}
 
原创粉丝点击