前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java 字节流

Java 字节流

作者头像
Tim在路上
发布2020-08-04 22:09:13
7990
发布2020-08-04 22:09:13
举报

OutputStream

此抽象类是表示输出字节流的所有类的超类。输出流接受输出字节并将这些字节发送到某个接收器

FileOutputStream

实现

BufferedOutputStream

缓冲,过设置这种输出流,应用程序就可以将各个字节写入基础输出流中,而不必为每次字节写入调用基础系统。

InputStream

此抽象类是表示字节输入流的所有类的超类。

FileInputStream

用于读取诸如图像数据之类的原始字节流,要读取字符流。

BufferedInputStream

读取或跳过流中的各字节时,必要时可根据所包含的输入流再次填充该内部缓冲区,一次填充多个字节。

1.实现数据的换行? 输入\n换行,在高级记事本中打开可以换行,但是在低记事本中不会 不同的系统换行符号不同,在windows下用\r\n 2.追加写入

代码语言:javascript
复制
  FileoutputStream fos = new FileoutputStream("a.txt",true);

3.IO流复制文件第一种方法

代码语言:javascript
复制
public class copy {

    public static void main(String[] args) throws IOException {
        
        //数据源
         FileInputStream fis = new FileInputStream(
                            new File("D:\\erweima.png"));
         //目的地
         FileOutputStream fos = new FileOutputStream(
                              new File("erweima.png")); 
         
         int by = 0;
         
         while((by=fis.read())!=-1){
             fos.write(by);
         }
         
         //释放资源
         fis.close();
         fos.close();
    }
}

4.IO流复制文件第二种方法

代码语言:javascript
复制
public class copy2 {

    public static void main(String[] args) throws IOException {
        //数据源
        FileInputStream fis = new FileInputStream(
                new File("D:\\erweima.png"));
        //目的地
        FileOutputStream fos = new FileOutputStream(
                new File("D:\\JE\\erweima.png"));
        
        byte[] bs = new byte[1024];
        while(fis.read(bs)!=-1){
            fos.write(bs);
        }
        //释放资源
        fis.close();
        fos.close();
    }
}

5.IO3

代码语言:javascript
复制
public class copy3 {

    public static void main(String[] args) throws IOException {
        
        //数据源
        BufferedInputStream bis = new BufferedInputStream
                (new FileInputStream("a.txt"));
        //目的地
        BufferedOutputStream bos = new BufferedOutputStream
                (new FileOutputStream("b.txt"));
        
        int by = 0;
        while((by = bis.read())!=-1){
            bos.write(by);
        }
        
        bis.close();
        bos.close();
    }
}

5.IO4

代码语言:javascript
复制
public class copy3 {

    public static void main(String[] args) throws IOException {
        
        //数据源
        BufferedInputStream bis = new BufferedInputStream
                (new FileInputStream("a.txt"));
        //目的地
        BufferedOutputStream bos = new BufferedOutputStream
                (new FileOutputStream("b.txt"));
        
        byte[] bs = new byte[1024];
        while(bis.read(bs)!=-1){
            bos.write(bs);
        }
        bis.close();
        bos.close();
    }
}

6.单极文件夹复制

  • 复制单极文件
  • 1.封装目录,判断目的目录是否存在
  • 2.获得所有文件目录,并遍历目录
  • 3.复制
代码语言:javascript
复制
public class CopyFolderFiles {
    
     public static void main(String[] args) {
        //封装目录
         File srcFolder = new File("H:\\课件");
         File destFolder = new File("E:\\test");
         
        //判断目的地文件是否存在,不存在创建文件
         if(!destFolder.exists()){
             destFolder.mkdirs();
         }
         
        //获得资源目录下的所有文件
         File[] files = srcFolder.listFiles();
         
         //遍历所有文件
         for (File file : files) {
            String name = file.getName();
            File start = new File(srcFolder,name);//出发地
            File end   = new File(destFolder,name);//目的地
            try {
                copy(start,end);
            } catch (IOException e) {
            
                e.printStackTrace();
            }
        }
    }
    //复制文件,所有各类型文件使用字节流
    private static void copy(File start, File end) throws IOException {
        //资源
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(start));
        //目的
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(end));
        
        byte[] bs = new byte[1024];
        
        while(bis.read(bs)!=-1){
            bos.write(bs);
            bos.flush();
        }
        
        //释放资源
        bis.close();
        bos.close();
    }

7.复制文件下所有的.java文件,并该为.jad文件

  • 把视频名全部重命名
  • 请大家把E:\java目录下所有以java结尾的绝对路径输出控制台
  • A:封装路径
  • B:获取该文件下所有文件或者文件夹的File数组
  • C:遍历File数组得到每一个File对象
  • D:判断该File对象是否为文件夹
  • 是:递归调用
  • 不是:判断是否以java结尾
  • 是:输出该文件的绝对路径
  • 否:不搭理
代码语言:javascript
复制
public class FileDemo4 {
    
    public static void main(String[] args) {
         
        File srcFolder = new File("D:\\");
        
        getAllClassFilesPath(srcFolder);
        
    }

    private static void getAllClassFilesPath(File srcFolder) {
        try {
            
            //获得当前目录下的所有文件夹
            File[] files = srcFolder.listFiles();
            //遍历files对象
            for (File file : files) {
                //判断当前是否为文件夹
                if(file.isDirectory()){
                    getAllClassFilesPath(file);
                }else{
                    if(file.getName().endsWith(".class")){
                        System.out.println(file.getAbsolutePath());
                    }
                }
            }
        } catch (Exception e) {
            e.getMessage();
        }
    }
}

8.复制多级文件

  • 1.封装目录
  • 2.判断该文件是文件夹还是文件
  • 是文件
  • 复制该文件到此目录下
  • 是文件夹
  • 就在该文件目录下创建该文件夹
  • 获取该文件夹下的所有File对象
  • 遍历
  • 回2
代码语言:javascript
复制
public class copy {
    
     public static void main(String[] args) {
        //数据源
         File srcFolder = new File("H:\\java");
         //目的地
         File destFolder = new File("F:\\");
         //复制多级文件
         try {
            copyFollder(srcFolder,destFolder);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private static void copyFollder(File srcFolder, File destFolder) throws Exception {
        
        if(srcFolder.isDirectory()){
            String newname = srcFolder.getName();
            File newFolder = new File(destFolder,newname);
            newFolder.mkdir();
            File[] files = srcFolder.listFiles();
            for (File file : files) {
                copyFollder(file, newFolder);
            }
            
        }else{
            File newFile = new File(destFolder,srcFolder.getName());
            copyFile(srcFolder,newFile);
        }
    }

    private static void copyFile(File srcFolder, File newFile) throws IOException {
        //数据源
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFolder));
        //目的地
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newFile));
        //字节数组复制
        byte[] bs = new byte[1024];
        
        while(bis.read(bs)!=-1){
            bos.write(bs);
            bos.flush();
        }
        //释放资源
        bos.close();
        bis.close();
    }

}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • OutputStream
    • FileOutputStream
      • BufferedOutputStream
        • InputStream
          • FileInputStream
            • BufferedInputStream
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档