栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java文件操作方法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java文件操作方法

创建文件
//创建文件
    private void createFile(String newFile){
        try{
            File file = new File(newFile);
            if(file.createNewFile())
                System.out.println("文件创建成功!");
            else
                System.out.println("出错了,该文件已经存在。");
        }
        catch(IOException e) {
            e.printStackTrace();
        }
    }
删除文件
//删除文件
    private void deleteFile(String deleteFile){
        try{
            File file = new File(deleteFile);
            if(file.delete()){
                System.out.println(file.getName() + " 文件已被删除!");
            }else{
                System.out.println("文件删除失败!");
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
获取文件编码方式
//获取文件编码方式
    public static String resolveCode(String pathName) throws Exception {
        InputStream inputStream = new FileInputStream(pathName);
        byte[] head = new byte[3];
        inputStream.read(head);
        String code = "gb2312";  //或GBK
        if (head[0] == -1 && head[1] == -2 )
            code = "UTF-16";
        else if (head[0] == -2 && head[1] == -1 )
            code = "Unicode";
        else if(head[0]==-17 && head[1]==-69 && head[2] ==-65)
            code = "UTF-8";
        inputStream.close();
        return code;
    }
读取文件
//File读取txt文件
    private ArrayList fileReaderTxt(String pathName){
        //定义数据
        ArrayList fileData=new ArrayList();
        //读取文件
        File file = new File(pathName);
        //建立输入流对象
        InputStreamReader reader = null;
        //建立对象并转换为可读内容
        BufferedReader br = null;
        try {
            try{
                //InputStreamReader(1,2) 1是文件流;2是文件编码方式,可不写
                reader = new InputStreamReader(new FileInputStream(pathName),"gb2312");
            }catch(Exception e){
                e.printStackTrace();
            }
            br = new BufferedReader(reader);
        }catch (Exception e){
            e.printStackTrace();
        }
        try{
            String line="";
            //逐行读取数据
            while((line= br.readLine())!=null){
                if(!line.isEmpty()){
                    String[] info=line.split("t");
                    fileData.add(info.clone());
                }
                //break;
            }
            for(int i=0;i
                for(int j=0;j
                    System.out.print(fileData.get(i)[j]+"t");
                }
                System.out.println();
                if(i==2)break;
            }
            //关闭数据
            reader.close();
        }catch(IOException e){
            System.out.println("文件读取错误");
        }
        return fileData;
    }
写入文件
//File写入txt文件
    private void fileWriteTxt(String pathName,ArrayList fileData){
        File file = new File(pathName);
        BufferedWriter out = null;
        try{
            //创建新文件
            file.createNewFile();
            out = new BufferedWriter(new FileWriter(file));
            for(int i=0;i
                for(int j=0;j
                    out.write(fileData.get(i)[j]+"t");
                }
                out.write("n");
                if(i==2)break;
            }
            //把缓存区内容压入文件
            out.flush();
            //关闭输出
            out.close();
        }catch(IOException e){
            e.printStackTrace();
        }
    }
获取文件夹内文件
//获取文件夹下所有文件名
    private void getFileName(){
        String targetFile="./";
        File file=new File(targetFile);
        if(!file.exists()){
            System.out.println("文件夹不存在");
        }
        //获取目录下文件及文件夹
        File[] files=file.listFiles();
        for(int i=0;i< files.length;i++){
            //ifFile()确保是文件,而不是文件夹
            if(files[i].isFile()) {
                //files[i]是文件路径,files[i].getName()则只是文件的名称
                System.out.println(files[i].getName());
            }
        }
    }
文件重命名
//文件重命名
    private void renameFile(String pathName,String rename){
        File file = new File(pathName);
        // 设置新的文件名
        File fileNew = new File(rename);
        // 判断旧文件是否存在
        if (!file.exists()) {
            System.out.println("文件不存在");
        }else{
            // 文件存在就重命名
            file.renameTo(fileNew);
            System.out.println("重命名完成!");
        }
    }
复制文件
//复制文件-读取和写入
    private void copyFile(){
        String fileTxt="./input.txt";
        ArrayList fileData=fileReaderTxt(fileTxt);
        String fileTxt1="./input-2.txt";
        //判断文件是否存在
        int count=0;
        while(true){
            File existFile=new File(fileTxt1);
            if(existFile.exists()){
                fileTxt1=fileTxt1.replace(".txt",Integer.toString(count)+".txt");
            }
            else{
                break;
            }
            count++;
        }
        System.out.println(fileTxt1);
        fileWriteTxt(fileTxt1,fileData);
    }
追加文件内容
//追加文件内容
    private void appendFile(String fileTxt){
        try {
            //FileWriter的append属性设置为true,表示追加内容
            BufferedWriter out = new BufferedWriter(new FileWriter(fileTxt,true));
            out.write("追加内容");
            out.close();
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
获取文件大小
//获取文件大小
    private void getFileSize(String pathName){
        File file = new File(pathName);
        if (!file.exists() || !file.isFile()) {
            System.out.println("文件不存在");
        }
        else{
            System.out.println("文件大小:"+file.length()+"B");
        }
    }
获取文件修改时间
//获取文件修改时间
    private void getReviseTime(String pathName){
        File file = new File(pathName);
        Long lastModified = file.lastModified();
        Date date = new Date(lastModified);
        System.out.println("文件修改时间:"+date);
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/973201.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号