常见字节流

开发者福利!热门AI工具限时免费用 购周边即赠Coding Plan Lite,Claude Code、Cursor等20+工具畅享,效率翻倍! 阅读详情


  以字节为单位获取数据的流,称为字节流,它们都继承于InputStream/OutputStream抽象类。

  常用的字节流有:

  • FileInputStream/FileOutputStream,文件字节流(节点流)
  • ByteArrayInputStream/ByteArrayOutputStream,字节数组流(节点流)
  • BufferedInputStream/BufferedOutputStream,字节缓存流(处理流)
  • DataInputStream/DataOutputStream,数据字节流(处理流)
  • ObjectInputStream/ObjectOutputStream,对象流(处理流)

  下面分别介绍一下这五组字节流及用法。

FileInputStream/FileOutputStream

  FileInputStream/FileOutputStream是最常用的文件字节流,当数据源为文件对象时,选择这个数据流进行处理。我们可以通过构造器快速了解它的操作数据类型:

    public FileInputStream(File file)
    
    public FileOutputStream(File file)

  可以看到,这组数据流的操作对象为File类型。

  关于数据流的操作,总是分为四个步骤:

  • 1.指定数据源
  • 2.选择合适的数据流
  • 3.进行数据操作
  • 4.关闭流,释放资源

  来一个文件的拷贝实例,了解其用法:

public class FileStream {
	
	public static void main(String[] args) {

		//1.指定数据源
		File in_scr = new File("a.txt"); // 输入数据为文件
		File out_scr = new File("a-copy.txt"); // 输出数据也为文件
		
		InputStream is = null;
		OutputStream os = null;
		
		try {

			//2.选择合适的数据流
			is = new FileInputStream(in_scr);
			os = new FileOutputStream(out_scr);

			//3.进行数据操作
			byte[] flush = new byte[1024]; //字节流,数据以字节数组为单位
			int len = -1;
			while( (len = is.read(flush)) != -1 ) {
//				for(int i=0;i<len;i++) {
//					System.out.println(flush[i]); 可以将输入流直接打印出来观察
//				}
				os.write(flush,0,len);
			}
			os.flush(); //将缓存内容输出
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {

			//4.关闭流,释放资源
			//先打开的先关闭
			try {
				if(is != null) {
					is.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			try {
				if(os != null) {
					os.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
}

  需要注意的点有:

  • 1.flush为byte[]数组,与形参不同,数组传入的是地址,所以在is.read(flush)方法中操作flush,外面的flush也会变换,read方法就是给flush赋值的一个方法,并且有返回值:读取到的元素个数,未读到时返回-1。
  • 2.输出流中是有缓存的,就是说当输出流的数据量到达一定程度,才会输出。所以在结尾,我们需要手动将未到达缓存量的数据输出,使用输出流的flush()方法。

ByteArrayInputStream/ByteArrayOutputStream

  ByteArrayInputStream/ByteArrayOutputStream,字节数组流,也是一个常用的节点流。当数据对象字节数组byte[ ]时,使用这种流进行操作。观察一下它们的构造器:

    public ByteArrayInputStream(byte buf[])
    
    public ByteArrayOutputStream()

  可以看到,输入流的操作对象未byte[ ]字节数组;输出流无参数,并不能直接将流中数据输出为文件等,但ByteArrayOutputStream有toString()等方法,来方便对输出流进行操作。

  还是来一个文件的拷贝实例,了解其用法:

public class ByteArrayStream {

	public static void main(String[] args) {
		//1.指定数据源
		byte[] in_scr = new byte[] { -28, -67, -96, -26, -120, -111, -28, -69, -106 };//输入数据

		ByteArrayInputStream bais = null;
		ByteArrayOutputStream baos = null;
		try {
			
			//2.选择合适的数据流
			bais = new ByteArrayInputStream(in_scr);
			baos = new ByteArrayOutputStream();
			
			//3.进行数据操作
			byte[] flush = new byte[1024];
			int len = -1;
			System.out.print("输入流:");
			while ((len = bais.read(flush)) != -1) {
				for (int i = 0; i < len; i++) {
					System.out.print(flush[i]+","); //读取输入流
				}
				baos.write(flush, 0, len);
			}
			System.out.println();
			baos.flush(); //将缓存内容输出,到此,输出流的数据写入已完成
			
			byte[] byteArray = baos.toByteArray(); //将输入流转换为字节数组
			System.out.print("输出流:");
			for (byte b : byteArray) {
				System.out.print(b+",");
			}
			System.out.println();
			System.out.println(baos.size());//输出流的元素个数
			System.out.println(baos.toString("UTF-8"));//注意,与byte[]的toString()不同

		} catch (IOException e) {
			e.printStackTrace();
		} finally {

			//4.关闭流,释放资源
			try {
				if (bais != null) {
					bais.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			try {
				if (baos != null) {
					baos.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}
---------------------------------------------------
输出结果为:
输入流:-28,-67,-96,-26,-120,-111,-28,-69,-106,
输出流:-28,-67,-96,-26,-120,-111,-28,-69,-106,
9
你我他

BufferedInputStream/BufferedOutputStream

  BufferedInputStream/BufferedOutputStream,字节缓存流,是一种处理流,使用了装饰设计模式,通过内部缓存数组来提高操作流的效率。构造器源码:

    public BufferedInputStream(InputStream in)
    
    public BufferedOutputStream(OutputStream out)

  可以看到BufferedInputStream/BufferedOutputStream只能对流进行操作,而不能直接操作数据,所以我们称它为处理流。

  当对文件或者其他数据源进行频繁的读写操作时,效率比较低,这时如果使用缓冲流就能够更高效的读写信息。因为缓冲流是先将数据缓存起来,然后当缓存区存满后或者手动刷新时再一次性的读取到程序或写入目的地。

  缓存区的大小默认是8192字节(8K,1024*8),也可以使用其它的构造方法自己指定大小。

  因此,缓冲流还是很重要的,我们在IO操作时都可以加上缓冲流来提升性能。

  来一个文件复制的效率对比实例,了解用法与性能的提升:

public class BufferedStream {

	public static void main(String[] args) {
		 // 使用缓冲字节流实现复制
        long time1 = System.currentTimeMillis();
        bufferedCopy("a.mp4", "a-copy1.mp4");
        long time2 = System.currentTimeMillis();
        System.out.println("缓冲字节流花费的时间为:" + (time2 - time1));
 
        // 使用普通字节流实现复制
        long time3 = System.currentTimeMillis();
        fileCopy("a.mp4", "a-copy2.mp4");
        long time4 = System.currentTimeMillis();
        System.out.println("普通字节流花费的时间为:" + (time4 - time3));
	}

	public static void bufferedCopy(String in_path, String out_path) {
		//1.指定数据源
		File in_scr = new File(in_path);
		File out_scr = new File(out_path);

		InputStream is = null;
		OutputStream os = null;

		try {
		
			//2.选择合适的数据流
			is = new BufferedInputStream(new FileInputStream(in_scr));
			os = new BufferedOutputStream(new FileOutputStream(out_scr));

			//3.进行数据操作
			byte[] flush = new byte[1024];
			int len = -1;
			while ((len = is.read(flush)) != -1) {
				os.write(flush, 0, len);
			}
			os.flush(); // 将缓存内容输出
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {

			//4.关闭流,释放资源
			// 先打开的先关闭
			try {
				if (is != null) {
					is.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}

			try {
				if (os != null) {
					os.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void fileCopy(String in_path, String out_path) {
		//1.指定数据源
		File in_scr = new File(in_path);
		File out_scr = new File(out_path);

		InputStream is = null;
		OutputStream os = null;

		try {
		
			//2.选择合适的数据流
			is = new FileInputStream(in_scr);
			os = new FileOutputStream(out_scr);

			//3.进行数据操作
			byte[] flush = new byte[1024];
			int len = -1;
			while ((len = is.read(flush)) != -1) {
				os.write(flush, 0, len);
			}
			os.flush(); // 将缓存内容输出
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {

			//4.关闭流,释放资源
			// 先打开的先关闭
			try {
				if (is != null) {
					is.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}

			try {
				if (os != null) {
					os.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}
-----------------------------------------------
输出结果为:
缓冲字节流花费的时间为:224
普通字节流花费的时间为:972

DataInputStream/DataOutputStream

  DataInputStream/DataOutputStream,数据字节流(处理流),也是操作流的处理流,构造器如下:

    public DataInputStream(InputStream in)
    
    public DataOutputStream(OutputStream out)

   除了对流的包装处理外,DataInputStream/DataOutputStream还可以实现直接对Java基础数据类型(int、double、String等)直接进行读写操作。

   需要注意的是,使用数据流时,读取的顺序一定要与写入的顺序一致,否则不能正确读取数据。

  先来一个对流的处理实例,还是复制为功能:

public class DataStream1 {

	public static void main(String[] args) {
		//1.指定数据源
		File in_scr = new File("a.txt");
		File out_scr = new File("a-copy.txt");
		
		DataInputStream dis = null;
		DataOutputStream dos = null;
		
		try {
			//2.选择合适的数据流
			dis = new DataInputStream(new FileInputStream(in_scr));
			dos = new DataOutputStream(new FileOutputStream(out_scr));

			//3.进行数据操作
			byte[] flush = new byte[1024];
			int len = -1;
			while( (len = dis.read(flush)) != -1 ) {
//				for(int i=0;i<len;i++) {
//					System.out.print(flush[i]);
//				}
				dos.write(flush, 0, len);
			}
			dos.flush();

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			//4.关闭流,释放资源
			//先打开的先关闭
			try {
				if(dis != null) {
					dis.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			try {
				if(dos != null) {
					dos.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}

  再来一个直接读写基本数据类型的实例:

public class DataStream2 {

	public static void main(String[] args) {
		//1.指定数据源
		File in_scr = new File("a.txt");  
		File out_scr = new File("a.txt");//输入数据源与输出数据源一致,先写再读
		
		DataInputStream dis = null;
		DataOutputStream dos = null;
		
		try {
		
			//2.选择合适的数据流
			dis = new DataInputStream(new FileInputStream(in_scr));
			dos = new DataOutputStream(new FileOutputStream(out_scr));

			//3.进行数据操作
			//将如下数据写入到文件中
            dos.writeChar('a');
            dos.writeInt(10);
            dos.writeDouble(Math.random());
            dos.writeBoolean(true);
            dos.writeUTF("你我他");//Stirng类型
            //手动刷新缓冲区:将流中数据写入到文件中
            dos.flush();
            //直接读取数据:读取的顺序要与写入的顺序一致,否则不能正确读取数据。
            System.out.println("char: " + dis.readChar());
            System.out.println("int: " + dis.readInt());
            System.out.println("double: " + dis.readDouble());
            System.out.println("boolean: " + dis.readBoolean());
            System.out.println("String: " + dis.readUTF());
			
			
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			//4.关闭流,释放资源
			//先打开的先关闭
			try {
				if(dis != null) {
					dis.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			try {
				if(dos != null) {
					dos.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}
-----------------------------------------
输出结果为:
char: a
int: 10
double: 0.8627686668141537
boolean: true
String: 你我他

ObjectInputStream/ObjectOutputStream

  DataInputStream/DataOutputStream可以直接读写Java的基本数据类型。但Java中除了基本数据类型,还有引用数据类型,那么我们自己定义类,能不能也以类的方式放入IO流中呢?

  ObjectInputStream/ObjectOutputStream就是Java提供的操作对象的流,也叫对象流(处理流),构造器源码:

    public ObjectInputStream(InputStream in)

    public ObjectOutputStream(OutputStream out)

  也是需要对流进行操作,但使用它后,可以直接读写任意的数据类型。

  需要注意的是,使用数据流时,读取的顺序一定要与写入的顺序一致,否则不能正确读取数据。

序列化与反序列化

  需要注意的是,对象流中传输的对象必须支持进行序列化与反序列化操作:

  当两个进程远程通信时,彼此可以发送各种类型的数据。 无论是何种类型的数据,都会以二进制序列的形式在网络上传送。比如,我们可以通过http协议发送字符串信息;我们也可以在网络上直接发送Java对象。发送方需要把这个Java对象转换为字节序列,才能在网络上传送;接收方则需要把字节序列再恢复为Java对象才能正常读取。

  把Java对象转换为字节序列的过程称为对象的序列化。把字节序列恢复为Java对象的过程称为对象的反序列化。

  对象序列化的作用有如下两种:

  • 1.持久化: 把对象的字节序列永久地保存到硬盘上,通常存放在一个文件中,比如:休眠的实现。以后服务器session管理,hibernate将对象持久化实现。

  • 2.网络通信:在网络上传送对象的字节序列。比如:服务器之间的数据通信、对象传递。

  只有实现了Serializable接口的类的对象才能被序列化。 Serializable接口是一个空接口,只起到标记作用。

  序列化中需要注意的是:

  • 为了防止读和写的序列化ID不一致,一般指定一个固定的序列化ID。
  • static属性不参与序列化。
  • 对象中的某些属性如果不想被序列化,使用transient修饰。

  来一个支持序列化类的实例:

//实现Serializable接口
class Student implements Serializable{
	// 添加序列化ID,它决定着是否能够成功反序列化!
	private static final long serialVersionUID = 1L;
	private transient int num;
	private String name;
	private int age;
	
	public Student(int num,String name, int age) {
		super();
		this.num = num;
		this.name = name;
		this.age = age;
	}

	@Override
	public String toString() {
		return "Student [num=" + num + ", name=" + name + ", age=" + age + "]";
	}

}

  有了可以序列化的自定义类后,我们来试试使用ObjectInputStream/ObjectOutputStream流读写它:

public class ObjectStream {

	public static void main(String[] args) {
		//1.指定数据源
		File in_scr = new File("a.txt");
		File out_scr = new File("a.txt"); //输入数据源与输出数据源一致,先写再读
		
		ObjectInputStream ois = null;
		ObjectOutputStream oos = null;
		try {
		
			//2.选择合适的数据流
			ois = new ObjectInputStream(new FileInputStream(in_scr));
			oos = new ObjectOutputStream(new FileOutputStream(out_scr));
			
			//3.进行数据操作
			//将如下数据写入到文件中
			oos.writeChar('a');;
			oos.writeInt(10);
			oos.writeDouble(Math.random());
			oos.writeBoolean(true);
			oos.writeUTF("你我他");
			//实例化对象
			Student student = new Student(1,"小猪",18);
			oos.writeObject(student);
            //手动刷新缓冲区:将流中数据写入到文件中
			oos.flush();
            //直接读取数据:读取的顺序要与写入的顺序一致,否则不能正确读取数据。
            System.out.println("char: " + ois.readChar());
            System.out.println("int: " + ois.readInt());
            System.out.println("double: " + ois.readDouble());
            System.out.println("boolean: " + ois.readBoolean());
            System.out.println("String: " + ois.readUTF());
            System.out.println("Object: " + ois.readObject());
			
			
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
		
			//4.关闭流,释放资源
			//先打开的先关闭
			try {
				if(oos != null) {
					oos.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			try {
				if(ois != null) {
					ois.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}
----------------------------------------------------
输出结果为:
char: a
int: 10
double: 0.6826249100838067
boolean: true
String: 你我他
Object: Student [num=0, name=小猪, age=18]
深入浅出:Java中的字节流和字符流详解 字节流是以**字节(byte)**为单位操作数据的流。它用于处理所有类型的文件,包括文本文件、图片、视频等。字节流不关心数据的编码方式,直接传输文件的原始字节。:表示输入字节流,用于读取数据。:表示输出字节流,用于写入数据。字符流是以**字符(char)**为单位操作数据的流,专为处理文本文件而设计。它会自动根据编码格式将字节转换为字符或将字符转换为字节。Reader:表示输入字符流,用于读取字符。Writer:表示输出字符流,用于写入字符。字节流 阅读详情

相关推荐

入输出字节流,文件的读取和写入操作,文件的复制

介绍输入输出字节流,完成文件的读取和写入操作,综合案例文件的复制

liu769的博客 2085

1、字节流详解

Java IO 字节流详解

shiyu_951的博客 6929

面向字节的输入输出流

按照数据流的类型,又可以将IO输入分为:面向字节的输入流和面向字符的输入流。 一:面向字节的输入流 父类InputStream为抽象类,不能被实例化。面向字节的输入流都是InputStream类的子类,其类层次结构下图所示: 下表 列出了 InputStream 的主要子类及说明 下表 列出了 InputStream 的常用方法: 二:面向字符

浅沫微雨的博客 2116

什么是字节流,缓冲字节流及其案例

Java的IO流提供了一个带有缓冲区的字节输入流(BufferedInputStream)、带有缓冲区的字节输出流(BufferedOutputStream)(4)使用字节输入输出流可以操作字符文件,但比较麻烦,需要进行转码.如果操作字符文件建议使用字符流。(3)所有流操作都是在字节流的基础上进行的,通过字节流读取到原始数据,然后交给其他流进行处理。(3)由于带有缓冲区的流是对原始流的包装流,所以要使用带有缓冲区的流必须在原始流的基础上使用。(1)带有缓冲区的字节流属于包装流,对原始流的包装流。

weixin_52682014的博客 1947

IO流之字节流与常见编码

什么是流 流的分类 按方向按单位按功能 InputStream OutputStream FileInputStream: FileOutputStream: 运行结果 2.22 文件字节输出流代码演示 运行结果 运行结果 2.3 字节缓冲流 缓冲流:BufferedInputStream/ BufferedOutputStream 提高IO效率,减少访问磁盘次数 数据存储在缓冲区中,flush是将缓冲区的内容写入文件中,也可以直接close 2

qq_60501861的博客 940

IO里面的常见类,字节流、字符流的差异

JAVA IO主要有这四个类InputStream、OutputStream、Reader、Writer来处理,要处理字节流的就用InputStream、OutputStream,要处理字符流,就用Reader、Writer,现实中根据需要,我们选择他们的相关子类进行数据的读写 首先什么是流: 流(Stream)的概念来源于UNIX中的管道(pipe)概念,在unix中,管道是一条不间断的字节流,...

weixin_44867712的博客 583

java中的IO流(字符流,字节流)及一些常见面试题

目录 一.File类 1.1File类的概述与构造方法 1.1.1File类的介绍 1.1.2File类的构造方法 1.2File类的常用方法 1.2.1File类的创建功能 1.2.2File类的判断与获取功能 1.2.3File类的删除功能​ 1.2.4绝对路径与相对路径 1.3递归 1.3.1递归介绍 1.3.2递归的注意事项 1.3.3递归遍历目录 二 .IO流 2.1IO流概述和分类 2.1.1IO流介绍 2.1.2IO流的分类 2.1.3IO流的使用...

qingmenghan的博客 1582

Java笔记----字节流与字符的常见类型

字节流: InputStream   |-- FileInputStream (基本文件流)   |-- BufferedInputStream   |-- DataInputStream    |-- ObjectInputStream 字符流 Reader |-- InputStreamReader (byte-&gt;char 桥梁) |-- BufferedReader (常用) ...

weixin_33797791的博客 124

java IO字节流和字符流常见类总结

前述 程序在运行结束后,数据内容就会被内存回收掉,从而消失,为了使一些有用的数据可以被下一次调用该程序时候直接使用,所以需要引入IO的操作将数据从内存中传输到磁盘中,从而实现数据的持久化(或者使用数据库也可以实现数据的持久化)。 数据的传输都是通过两种类型的流:输入流和输出流,这就是IO。 流的继承关系图 需要读入数据使用输入流,需要写入数据使用输出流; 按照操作的数

weisian的博客 7308

java常见的IO字节流/字符流操作

InputStream和OutputStream字节流操作File文件 File file = new File("C://Users//fchen//Desktop//EPL4398046700991_大报文.txt"); InputStream inputStream = new FileInputStream(file); byte[] bytes = new byte[2048]; StringBuffer sb = new StringBuffer(); int len = 0; while ((

奔跑的菜鸡 210

字节流写数据和两种常见问题

//字节流写数据 //字节流写数据输出换行 /*widows:\r\n linux:\n mac:\r */ //字节流写数据实现追加写入 import java.io.FileOutputStream; import java.io.IOException; import java.nio.charset.StandardCharsets.

qq_57907966的博客 240

常见的流对象有几种?什么是字节流,什么是文件,什么是字符流?

File文件,字节输入流,字节输入流,字符输入流,字节输出流的详解 File文件概述 1.File类型:用于表示一个文件或者是文件夹的路径的对象(抽象路径名) 2.路径:用于描述文件或者是文件夹的所在路径的所在的字符串 3.路径分类:绝对路径和相对路径 4.绝对路径:从跟目录开始的路径,称为绝对路径,在window中盘符路径就是跟目录,在Linu目录中从根目录开始的路径就是绝对路径 5.相对路径:...

superliug的博客 5558

(一)IO常见的字节流、字符流、缓冲流

详解io字节流、字符流、缓冲流

qq_52370789的博客 752

IO流及常见操作代码示例、字节流和字符流区别

在选择字节流还是字符流时,应根据处理的数据类型和需求进行选择。如果处理的是二进制数据,或者需要进行底层的字节操作,可以选择字节流。如果处理的是文本数据,或者需要进行字符集编解码操作,可以选择字符流。字符流在底层会使用字节流,但提供了字符集编解码的功能,能够更方便地处理字符数据。字符流适合处理文本数据和字符流式的操作,如读取文本文件、写入字符数据等。字符流在处理数据时会进行字符集编解码操作,可能会略微降低处理效率。字节流适合处理二进制数据和字节流式的操作,如文件复制、网络传输等。

BaoZi969的博客 330

Java IO 流:字节流、字符流、接口、实现类与阻塞方法

在 Java 中,IO(Input/Output)流是处理数据输入输出的重要部分。无论是从文件、网络还是其他数据源读取数据,或是将数据写入到这些位置,Java 都提供了丰富的流类库来支持这些操作。下面我们将详细介绍 Java IO 流中的字节流、字符流、常见的接口、实现类以及阻塞方法。

✨ 欢迎来到【Seal ^_^ 的CSDN博客】!✨ 7201

6.2字节流

在 Java 中,IO(Input/Output)流是用于处理数据输入输出的核心机制,它提供了一种统一的方式来读写不同类型的数据。字节流以字节(8 位)为单位处理数据,适用于所有类型的数据(如图片、视频、二进制文件等)。System.outint read()b.lengthblenboffbbofflen标准输出(System.out是类型)print()intStringObjectprintln()printf()format()printf()booleanflush()close()

chxii的专栏 2258

Java 中字节流的使用详解

本文介绍了Java字节流的基本概念和使用方法,重点讲解了FileInputStream、FileOutputStream、BufferedInputStream/BufferedOutputStream以及ByteArrayInputStream/ByteArrayOutputStream等核心类的使用场景和示例代码。通过对比字节流与字符流的区别,指出字节流适合处理二进制数据而字符流更适合文本文件。文章还提供了性能优化建议,如使用缓冲区提升大文件操作效率。最后总结了掌握字节流对理解Java文件处理机制的重要

2503_92145588的博客 1028

字节流和字符流区别与适用场景

Java 中的字节流处理的最基本单位为 1 个字节,通常用来处理二进制数据。字节流类InputStream 和 OutputStream 类均为抽象类,代表了基本的输入字节流和输出字节流。 Java 中的字符流处理的最基本的单元是 Unicode 代码单元(大小2字节),通常用来处理文本数据。 区别: 字节流操作的基本单元是字节;字符流操作的基本单元是字符 字节流默认不使用缓冲区;字...

ConstXiong 3914

字节流在基础部分使用中的常遇的小问题,以及字节流和字符流的对比.

很多刚接触的朋友在使用字节流和字符流时候会遇到一些小问题,网上搜索很难找到,现暂发布一些拙见,有问题欢迎多多提出. 先贴代码 第二处红标这个地方read读取的究竟是bytes数组中的什么? 答:这个地方是用到了bytes数组的内容,是作为缓冲区存在的. fis,read,返回值是一个数字255,为什么是255? 答,这个返回值为255,是首个字节的内容 ,输入流的read方法的返回值,分为两种情...

HelloWorld哥的博客 358
上一篇: IO流简介
下一篇: 常见字符流
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值