当前位置: 首页 > article >正文

JAVASE入门十二脚-IO流charArrayReader,bufferedReader,输入与输出,采集百度网页,分块操作

 

charArrayReader

package Reader_Wirter;

import java.io.CharArrayReader;
import java.io.IOException;

public class charArraySreaminput {
    public static void main(String[] args) {
        char[] chars="大王叫我来寻山".toCharArray();
        CharArrayReader charArrayReader=new CharArrayReader(chars);
        try {

            int length=0;
            while((length=charArrayReader.read())!=-1){
            System.out.print((char)length);}
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

package Reader_Wirter;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class BufferReader {
    public static void main(String[] args) {
        BufferedReader bufferedReader=null;
        try {
            bufferedReader=new BufferedReader(new FileReader("aaa.txt"));
            String read=null;
            while ((read=bufferedReader.readLine())!=null){
                System.out.println(read);
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}
package Reader_Wirter;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class BufferedWriterDemo {
    public static void main(String[] args) {
        FileWriter fileWriter=null;
        BufferedWriter bufferedWriter=null;
        try {
            fileWriter=new FileWriter(new File("abc.txt"));
            bufferedWriter=new BufferedWriter(fileWriter);
            bufferedWriter.write("da大大大");
            bufferedWriter.newLine();
            bufferedWriter.append("ww.coom");
            bufferedWriter.flush();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                bufferedWriter.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            try {
                fileWriter.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

    }
}

输入与输出

bufferedreader.readline==null字符串

package Reader_Wirter;

import java.io.*;
import java.util.NavigableMap;

public class BufferedReader_Wirter {
    public static void main(String[] args) {
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(System.out);
        InputStreamReader inputStreamReader = new InputStreamReader(System.in);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
        String str = "";
        try {
            while (!str.equals("exit")) {
                str = bufferedReader.readLine();
                bufferedWriter.write(str);
                bufferedWriter.flush();
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                inputStreamReader.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            try {
                outputStreamWriter.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            try {
                bufferedWriter.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            try {
                bufferedReader.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

    }
}

 采取百度网页内容

package Reader_Wirter;

import com.sun.org.apache.xpath.internal.operations.Neg;

import java.io.*;
import java.net.URL;

public class Baidu {
    public static void main(String[] args) {
        BufferedReader bufferedReader=null;
        BufferedWriter bufferedWriter=null;
        try {
            bufferedReader=new BufferedReader (new InputStreamReader(new URL("http://www.baidu.com").openStream(),"utf-8"));
            bufferedWriter=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("baidu.html")));
            String msg=null;
            while ((msg=bufferedReader.readLine())!=null){
                bufferedWriter.write(msg);
                bufferedWriter.newLine();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                bufferedReader.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            try {
                bufferedWriter.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }


    }
}

PrintStream

package Reader_Wirter;

import com.sun.xml.internal.bind.v2.runtime.output.DOMOutput;

import java.io.IOException;
import java.io.PrintStream;

public class PrintStreamDemo {
    public static void main(String[] args) {
        PrintStream printStream=null;
        printStream=new PrintStream(System.out);
        try {
            printStream.write("hello test".getBytes());
            printStream.println(123);
            //格式化输出
            System.out.printf("%s---%d---%f","abc",123,45.25);
            System.err.println("daaaa");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            printStream.close();
        }
    }
}

 分块操作

package Reader_Wirter;

import java.io.*;

/*
* 文件进行分块操作
* */
public class BlockingDemo {
    public static void main(String[] args) {
        File file=new File("ccc.txt");
        //整个文件的大小length
        long length = file.length();
        //规定快的大小blocksize
        int blockSize=1024;
        //文件可以分成多少块size
        int size=(int)Math.ceil(length*1.0/blockSize);
        System.out.printf("要被切成%d个快",size);

        int beginPos=0;
        int actualSize=(int)(blockSize>length?length:blockSize);
        for(int i=0;i<size;i++){
            //每次读取快的时候的起始偏移量
            beginPos=i*blockSize;
            if(i==size-1){
                actualSize=(int)length;

            }else {
                actualSize=blockSize;
                length-=actualSize;
            }
            System.out.println(i+"--->>起始位置:"+beginPos+"----->>读取的大小是"+actualSize);
         readSplit(i,beginPos,actualSize);
        }
    }
public  static void readSplit(int i,int beginPos,int actualSize){
    RandomAccessFile randomAccessFile=null;
    try {
        randomAccessFile=new RandomAccessFile(new File("ccc.txt"),"r");
        byte[] bytes=new byte[1024];
        int length=0;
        while ((length=randomAccessFile.read(bytes))!=-1){
            if(actualSize>length){
                System.out.println(new String(bytes,0,length));
                actualSize-=length;
            }else {
                System.out.println(new String(bytes,0,actualSize));
                break;
            }
        }
    } catch (FileNotFoundException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }finally {
        try {
            randomAccessFile.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
}


http://www.kler.cn/a/526283.html

相关文章:

  • 【NLP251】意图识别 与 Seq2Seq
  • 简要介绍C语言与c++共有的数学函数
  • Ubuntu-手动安装 SBT
  • C28.【C++ Cont】顺序表的实现
  • LabVIEW纤维集合体微电流测试仪
  • SpringBoot或SpringAI对接DeekSeek大模型
  • Golang 并发机制-1:Golang并发特性概述
  • 实战:如何快速让新网站被百度收录?
  • 11 Spark面试真题
  • Redis常用命令合集【一】
  • 春节旅游高峰,人力资源如何巧妙应对?‌
  • Python标准库 - os (3) 调度策略、系统信息
  • 数据结构--数组链表
  • 大模型时代下的具身智能
  • 实验五---控制系统的稳定性分析---自动控制原理实验课
  • LabVIEW温度修正部件测试系统
  • 图漾相机——C++语言属性设置
  • Java 知识速记:全面解析 final 关键字
  • Linux《基础指令》
  • 动态规划DP 最长上升子序列模型 登山(题目分析+C++完整代码)
  • RocketMQ实战—2.RocketMQ集群生产部署
  • 车载软件架构 --- 基于AUTOSAR软件架构的ECU开发流程小白篇
  • 【腾讯云】腾讯云docker搭建单机hadoop
  • 窥探目标文件
  • Git进阶之旅:.gitignore 文件
  • PostgreSQL技术内幕24:定时任务调度插件pg_cron