Java IO流

来源:互联网 发布:许嵩所有歌曲知乎 编辑:程序博客网 时间:2024/06/08 04:10

当java程序要读取数据的时候,是通过流来完成的,数据源可以是文件、内存或者网络连接。而java程序写入数据也是通过流来完成的。

流按数据方向来划分,分为输入流和输出流;而按数据类型划分分为字节流和字符流。

输入流:InputStream/Reader

输出流:OutputStream/Writer

字节流:InputStream/OutputStream

字符流:Reader/Writer


字节流和字符流有什么区别?

1、字节流读取数据的时候,读到一个Byte就返回一个Byte;字符流使用了字节流读到一个或多个字节时(中文是2个,UTF-8是3个),先去查指定的编码表,将查到的字符返回。

2、字节流可以处理所有数据类型的数据,如image、MP3;字符流只能处理字符数据。

3、字节流输入流都以InputStream为结尾,字节流的输出流都以OutputStream为结尾;字符流输入流都以Reader为结尾,输出流都是以Writer为结尾。字节流不会用到缓冲区,字符流在处理文件时会用到缓冲区。

总体来说字节流要优于字符流,因为数据本身是通过字节来存储和传输的,字符只是在内存中才会形成,如果要是纯文本的数据可以考虑使用字符流,其他一律使用字节流。

常用的字节流:

FileInputStream  FileOutputStream BufferedInputSream BuffereOutputStream

常用的字符流:

BufferedReader BufferedWriter InputStreamReader FileReader OutputStreamWriter FileWriter


InputStream为所有字节输入流的父类,有三个基本的read()方法:

int read()从流里读一个字节

int read(byte[] b)将数据读入到字节数组中,并返回所读的字节数

int read(byte[] b, int off, int len)将数据读到字节数组中,从off开始读,最多读len个byte,并返回读到的字节长度

其他方法:

void close()关闭此输入流并释放与该流关联的所有资源

int available()返回不受阻塞地从此输入流读取的字节数

long skip(long n)跳过和放弃次输入流中的n个数据字节

OutputStream为所有字节输出流的父类,有三个基本的write()方法:

void write(int n)将制定的字节输入此输出流

void write(byte[] b)将b.length个字节从制定的字节数组输出到输出流

void write(byte[] b, int off, int len)将制定字节从off处,len个长度输出到输出流

其他方法:

void close()关闭此输出流并释放与该流关联的所有资源

void flush()刷新此输出流并强制写出所有缓冲的输出字节

文件输入输出流:FileInputStream和FileOutputStream

FileInputStream fin = new FileInputStream("infile.txt");

FileOutputStream fout =  new FileOutputStream("outfile.txt");//建立新的file outfile.txt,如果已存在将被覆盖,如果要写已存在文件的追加则FileOutputStream fout =  new FileOutputStream("outfile.txt",true);//true为追加,false为覆盖,默认false

例子:

import java.io.*;

class hello{

public static void main(String[] args)throws IOException{

String fileName = "D:" + File.separator + "hello.txt";

File f = new File(fileName);

OutputStream out = new FileOutputStream(f);

String str = "您好";

byte[] b = str.getBytes();

out.write(b);

out.close();

}

}

字符流:Reader和Writer为所有字符流的父类,InputStreamReader和OutputStreamWriter是桥接字符流和字节流转换器。

例子:

import java.io.*;

class hello{

public static void main(String[] args) throws IOException{

String fileName =  "D:" + File.separator + "hello.txt";

File f = new File(fileName);

Writer out  = new FileWriter(f);//追加时new FileWriter(f, true);

String str =  "hello";

out.write(str);//不需将之转化为字节数组了

out.close();

}

}


更常用的一个类是Scanner类,举个例子:

import java.util.Scanner;

public class ScannerDemo{

public static void main(String[] args){

Scanner sca = new Scanner(System.in);

int temp = sca.nextInt();

System.out.println(temp);

float f = sca.nextFloat();

System.out.println(f);

String str = sca.next();

}

}


DataInputStream与DataOutputStream

使用DataInputStream/DataOutputStrream时,必须与其父类FileInputStream/FileOutputStream搭配使用。

例:

FileInputStream fis = new FileInputStream(fileName_source);

DataInputStream dis = new DataInputStream(fis);

dis.read();//读入数据流

例:

FileOutputStream fos = new FileOutputStream(fileName_target);

DataOutputStream dos = new DataOutputStream(fos);

dos.write(string.getBytes());//写入到文件中


0 0
原创粉丝点击