文件讀,寫,拷貝,刪除

// 建立臨時文件
    private static File getFileFromBytes(byte[] b, String suffix) {
        BufferedOutputStream stream = null;
        File file = null;
        try {
            file = File.createTempFile("pattern", "." + suffix);
            System.out.println("臨時文件位置:"+file.getCanonicalPath());
            FileOutputStream fstream = new FileOutputStream(file);
            stream = new BufferedOutputStream(fstream);
            stream.write(b);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (stream != null) {
                try {
                    stream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return file;
    }

    今天比較悠閒,總結下最近遇到的文件操做的方法,以供各位道友及貧道從此使用:java

//讀取文件

    public static String readFile(File f){
        StringBuffer content= new StringBuffer();
        byte[] b = new byte[1024*1024];
       try {
           InputStream reader = new FileInputStream(f);
            while(reader .read(b) != -1){
                content.append(new String(b,"UTF-8"));
            }
            reader .close();
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            if(reader != null){
                try {
                    reader .close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        return content.toString().trim();
    }

 

//解決讀取文件時出現亂碼問題

    public static String readFileByUTF8(File file) {
        StringBuffer content = new StringBuffer();
        try {
            if (file.isFile() && file.exists()) {
                InputStreamReader isreader= new InputStreamReader(
                        new FileInputStream(file), "UTF-8");
                BufferedReader breader = new BufferedReader(isreader);
                String fileLine;
                while ((fileLine= breader.readLine()) != null) {
                    content .append(fileLine+ "\n");
                }
                isreader.close();
            }
        } catch (Exception e) {
            System.out.println("讀取文件內容操做出錯");
            e.printStackTrace();
        }
        return content .toString();
    }



//寫入文件(獲得的String內容寫入文件中)

public static void writeFile(String content, String filepath) {
        try {
            File file = new File(filepath).getParentFile();
            if (!file .exists()) {
               file .mkdirs();
            }
            FileOutputStream fos = new FileOutputStream(filepath);
            Writer out = new OutputStreamWriter(fos, "UTF-8");
            out.flush();
            out.write(content);
            out.close();
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
        System.out.println("備份報文:" + filepath);
    }

//寫入文件(獲得的byte[]內容寫入文件中)

    public static void writeFile(byte[] content, String filepath) {
        try {
            File file = new File(filepath).getParentFile();
            if (!file.exists()) {
                file.mkdirs();
            }
            FileOutputStream fos = new FileOutputStream(filepath);
            fos.write(content, 0, content.length);
            fos.flush();
            fos.close();
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
        System.out.println("Write File:" + filepath);
    }



//文件拷貝 把一個文件內容拷貝到另外一箇中

    public static void copyfile(String readpath, String writepath)
            throws FileNotFoundException, IOException {
        File file = new File(writepath).getParentFile();;
        /*若是該文件爲空,則建立文件夾*/
        if (!file.exists()) {
            file.mkdirs();
        }
        FileInputStream fis = new FileInputStream(readpath);
        FileOutputStream fos = new FileOutputStream(writepath);
        int len = 0;
        byte[] b = new byte[1024];
        while ((len = fis.read(b)) != -1) {
            fos.write(b, 0, len);
        }
        fis.close();
        fos.close();
    }

//文件刪除

    public static void delFile(File file) {
        if (file.exists()) {
            if (file.isFile()) {
                file.delete();
            } else {
                File[] files = file.listFiles();
                for (int i = 0; i < files.length; i++) {
                    delFile(files[i]);
                }
                file.delete();
            }
        } else {
            log.error("Delete File:" + file);
        }
    }
相關文章
相關標籤/搜索