一起重新开始学大数据-java篇-DAY19-IO流(2)BufferedStream

Day20 BufferedStream

主要时基于学习大数据,所以案例和知识点都比较往那边靠拢

每天学习还要更新博客

状态越来越不行了,此时的我:

在这里插入图片描述

写入数据的换行和追加

换行:

文件中的换行是通过符号来进行表示,不同的操作系统有不同的换行符号

系统换行符
windows\r\n
linux\n
mac\r

追加:

方法说明
FileOutputStream(String name)创建文件输出流以指定的名称写入文件。
FileOutputStream(new File)创建文件输出流以写入由指定的 File对象表示的文件。
FileOutputStream(String name,boolean append)创建文件输出流以指定的名称写入文件。append - 如果是 true ,那么字节将被写入文件的末尾,而不是开头

案例获取当前写了多少行代码(更准确)

package Day20;
import java.io.File;
import java.io.FileInputStream;
public class get {
    static int sum = 0;
    public static void main(String[] args) throws Exception {
        File file = new File("D:\\ideafile\\Bigdata\\src");
        func(file);
        System.out.println(sum);
    }
    public static void func(File file) throws Exception {
        File[] files = file.listFiles();
        for (File file1 : files) {
            if (file1.isDirectory()) {
                func(file1);
            } else if (file1.isFile() && file1.getName().endsWith(".java")) {
                FileInputStream fi = new FileInputStream(file1);
                int o=0;
                while ((o=fi.read())!=-1){
                    if (o=='\n') {
                        sum++;
                    }}
                fi.close();
            }
        }

    }
}

反正我平时学习练习已经敲了19749行代码 😵

在这里插入图片描述

案例copy:

//将data文件下的文件复制到copy文件下

package Day20;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
// 分组,聚合
public class Copy {
    public static void main(String[] args) throws Exception{
        File oldFile = new File("d:\\ideaFile\\BigData\\data");
        File copyFile = new File("d:\\ideaFile\\BigData\\copy");
        copy(oldFile,copyFile);
    }
    public static void copy(File oldFile,File copyFile)throws Exception{
        File[] files = oldFile.listFiles();
        for (File f : files) {
            if (f.isFile()){// 去读 然后写(写在新的位置)
                //读
                FileInputStream fis = new FileInputStream(f);
                //写 需要新的目录下 路径为:copyFile+f,getName();
                FileOutputStream fos = new FileOutputStream(new File(copyFile, f.getName()));
                int sum;
                byte[] bytes = new byte[1024];
                while ((sum=fis.read(bytes))!=-1){
                    fos.write(bytes,0,sum);
                }
                fos.close();
                fis.close();
                f.delete();
            }else {//目录
                File file = new File(copyFile, f.getName());
                file.mkdirs();
                copy(f,file);
            }
        }
        oldFile.delete();
    }
}

字节流读数据

字节流抽象父类

InputStream这个抽象类表示字节输入流的所有类的符类
OutputStream这个抽象类表示字节输除流的所有类的符类
FileInputStream文件字节输入流(具体的字节输入流实现类)
FileInputStream(String name)创建文件字节输出流读取文件

使用步骤:

1.创建对象(①.调用系统创建文件②.创建字节输入流对象③.让对象指向文件)

2.调用读数据的方法(read())

3.释放资源(关闭文件流并释放与此相关的资源)

字节缓冲流

BufferedOutputStream:

该类实现缓冲输出流。 通过设置这样的输出流,应用程序可以向底层输出流写入字节,而不必为写入的每个字节导致底层系统的调用。

BufferedInputStream:

创建一个内部缓冲区数组。 当从流中读取或跳过字节时,内部缓冲区将根据需要从所包含的输入流中重新填充,一次有多个字节。

构造方法:

BufferedOutputStream(OutputStream out)
BufferedInputStream(InputStream input)

注意:

字节缓冲流仅仅提供缓冲区,真正的读写数据还是依靠基本的字节流对象进行操作

案例一、读文件

BufferedInputStream(InputStream input)

package Day20;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
public class Demo13BufferedInputStream {
    public static void main(String[] args) throws Exception{

        BufferedInputStream bis = new BufferedInputStream(
                new FileInputStream("E:\\ideaFile\\BigData\\copy\\test.txt"));
        int sum;
        byte[] bytes = new byte[1024];
        while ((sum=bis.read(bytes))!=-1){
            System.out.println(sum);
            System.out.println(new String(bytes,0,sum));
        }
        bis.close();

    }
}

案例二、写文件
BufferedInputStream(InputStream input)

package Day20;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
public class Demo13BufferedOutputStream {
    public static void main(String[] args) throws Exception{
        BufferedOutputStream bos = new BufferedOutputStream(
                new FileOutputStream("E:\\ideaFile\\BigData\\copy\\test.txt"),5);
        bos.write(97);
        bos.write(98);
        bos.write(99);
        bos.write(100);
        bos.write(101);
        bos.write(102);
        bos.write(103);
        bos.write(104);
       bos.close();
    }
}

案例三 、分类聚合

文本文档内容:

在这里插入图片描述

package Day20;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.util.HashMap;

public class Demo14Task {
    public static void main(String[] args) throws Exception{
        BufferedInputStream bis = new BufferedInputStream(
                new FileInputStream("E:\\ideaFile\\BigData\\data\\data.txt"));
        HashMap<String, Integer> map = new HashMap<>();
        int sum;
        while ((sum=bis.read())!=-1){
            if(sum!=10&&sum!=13){
                String str= String.valueOf(sum);
                if(!map.containsKey(str)){
                    map.put(str,1);
                }else {
                    map.put(str,map.get(str)+1);
                }
            }
        }
        bis.close();
        System.out.println(map);
    }
}

案例四、数据切块(可能有问题,随便看)

需求:将一个读取一个文档的字节数,通过大小128个为一个文档,切分出多个文档,同时满足切割后不满足切割值并且其值为切割值得百分之10以内,将其数据写入上一个文档中

package Day20;
import java.io.*;
//1.读取数据长度
//128*3=384
//384+35=419
//4.切分
//5.输出
//6.前移
//3.关闭

public class hadoop {
    static int count=0;
    public static void main(String[] args) throws Exception {
        File file = new File("D:\\ideafile\\Bigdata\\1.txt");
        File newfile = new File("D:\\ideafile\\Bigdata\\result");
        contlin(file);//1027
        System.out.println(count);
        int index=0;
        int l=((count*3)%384);
        System.out.println(l);//剩下的最后一个block块为多少,9
        byte[] bytes = new byte[419];
        FileInputStream fis = new FileInputStream(file);
        int temp=0;
        int off=0;
        int len=384;
        while ((temp=fis.read(bytes))!=-1){
            FileOutputStream fos = new FileOutputStream(new File(newfile, "-----filename" + index));
            fos.write(bytes,off ,len);
            index++;
            off=35;
            for (int i = 385; i <bytes.length ; i++) {
                bytes[0]=bytes[i];
            }
            fos.close();
        }
        fis.close();
    }

    public static int contlin(File file)throws Exception{
        BufferedInputStream buf = new BufferedInputStream(new FileInputStream(file));

        int sum=0;
        while ((sum=buf.read())!=-1){
            if (sum=='\n'){
                count++;
            }
        }return count;
    }
}

在这里插入图片描述

编码表

计算机中存储的信息都是使用的二进制的方式,而我们所看的英文\汉字都是二进制转换之后的结果

例如:

a 97 01100001
按照某种规则,将字符存储到计算机中,称之为编码,反之将存储在计算机中的二进制按照某种规则进行解析,称之为解码
注意:编码和解码所使用的规则要保持一直,不然会出现乱码问题
这里的规则就是编码表

常用编码表:

ASCII,GBK,Unicode
Unicode
统一编码表,标准万国码,使用最多4个字节的数字表达字母,有三种

编码格式,

UTF-8 UTF-16和UTF32,常用UTF-8

字符串中的编码问题

编码:

byte[] getBytes():使用平台的默认字符编码集将String存储字节数组

byte[] getBytes(String chareName):使用指定的编码集String存储字节数组

解码:

String(byte[] bytes):通过默认字符集解码指定的数组来构造新的
String
String(byte[] bytes,String chareName):通过指定字符集解码指定的数组来构造新的String

注意:

编码和解码所随用的编码集要保持一直,不然会出现乱码问题

|
|
|
|
|

上一章节-java篇-DAY19-IO流(1)File

下一章节-随缘更新

没人看我也 懒得 要个点赞收藏了!😛

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你的动作太慢了!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值