`
windywindy
  • 浏览: 169379 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

数据流,对象流,字节流,字符流

阅读更多
1.什么是数据流 ?
数据流是指所有的数据通信通道
有两类流,InputStream and OutputStream,Java中每一种流的基本功能依赖于它们
InputStream 用于read,OutputStream 用于write, 读和写都是相对与内存说的,读就是从其他地方把数据拿进内存,写就是把数据从内存推出去这两个都是抽象类,不能直接使用
2.File 类
File 可以表示文件也可以表示目录,File 类控制所有硬盘操作
构造器:
File(File parent,String child) 用父类和文件名构造
File(String pathname) 用绝对路径构造
File(String parent,String child) 用父目录和文件名构造
File(URI uri) 用远程文件构造
常用方法:
boolean createNewFile();
boolean exists();
3.文件流的建立
File f=new File("temp.txt");
FileInputStream in=new FileInputStream(f);
FileOutputStream out=new FileOutputStream(f);

例子:文件拷贝
import java.io.*;
public class Copy{
public static void main(String args[]){
FileInputStream fis=null;
FileOutputStream fos=null;
try{
fis=new FileInputStream("c2.gif");
fos=new FileOutputStream("c2_copy.gif");
int c;
while((c=fis.read()) != -1)
fos.write(c);
}catch(Exception e){
e.printStackTrace();
}finally{
if(fis != null) try{ fis.close(); }catch(Exception e){ e.printStackTrace(); }
if(fos!= null) try{ fos.close(); }catch(Exception e){ e.printStackTrace(); }
}}}

4.缓冲区流
BufferedInputStream
BufferedOutputStream
他们是在普通文件流上加了缓冲的功能,所以构造他们时要先构造普通流
例子:文件拷贝的缓冲改进
import java.io.*;
public class Copy{
public static void main(String args[]){
BufferedInputStream bis=null;
BufferedOutputStream bos=null;
byte buf[]=new byte[100];
try{
bis=new BufferedInputStream(new FileInputStream("persia.mp3"));
bos=new BufferedOutputStream(new FileOutputStream("persia_copy.mp3"));
int len=0;
while( true ){
len=bis.read(buf);
if(len<=0) break;
bos.write(buf,0,len);
}
bos.flush();//缓冲区只有满时才会将数据输出到输出流,用flush()将未满的缓冲区中数据强制输出
}catch(Exception e){
e.printStackTrace();
}finally{
if(bis != null) try{ bis.close(); }catch(Exception e){ e.printStackTrace(); }
if(bos!= null) try{ bos.close(); }catch(Exception e){ e.printStackTrace(); }
}}}

5.原始型数据流
DataInputStream
DataOutputStream
他们是在普通流上加了读写原始型数据的功能,所以构造他们时要先构造普通流方法:
readBoolean()/writeBoolean()
readByte()/writeByte()
readChar()/writeByte()
......
例子://这个流比较简单,要注意的就是读时的顺序要和写时的一样
import java.io.*;
public class DataOut{
public static void main(String args[]){
DataOutputStream dos=null;
try{
dos=new DataOutputStream(new FileOutputStream("dataout.txt"));
dos.writeInt(1);
dos.writeBoolean(true);
dos.writeLong(100L);
dos.writeChar('a');
}catch(Exception e){
e.printStackTrace();
}finally{
if(dos!=null)
try{
dos.close();
}catch(Exception e){
}}}}
 
import java.io.*;
public class DataIn{
public static void main(String args[]){
DataInputStream dis=null;
try{
dis=new DataInputStream(new FileInputStream("dataout.txt"));
System.out.println(dis.readInt());
System.out.println(dis.readBoolean());
System.out.println(dis.readLong());
System.out.println(dis.readChar());
}catch(Exception e){
e.printStackTrace();
}finally{
if(dis!=null)
try{
dis.close();
}catch(Exception e){
}}}}


6.对象流
串行化:对象通过写出描述自己状态的数值来记述自己的过程叫串行化
对象流:能够输入输出对象的流
将串行化的对象通过对象流写入文件或传送到其他地方,对象流是在普通流上加了传输对象的功能,所以构造对象流时要先构造普通文件流
注意:只有实现了Serializable接口的类才能被串行化

例子:
import java.io.*;
class Student implements Serializable{
private String name;
private int age;
public Student(String name,int age){
this.name=name;
this.age=age;
}
public void greeting(){
System.out.println("hello ,my name is "+name);
}
public String toString(){
return "Student["+name+","+age+"]";
}}
public class ObjectOutTest{
public static void main(String args[]){
ObjectOutputStream oos=null;
try{
oos=new ObjectOutputStream(new FileOutputStream("student.txt"));
Student s1=new Student("Jerry",24);
Student s2=new Student("Andy",33);
oos.writeObject(s1);
oos.writeObject(s2);
}catch(Exception e){
e.printStackTrace();
}finally{
if(oos!=null)
try{
oos.close();
}catch(Exception e){
e.printStackTrace();
}}}}
 
import java.io.*;
public class ObjectInTest{
public static void main(String args[]){
ObjectInputStream ois=null;
Student s=null;
try{
ois=new ObjectInputStream(new FileInputStream("student.txt"));
System.out.println("--------------------");
s=(Student)ois.readObject();
System.out.println(s);
s.greeting();
System.out.println("--------------------");
s=(Student)ois.readObject();
System.out.println(s);
s.greeting();
}catch(Exception e){
e.printStackTrace();
}finally{
if(ois!=null)
try{
ois.close();
}catch(Exception e){
e.printStackTrace();
}}}}

附:如何遍历对象
ObjectInputStream无论是读对象,还是记取int等java的基本数据类型,在判结束时,绝对既不是-1,也不是什么null。
若文件中有若于个int的数,你用DataInputStream中的readint()去读,何时判读到结尾? 绝对既不是-1,也不是什么null
同样道理:若文件中有若于个Object对象,你用ObjectInputStream中的readObject()去读,何时判读到结尾?
绝对既不是-1,也不是什么null
方法之一:(常用的方法)将若干个对象(数量不定)都装入一个容器中(如:ArrayList之类),然后将容器这一个对象写入就行了。读取时,只要读取一个对象(即容器对象)就行了。
方法之二:(若不想用容器),则由于数量不定,正是用EOFException来判断结束。代码结构如下:(无论是readInt()读int,还是readObject()读对象)
try{
while(true){
Object o=ois.radObject();
处理已读出的对象o; }
}catch(EOFxception e){ 已从流中读完。 }
finallly{ 流的关闭。 }


7.字符流 InputStreamReader/OutputStreamWriter
上面的几种流的单位是 byte,所以叫做字节流,写入文件的都是二进制字节,我们无法直接看,下面要学习的是字节流。Java采用 Unicode 字符集,每个字符和汉字都采用2个字节进行编码,ASCII 码是 Unicode 编码的子集。
InputStreamReader 是 字节流 到 字符桥的桥梁 ( byte->char 读取字节然后用特定字符集编码成字符)
OutputStreamWriter是 字符流 到 字节流的桥梁 ( char->byte )
他们是在字节流的基础上加了桥梁作用,所以构造他们时要先构造普通文件流
我们常用的是:
BufferedReader 方法:readLine()
PrintWriter 方法:println()
例子:
import java.io.*;
public class PrintWriterTest{
public static void main(String args[]){
PrintWriter pw=null;
try{
pw=new PrintWriter(new OutputStreamWriter(new FileOutputStream("bufferedwriter.txt")));
pw.println("hello world");
}catch(Exception e){
e.printStackTrace();
}finally{
if(pw!=null)
try{
pw.close();
}catch(Exception e){
e.printStackTrace();
}}}}
 
import java.io.*;
public class BufferedReaderTest{
public static void main(String args[]){
BufferedReader br=null;
try{
br=new BufferedReader(new InputStreamReader(new FileInputStream("bufferedwriter.txt")));
System.out.println(br.readLine());
}catch(Exception e){
e.printStackTrace();
}finally{
if(br!=null)
try{
br.close();
}catch(Exception e){
e.printStackTrace();
}}}}

8.小结
a. 字节流:
InputStream
|-- FileInputStream (基本文件流)
|-- BufferedInputStream
|-- DataInputStream
|-- ObjectInputStream
OutputStream 同上图
BufferedInputStream,DataInputStream,ObjectInputStream 只是在FileInputStream 上增添了相应的功能,构造时先构造FileInputStream

b. 字符流:
Reader
|-- InputStreamReader (byte->char 桥梁)
|-- BufferedReader (常用)
Writer
|-- OutputStreamWriter (char->byte 桥梁)
|-- BufferedWriter
|-- PrintWriter (常用)
分享到:
评论

相关推荐

    数据流,对象流,字节流,字符流.docx

    根据不同的数据类型和操作方式,I/O流分为多个类别,主要包括数据流、对象流、字节流和字符流。 1. **数据流**: 数据流主要指的是InputStream和OutputStream这两类基本流。InputStream是所有读取数据的流的基类,...

    JAVA 字符流与字节流

    在Java编程语言中,输入/输出(I/O)操作是处理数据流的关键部分,而字符流与字节流则是实现这些操作的两种基本方式。理解它们的区别和应用场景对于任何Java开发者来说都是至关重要的。 ### 字节流 字节流是最基本...

    Java字符流和字节流

    根据处理数据类型的不同,Java I/O流主要分为字节流和字符流两大类。字节流处理的是8位的字节数据,而字符流处理的是16位的Unicode字符。这两种流都有各自的特点和应用场景。 #### 二、字节流 字节流是最基本的数据...

    字节流字符流

    在Java编程语言中,字节流(Byte Stream)和字符流(Character Stream)是处理输入输出数据的两种基本方式。它们构成了Java I/O系统的核心,用于读写文件、网络通信等场景。字节流和字符流的区别在于处理数据的基本...

    java字节流和字符流

    Java中的字节流和字符流是IO操作中的两种基本类型,它们主要用于数据的输入和输出。字节流处理的数据单位是字节,而字符流处理的是Unicode字符。 字节流: 字节流主要由两个核心类构成:`InputStream`和`...

    IO流学习(字节流 字符流 文本流)!

    总的来说,IO流是Java编程中不可或缺的一部分,理解和熟练运用字节流、字符流和文本流能帮助我们高效地处理数据输入和输出,为各种复杂的应用场景提供支持。在实际开发中,要根据数据类型和需求选择合适的流,并利用...

    Java IO字符流和字节流

    字节流是处理字节的数据流,它是最基础的数据流形式。字节流处理的数据单位是字节(8位),适用于处理所有类型的数据,包括文本、图像、音频等。Java中的`InputStream`和`OutputStream`是所有字节流的基类。 - **`...

    Java IO 字节流 字符流

    字节流主要用于处理任何类型的原始数据,如图片、音频文件或者二进制文件,而字符流则专注于处理基于文本的数据。本文将深入探讨这两类流的原理、用途以及它们之间的关联。 首先,我们来看字节流。字节流由...

    字节流字符流练习

    除此之外,InputStreamReader和OutputStreamWriter是字节流与字符流之间的桥梁,它们允许我们在字节流和字符流之间转换,以适应不同编码格式的需求。 在Java IO中,还有一套转换流(Wrapper Stream),即...

    io流详解,字符流和字节流代码

    本文件包“io流详解,字符流和字节流代码”显然是针对Java IO流的深入学习资源,包含了代码示例和可能的可视化解释。 IO流分为两大类:字节流(Byte Stream)和字符流(Character Stream)。字节流处理的是8位的数据...

    字节流字符流的使用方法源码+文档

    1. **字节流**:字节流是基于八位字节的数据流,分为输入流和输出流。Java中的`InputStream`和`OutputStream`是所有字节输入流和输出流的基类。例如,`FileInputStream`用于读取文件,而`FileOutputStream`用于写入...

    JavaIO实例_字节流_字符流_缓冲流_转换流IODemo

    在Java中,IO流分为两大类:字节流和字符流,每种流又有输入流和输出流之分,分别用于数据的读取和写入。 1. **字节流**: - 字节流处理的是8位的字节数据,是最基本的流类型。Java中的`InputStream`和`...

    字节流,字符流,对象流,序列化,持久化

    在Java编程语言中,字节流、字符流、对象流和序列化是处理数据传输和存储的核心概念。这些概念在程序设计中起着至关重要的作用,尤其是在进行输入输出操作时。下面将对这些主题进行详细解释。 1. 字节流(Byte ...

    Java基础11-(字节流、字符流)

    Java的IO流是按照数据的流向分为输入流和输出流,按照数据类型分为字节流和字符流。字节流处理的是二进制数据,适用于任何类型的文件,包括文本文件和非文本文件(如图片、音频、视频等)。字符流则用于处理字符数据...

    java字节流和字符流[整理].pdf

    总结起来,Java中的字节流和字符流主要用于数据的输入和输出。字节流适合处理任何类型的二进制数据,如图片、音频等;而字符流主要处理字符编码的数据,如文本文件。在实际编程中,根据数据类型选择合适的流类型,并...

    java 字符流 讲解

    数据流可以分为字节流(Byte Stream)和字符流(Character Stream)。本讲解主要聚焦于字符流,它是Java IO体系中处理文本数据的重要部分。 字符流包括两大基本类:`java.io.Reader`和`java.io.Writer`。`Reader`类...

    Java字节流与字符流的介绍.pdf

    当用于处理文本数据时,选择字符流比字节流更好。但对只出路基本数据类型的开发者,可以继续使用字节流。所有的读操作都继承自一个公共超类 java.io.Reader 类。所有的写操作都继承自一个公共超类 java.io.Writer 类...

    JAVA_字节流和字符流

    在Java编程语言中,字节流(Byte Stream)和字符流(Character Stream)是处理输入输出数据的两种基本方式。字节流主要用于处理任何类型的二进制数据,如图片、音频、视频等,而字符流则专为处理基于Unicode编码的...

    Java 字节流、字符流题目.pdf

    在Java中,流分为字节流和字符流两大类,它们都是抽象的概念,用于表示数据流动的方向。字节流处理的数据单位是字节,而字符流处理的是Unicode编码的字符。 一、字节流 字节流分为输入流和输出流,Java中最基础的...

Global site tag (gtag.js) - Google Analytics