FileUtil

阅读更多
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

import org.springframework.web.multipart.MultipartFile;


public class FileUtil {


    /**
     *
     * @param fileName  文件名
     * @param filecontent  加密后内容
     * @return
     */
    public static byte[] createFile(String fileName,String filecontent){
        Boolean bool = false;
        /*File file = new File(fileName);*/
        try {
            /*//如果文件不存在,则创建新的文件
            if(!file.exists()){
                file.createNewFile();
                bool = true;
                System.out.println("success create file,the file is "+fileName);
                //创建文件成功后,写入内容到文件里
                return writeFileContent(fileName, filecontent);
            }else{
                file.delete();
                file.createNewFile();
                bool = true;
                System.out.println("success create file,the file is "+fileName);
                //创建文件成功后,写入内容到文件里
                return writeFileContent(fileName, filecontent);
            }*/
            return writeFileContent(fileName, filecontent);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }


    public static byte[] writeFileContent(String filepath,String newstr) throws IOException {
        String filein = newstr+"\r\n";//新写入的行,换行
        StringBuffer buffer = new StringBuffer();
        try{
            buffer.append(filein);
            return buffer.toString().getBytes();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return null;
    }



    public static byte[] File2byte( File file)
    {
        byte[] buffer = null;
        try
        {
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] b = new byte[1024];
            int n;
            while ((n = fis.read(b)) != -1)
            {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return buffer;
    }


    /**
     * @param fileName
     * @return
     */
    public static String getFileType(String fileName) {
        String type = "";
        if (fileName.contains(".")) {
            type = fileName.substring(fileName.lastIndexOf(".") + 1);
            return type;
        } else {
            return "";
        }
    }


    //获取生成文件的file
    public static File getFile(MultipartFile imgFile, String brandName, List fileTypes) {
        String fileName = imgFile.getOriginalFilename();
        // 获取上传文件类型的扩展名,先得到.的位置,再截取从.的下一个位置到文件的最后,最后得到扩展名
        String ext = fileName.substring(fileName.lastIndexOf(".") + 1,
                fileName.length());
        // 对扩展名进行小写转换
        ext = ext.toLowerCase();
        File file = null;
        if (fileTypes.contains(ext)) { // 如果扩展名属于允许上传的类型,则创建文件
            file = creatFolder(brandName, fileName);
            try {
                imgFile.transferTo(file); // 保存上传的文件
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            // this.scale(file);
        }
        return file;
    }
    //生成文件
    public static File creatFolder(String brandName, String fileName) {
        File file = null;
        File firstFolder = new File(brandName);

        if (firstFolder.exists()) { // 如果一级文件夹存在,则检测二级文件夹
            file = new File(brandName + "\\"+ fileName);
        } else { // 如果一级不存在,则创建一级文件夹
            firstFolder.mkdir();
            file = new File(brandName + "\\" + fileName);
        }
        return file;
    }

    public  static String compare(String str) {
        String re = str;
        int m = 0;
        int n = 0;

        int x =0;
        int y =0;

        if(str ==" "){
            return re;
        }
        else{
            char c [] = str.toCharArray();
            for(int i=0;i 
  

 

你可能感兴趣的:(FileUtil)