首页 > 编程语言 > 详细

java文件一些操作

时间:2021-09-01 13:25:39      阅读:13      评论:0      收藏:0      [点我收藏+]
1、文件操作
public
static void main(String args[]) throws IOException { String dirPath = "d:/aaa/bbb/"; File fileDirPath = new File(dirPath); System.out.println(fileDirPath.exists()); //判断文件目录是否存在 if (!fileDirPath.exists()) { if (!fileDirPath.isDirectory()) { fileDirPath.mkdir(); } } String filePath = dirPath + "/4244.txt"; System.out.println(filePath); File file = new File(filePath); //判断文件是否存在 if (!file.exists()) { file.createNewFile(); } //写数据到文件 BufferedWriter out = new BufferedWriter(new FileWriter(file)); out.write("55555fsfddasf55"); out.write("aaaaaaaa"); out.close(); System.out.println("文件创建成功!"); // System.out.println(delAllFile(dirPath)); //删除文件 // file.delete(); //读取文件 FileInputStream fileInputStream = new FileInputStream(filePath); int i ; byte[] bytes = new byte[1024]; while ((i = fileInputStream.read(bytes))!= -1) { String s = new String(bytes); System.out.println(s); } fileInputStream.close(); }

 2、文件打包成tar

先导入pom:

<dependency>
            <groupId>org.xeustechnologies</groupId>
            <artifactId>jtar</artifactId>
            <version>1.1</version>
        </dependency>

 

/**
             * 文件打成tar
             */
            //原来的文件地址
            FileOutputStream dest;
            //打包后的tar地址
            TarOutputStream out;
            try {
                dest = new FileOutputStream(tarPath);
                out = new TarOutputStream(new BufferedOutputStream(dest));
                File f = new File(txtPath);
                out.putNextEntry(new TarEntry(f, f.getName()));
                BufferedInputStream origin = new BufferedInputStream(new FileInputStream(f));
                int count;
                byte data[] = new byte[2048];
                while ((count = origin.read(data)) != -1) {
                    out.write(data, 0, count);
                }
                out.flush();
                origin.close();
                out.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

 

java文件一些操作

原文:https://www.cnblogs.com/luoa/p/15206005.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!