JavaIO流

JavaIO流

基础概念

 数据流:
    一组有序,有起点和终点的字节的数据序列。包括输入流和输出流
 输入流:
    程序从输入流读取数据源。数据源包括外界(键盘、文件、网络…),即是将数据源读入到程序的通信通道
 输出流:
    程序向输出流写入数据。将程序中的数据输出到外界(显示器、打印机、文件、网络…)的通信通道。
 数据流分类:
    字节流:数据流中最小的数据单元是字节
    字符流:数据流中最小的数据单元是字符, Java中的字符是Unicode编码,一个字符占用两个字节。

标准IO

 标准输入,输出数据流
    ·标准输出流 System.out 
        数据类型为PrintStream
        Void print( 参数)
        Void println( 参数)
    ·标准输入流 System.in
        数据类型为InputStream
        int read() 
        int read(byte[] b)
    ·标准错误流
        数据类型为PrintStream
        Void print( 参数)
        Void println( 参数)
        输出的字是红色的,printStackTrace使用的就是标准错误流
            public void printStackTrace() {
                printStackTrace(System.err);
            }

java.IO层次体系结构

 重要内容:
    ·5个类:
        File、OutputStream、InputStream、Writer、Reader
    ·1个接口:
        Serializable
  I/O层次
    ·流式(IO主体)
        字节:OutputStream、InputStream
        字符:Writer、Reader
    ·非流式(辅助流式部分的类)
        File、RandomAccessFile、FileDescriptor等
    ·其他(文件读取部分的与安全相关的类、本地操作系统相关的文件系统的类)
        SerializablePermission、FileSystem、Win32FileSystem、WinNTFileSystem等

File类

 !!!不负责输入输出!!!
 构造函数:
    File(String pathname)   
    File(String parent, String child)
    File(File parent, String child)
 常用函数:
    1)public boolean exists( ) 判断文件或目录是否存在
    2)public boolean isFile( ) 判断是文件还是目录 
    3)public boolean isDirectory( ) 判断是文件还是目录
    4)public String getName( ) 返回文件名或目录名
    5)public String getPath( ) 返回文件或目录的路径。
    6)public long ( ) 获取文件的长度 
    7)public String[ ] list ( ) 将目录中所有文件名保存在字符串数组中返回。 
对文件或目录进行管理、操作的方法:
    1)public boolean renameTo( File newFile );    重命名文件
    2)public void delete( );   删除文件
    3)public boolean mkdir( ); 创建目录

io流的具体分类

 1. Memory 
    1)从/向内存数组读写数据: CharArrayReader、 CharArrayWriter、ByteArrayInputStream、ByteArrayOutputStream
    2)从/向内存字符串读写数据 StringReader、StringWriter、StringBufferInputStream
 2.Pipe管道  
    实现管道的输入和输出(进程间通信): PipedReader、PipedWriter、PipedInputStream、PipedOutputStream
 3.File 文件流。对文件进行读、写操作 :
    FileReader、FileWriter、FileInputStream、FileOutputStream
 2. ObjectSerialization 对象输入、输出 :\
    ObjectInputStream、ObjectOutputStream
 5.DataConversion数据流 按基本数据类型读、写(处理的数据是Java的基本类型(如布尔型,字节,整数和浮点数)):
    DataInputStream、DataOutputStream
 6.Printing 包含方便的打印方法 :
    PrintWriter、PrintStream
 7.Buffering缓冲  在读入或写出时,对数据进行缓存,以减少I/O的次数:
    BufferedReader、BufferedWriter、BufferedInputStream、BufferedOutputStream
 8.Filtering 滤流,在数据进行读或写时进行过滤:
    FilterReader、FilterWriter、FilterInputStream、FilterOutputStream过
 9.Concatenation合并输入 把多个输入流连接成一个输入流 :
    SequenceInputStream
 10.Counting计数  在读入数据时对行记数 :
    LineNumberReader、LineNumberInputStream
 11.Peeking Ahead 通过缓存机制,进行预读 :
    PushbackReader、PushbackInputStream
 12.Converting between Bytes and Characters 按照一定的编码/解码标准将字节流转换为字符流,或进行反向转换(Stream到Reader,Writer的转换类):
    InputStreamReader、OutputStreamWriter

InputStream抽象类

 常用方法:
    (1public abstract int read( ):
        读取一个byte的数据,返回值是高位补0int类型值。若返回值=-1说明没有读取到任何字节读取工作结束。
    (2public int read(byte b[ ]):
        读取b.length个字节的数据放到b数组中。返回值是读取的字节数。该方法实际上是调用下一个方法实现的 
    (3public int read(byte b[ ], int off, int len):
        从输入流中最多读取len个字节的数据,存放到偏移量为off的b数组中。 
    (4public int available( ):
        返回输入流中可以读取的字节数。注意:若输入阻塞,当前线程将被挂起,如果InputStream对象调用这个方法的话,它只会返回0,这个方法必须由继承InputStream类的子类对象调用才有用, 
    (5public long skip(long n):
        忽略输入流中的n个字节,返回值是实际忽略的字节数, 跳过一些字节来读取 
    (6public int close( ) :
        我们在使用完后,必须对我们打开的流进行关闭. 
 主要子类:
    1) FileInputStream:把一个文件作为InputStream,实现对文件的读取操作   
    2) ByteArrayInputStream:把内存中的一个缓冲区作为InputStream使用 
    3) StringBufferInputStream:把一个String对象作为InputStream 
    4) PipedInputStream:实现了pipe的概念,主要在线程中使用 
    5) SequenceInputStream:把多个InputStream合并为一个InputStream 

OutputStream抽象类

 常用方法:
    1. public void write(byte b[ ]):将参数b中的字节写到输出流。 
    2. public void write(byte b[ ], int off, int len) :将参数b的从偏移量off开始的len个字节写到输出流。 
    3. public abstract void write(int b) :先将int转换为byte类型,把低字节写入到输出流中。 
    4. public void flush( ) : 将数据缓冲区中数据全部输出,并清空缓冲区。 
    5. public void close( ) : 关闭输出流并释放与流相关的系统资源。 
 主要子类:
    1) ByteArrayOutputStream:把信息存入内存中的一个缓冲区中 
    2) FileOutputStream:把信息存入文件中 
    3) PipedOutputStream:实现了pipe的概念,主要在线程中使用 
    4) SequenceOutputStream:把多个OutStream合并为一个OutStream     

FileInputStream类

 以文件作为数据输入源的数据流
import java.io.IOException;
import java.io.FileInputStream;

public class TestFile {
	public static void main(String args[]) throws IOException {
		try{	
		       FileInputStream rf=new   FileInputStream("InputFromFile.java");
		       int n=512;   byte  buffer[]=new  byte[n];   
		       while((rf.read(buffer,0,n)!=-1)&&(n>0)){
		           System.out.println(new String(buffer) );
		        }
		        System.out.println();
		        rf.close();
		} catch(IOException  IOe){	    
		      System.out.println(IOe.toString());
		}
 
	}
 
}

FileOutputStream类

 用来处理以文件作为数据输出目的数据流
import java.io.IOException;
import java.io.FileOutputStream;
public class TestFile {
	public static void main(String args[]) throws IOException {
		try {
			System.out.println("please Input from      Keyboard");
			int count, n = 512;
			byte buffer[] = new byte[n];
			count = System.in.read(buffer);
			FileOutputStream wf = new FileOutputStream("d:/myjava/write.txt");
			wf.write(buffer, 0, count);
			wf.close(); // 当流写操作结束时,调用close方法关闭流。
			System.out.println("Save to the write.txt");
		} catch (IOException IOe) {
			System.out.println("File Write Error!");
		}
	}
 
}

IOException异常类的子类

 1.public class  EOFException :非正常到达文件尾或输入流尾时,抛出这种类型的异常。
 2.public class FileNotFoundException:当文件找不到时,抛出的异常。
 3.public class InterruptedIOException:当I/O操作被中断时,抛出这种类型的异常。
posted @   淡漠灬白驹  阅读(15)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示