Java IO流分为字节流和字符流

下面首先介绍一下字节流

/**
* 字节流测试
* @author hc
*
*/
public class Test { public static void main(String[] args) throws IOException, ClassNotFoundException {
createFile();
inputStre();
buffer();
ObjectOutputstr();
}
public static void createFile() throws IOException{ //File file=new File("a.txt");
//file.createNewFile();
//创建此抽象路径名指定的目录,包括所有必需但不存在的父目录。
File file2=new File("a/b/c");
file2.mkdirs();
}
//字节流 inputstream
//用来统计长度
public static void inputStre(){
InputStream inputStream=null;
try{
int count=0;
inputStream=new FileInputStream(new File("a.txt"));
while(inputStream.read()!=-1){
count++;
}
System.out.println("字符串长度是"+count+"字节");
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
/**
* FileOutputStream 循序渐进版, InputStream是所有字节输出流的父类,
* 子类有ByteArrayOutputStream,FileOutputStream,ObjectOutputStreanm,
* 这些我们在后面都会一一说到。先说FileOutputStream
* 缓冲区的使用
* @throws IOException
*/ public static void buffer() throws IOException{
byte[] buffer=new byte[1024];
int num=0;
FileInputStream fileInputStream=new FileInputStream(new File("a.txt"));
FileOutputStream fileOutputStream=new FileOutputStream(new File("b.txt"));
while ((num=fileInputStream.read(buffer))!=-1) {
fileOutputStream.write(buffer, 0, num); }
System.out.println("复制完成");
}
/**
* 读写对象
* ObjectInputStream 和ObjectOutputStream ,该流允许读取或写入用户自定义的类,
* 但是要实现这种功能,
* 被读取和写入的类必须实现Serializable接口,其实该接口并没有什么方法,可能相当于一个标记而已,但是确实不合缺少的
* @throws IOException
* @throws FileNotFoundException
* @throws ClassNotFoundException
*/
public static void ObjectOutputstr() throws FileNotFoundException, IOException, ClassNotFoundException{
ObjectOutputStream objectOutputStream=new ObjectOutputStream(new FileOutputStream(new File("o.txt")));
ObjectInputStream objectInputStream=new ObjectInputStream(new FileInputStream(new File("o.txt")));
//将对象写到文件中
objectOutputStream.writeObject(new Person(11, "张三"));
Object ob= objectInputStream.readObject();
Person p=(Person)ob;
System.out.println(p.getName()+"---------"+p.getAge());
} /*
*
*.有时没有必要存储整个对象的信息,而只是要存储一个对象的成员数据,
*成员数据的类型假设都是Java的基本数据类型,这样的需求不必使用到与Object输入、输出相关的流对象,
*可以使用DataInputStream、DataOutputStream来写入或读出数据。
*
*/
/*
* PushbackInputStream类继承了FilterInputStream类是iputStream类的修饰者。提供可以将数据插入到输入流前端的能力(当然也可以做其他操作)。
* 简而言之PushbackInputStream类的作用就是能够在读取缓冲区的时候提前知道下一个字节是什么,
* 其实质是读取到下一个字符后回退的做法,这之间可以进行很多操作,
* 这有点向你把读取缓冲区的过程当成一个数组的遍历,遍历到某个字符的时候可以进行的操作,
* 当然,如果要插入,能够插入的最大字节数是与推回缓冲区的大小相关的,插入字符肯定不能大于缓冲区吧!
*/
public static void pushbackinputStream() throws IOException{
String str = "hello,rollenholt";
PushbackInputStream push = null; // 声明回退流对象
ByteArrayInputStream bat = null; // 声明字节数组流对象
bat = new ByteArrayInputStream(str.getBytes());
push = new PushbackInputStream(bat); // 创建回退流对象,将拆解的字节数组流传入
int temp = 0;
while ((temp = push.read()) != -1) { // push.read()逐字节读取存放在temp中,如果读取完成返回-1
if (temp == ',') { // 判断读取的是否是逗号
push.unread(temp); //回到temp的位置
temp = push.read(); //接着读取字节
System.out.print("(回退" + (char) temp + ") "); // 输出回退的字符
} else {
System.out.print((char) temp); // 否则输出字符
}
}
}
/*
* SequenceInputStream:有些情况下,当我们需要从多个输入流中向程序读入数据。此时,可以使用合并流,
* 将多个输入流合并成一个SequenceInputStream流对象。
* SequenceInputStream会将与之相连接的流集组合成一个输入流并从第一个输入流开始读取,直到到达文件末尾,接着从第二个输入流读取,依次类推,
* 直到到达包含的最后一个输入流的文件末尾为止。 合并流的作用是将多个源合并合一个源。其可接收枚举类所封闭的多个字节流对象。
*/
public static void sequenceInputStream(){ // 创建一个合并流的对象
SequenceInputStream sis = null;
// 创建输出流。
BufferedOutputStream bos = null;
try {
// 构建流集合。
Vector<InputStream> vector = new Vector<InputStream>();
vector.addElement(new FileInputStream("D:\text1.txt"));
vector.addElement(new FileInputStream("D:\text2.txt"));
vector.addElement(new FileInputStream("D:\text3.txt"));
Enumeration<InputStream> e = vector.elements(); sis = new SequenceInputStream(e); bos = new BufferedOutputStream(new FileOutputStream("D:\text4.txt"));
// 读写数据
byte[] buf = new byte[1024];
int len = 0;
while ((len = sis.read(buf)) != -1) {
bos.write(buf, 0, len);
bos.flush();
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
} finally {
try {
if (sis != null)
sis.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (bos != null)
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/*
* PrintStream
* System.out.println();
*
*/
}
字符流
/**
* 字符流测试
* @author hc
*
*/
public class Test2 {
public static void main(String[] args) throws IOException {
//test1();
}
//FileReader ,PrintWriter
public static void test1() throws IOException{
char[] buffer=new char[512]; //一次取出的字节数大小,缓冲区大小
int numberRead=0;
FileReader reader=null; //读取字符文件的流
PrintWriter writer=null; //写字符到控制台的流 try {
reader=new FileReader("D:/David/Java/java 高级进阶/files/copy1.txt");
writer=new PrintWriter(System.out); //PrintWriter可以输出字符到文件,也可以输出到控制台
while ((numberRead=reader.read(buffer))!=-1) {
writer.write(buffer, 0, numberRead);
}
} catch (IOException e) {
// TODO自动生成的 catch 块
e.printStackTrace();
}finally{
try {
reader.close();
} catch (IOException e) {
// TODO自动生成的 catch 块
e.printStackTrace();
}
writer.close(); //这个不用抛异常
}
}
/*
* bufferedReader/bufferedWriter
*/
public static void bufferWri(String...fileName){ String str;
//构建对该文件您的输入流
BufferedWriter writer=new BufferedWriter(new FileWriter("D:/David/Java/java 高级进阶/files/copy2.txt"));
for(String name: fileName){
BufferedReader reader=new BufferedReader(new FileReader(name)); while ((str=reader.readLine())!=null) {
writer.write(str);
writer.newLine();
}
}
} }

JavaIO总结的更多相关文章

  1. JavaIO学习笔记(五)

    JavaIO前期准备 什么是同步 指的是用户进程触发IO操作并等待或者轮询的去查看IO操作是否就绪 什么是异步 异步是指用户进程触发IO操作以后便开始做自己的事情,而当IO操作已经完成的时候会得到IO ...

  2. javaIO系统----再看装饰者模式

    javaIO系统拥有各种各样的类,尤其是每次要进行读写操作时,总会一层套一层的new,以前不明白为什么要这样做,不过学习了适配器模式后,对于这种做法立刻了解了:动态扩展IO的功能,使之符合使用者的习惯 ...

  3. Java学习日记之 Java-IO流

    Java中的IO流在处理上分为字节流和字符流.字节流和字符流的区别 : 1.字节流读取的时候,读到一个字节就返回一个字节:  字符流使用了字节流读到一个或多个字节(中文对应的字节数是两个,在UTF-8 ...

  4. javaIO框架小析

    IO即数据读写.数据是应用的中心要素,而数据读写的能力和可扩展性是编程平台的基础支撑. 概念框架 方式: 字节流 Byte 和 字符流 Char 方向: 输入 Input 和 输出 Output : ...

  5. javaIO流实现读写txt文件

    javaIO流实现文件读写 文件写入: InputStreamReader BufferedReader 文件读取: FileOutputStream package javatest.basic22 ...

  6. javaIO操作之字节输出流--OutputStream

    OutputStream /** * <li>输出单个字节:public abstract void write(int b) throws IOException ; * <li& ...

  7. [零] JavaIO入门简介 程序设计语言 为什么需要IO库

     本文旨在引申出来Java IO的概念含义,作为学习JavaIO一个起步的了解知识点 部分内容引自<计算机操作系统第三版>  操作系统的文件管理   "在现代计算机系统中,要用到 ...

  8. [三]JavaIO之IO体系类整体设计思路 流的概念以及四大基础分类

    从本文开始,将正式进入JavaIO的简介 在继续javaIO系列的文章之前 可以过去看一下 本人博客上的设计模式中的 适配器模式和装饰器模式 这会对接下来的阅读大有帮助   本文是从逻辑上介绍整个的J ...

  9. [四] JavaIO之类层次体系结构横向比对

      IO家族类层次体系结构横向匹配   上一篇文章中主要介绍了JavaIO流家族的整体设计思路,简单回顾下 基本逻辑涉及数据源 流的方向,以及流的数据形式这三个部分的组合 按照流的数据形式和流的方向, ...

  10. [七]JavaIO之 PipedInputStream 和 PipedInputStream

    管道简介

随机推荐

  1. 如果使用 Excel5 ,输出的内容应该是GBK编码

    下面就是php导出excel的程序 <?phpini_set("display_errors",1);//是否显示报错信息set_include_path(get_inclu ...

  2. php session详解

    <?php /* * session_abort — Discard session array changes and finish session 舍弃会话序列变化和结束会话 session ...

  3. Portable Operating System Interface for uni-X

    https://kb.iu.edu/d/agjv Short for "Portable Operating System Interface for uni-X", POSIX ...

  4. Wordpress制作文章页面single.php

    可以调用的文章内容: 调用文章标题:<?php the_title(); ?> 调用文章内容:<?php the_content(); ?> 调用文章摘要:<?php t ...

  5. Java集合---HashMap源码剖析

    一.HashMap概述二.HashMap的数据结构三.HashMap源码分析     1.关键属性     2.构造方法     3.存储数据     4.调整大小 5.数据读取           ...

  6. rndc控制远程dns服务器配置方法

    1- 如果不存在/etc/rndc.conf touch /etc/rndc.conf chown named:named /etc/rndc.conf 2- rndc-confgen > /e ...

  7. Display Voxel Gird and PCA

    https://github.com/yhexie/NDTEX 最近科研没有思路,写点代码加强基础知识的学习吧. 下面写了一个点云体素分割,PCA计算体素内点云的特征值和特征向量.

  8. SQL-乐观锁,悲观锁之于并发

    每次写博客,第一句话都是这样的:程序员很苦逼,除了会写程序,还得会写博客!当然,希望将来的一天,某位老板看到此博客,给你的程序员职工加点薪资吧!因为程序员的世界除了苦逼就是沉默.我眼中的程序员大多都不 ...

  9. mysql的一些基本操作语句

    -- 创建一个php2016的数据库create database php2016;-- 查看数据库的创建创建语句show create database php2016;-- 指定默认的操作数据库u ...

  10. 解决SVN不显示状态图标

    打开注册表,找到"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\ShellIconOverlay ...