⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileio.java

📁 java阿里巴巴代码
💻 JAVA
字号:
package tools.util;

import java.io.*;

/**
 * Created by zywang
 * Date: 2006-8-28
 * Time: 19:32:10
 */
public class FileIO {

    static public String LoadFile(String strFileName){
        String str = "";
        char[] chrBuffer = new char[1];
        File objFile = new File(strFileName);
        try {
            if(objFile.exists()){
                FileReader  objFileReader = new FileReader(objFile);
                while((objFileReader.read(chrBuffer))!=-1){
                    str = str + String.valueOf(chrBuffer);
                }
                objFileReader.close();
            }
            else{
                System.out.println("下列文件不存在:"+strFileName);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return str;
    }

   static synchronized public void SaveToFile(String strbody,String strFileName){
        try {
            PrintWriter pw = new PrintWriter(new FileOutputStream(strFileName));
            pw.println(strbody);
            pw.close();
        } catch(IOException e) {
            System.out.println("写入文件失败!"+strFileName);
            e.printStackTrace();
        }
    }

    static synchronized public void SaveToFile(String strbody, String filename, String filepath){
       try{
            CheckFloder(filepath);
            FileOutputStream fileoutputstream = new FileOutputStream(filepath + filename);

            byte abyte0[] = strbody.getBytes();
            fileoutputstream.write(abyte0);
            fileoutputstream.close();
        }catch(IOException e){
            System.out.println("写入文件失败!"+e.getMessage());
        }
    }

    static public boolean DelFile(String filepath){
        boolean flag = false;
        File file = new File(filepath);
        if (file.exists()){
            file.delete();
            flag = true;
        }
        return flag;
    }

    static public boolean ExistFloder(String foldpath){
        boolean flag = false;
        File file = new File(foldpath);
        if (file.isDirectory()){
            flag = true;
        }
        return flag;
    }

    static public boolean CreateFloder (String foldpath){
        boolean flag = true;
        File file = new File(foldpath);
        if (!file.exists()){
            try {
                file.mkdirs();
            } catch (Exception e) {
                e.printStackTrace();
                flag = false;
            }
        }
        return flag;
    }

    static public boolean RemoveFloder(String foldpath){
        boolean flag = true;
        File file = new File(foldpath);
        if (file.exists()){
            try {
                file.delete();
            } catch (Exception e) {
                e.printStackTrace();
                flag = false;
            }
        }
        return flag;
    }

    static public boolean RemoveFloders(String foldpath){
        boolean flag = true;
        File file = new File(foldpath);
        try {
            FileIO.DeleteFolds(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return flag;
    }

    static public boolean DeleteFolds(File file) throws IOException {
        boolean flag = true;

        if(!file.exists()){
            System.out.println("指定目录不存在:"+file.getName());
        }

        if(!(flag=file.delete())){
            File subs[] = file.listFiles();
            for (int i = 0; i <= subs.length - 1; i++) {
                if (subs[i].isDirectory())
                    DeleteFolds(subs[i]);
                flag = subs[i].delete();
            }
            flag = file.delete();
        }
        if(!flag){
            System.out.println("无法删除:"+file.getName());
        }
        return flag;
    }

    static public void CheckFloder (String filepath){
        File file = new File(filepath);
        if (!file.exists()){
            try {
                file.mkdirs();
            } catch (Exception e) {
                System.out.println("创建文件夹失败!"+filepath);
                e.printStackTrace();
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -