黑马程序员——Java基础--IO流(2)

来源:互联网 发布:苹果电脑能编程吗 编辑:程序博客网 时间:2024/06/03 01:55
------- android培训、java培训、期待与您交流! ----------

一  转换流

由于字节流操作中文不是特别方便,所以,java就提供了转换流。

字符流=字节流+编码表。

编码表:由字符及其对应的数值组成的一张表。

OutputStreamWriter 字符输出流:

构造方法:

public OutputStreamWriter(OutputStream out)

public OutputStreamWriter(OutputStream out,String charsetName)

常用方法:

public void write(int c)

public void write(char[] cbuf)

public void write(char[] cbuf,int off,int len)

public void write(String str)

public void write(String str,int off,int len)

字符流操作要注意的问题

flush()的作用

flush()和close()的区别

InputStreamReader 字符输入流:

构造方法:

public InputStreamReader(InputStream in)

public InputStreamReader(InputStream in,String charsetName)

常用方法:

public int read()

public int read(char[] cbuf)

import java.io.*;public class InputStreamReaderDemo {public static void main(String[] args) throws IOException{/* * 字符流,转换流 InputStreamReader读取文本文件 */public static void method()throws IOException{//创建字节输入流,封装文件FileInputStream fis = new FileInputStream("c:\\a.txt");//创建转换流对象,传递字节输入流InputStreamReader isr = new InputStreamReader(fis);//读取单个字符/*int len = 0 ;while((len = isr.read())!=-1){System.out.print((char)len);}*///读取中,传递字符数组char[] ch = new char[1024];int len = 0 ;while((len = isr.read(ch))!=-1){//把数组变成字符串输出System.out.println(new String(ch,0,len));}isr.close();}}

简化写法:

FileWriter写数据

import java.io.*;public class FileWriterDemo {public static void main(String[] args) throws IOException{//创建转换流便捷类,传递字符串文件名FileWriter fw = new FileWriter("c:\\1.txt");//写单个字符fw.write(100);fw.flush();//写字符数组fw.write("abc".toCharArray());fw.flush();//写字符串fw.write("字符串");fw.flush();fw.close();}}

FileReader读取数据

import java.io.*;public class FileReaderDemo {public static void main(String[] args) throws IOException{//创建便捷类对象,封装文件FileReader fr = new FileReader("c:\\1.txt");int len = 0 ;/*while((len = fr.read())!=-1){System.out.print((char)len);}*/char[] ch = new char[1024];while((len = fr.read(ch))!=-1){System.out.println(new String(ch,0,len));}fr.close();}}

字符缓冲流:

BufferedWriter BufferedReader

特殊功能:

BufferedWriter:voidnewLine()

BufferedReader:String readLine()

二  基本数据类型的流

DataInputStream

   构造方法,传递字节输入流

   从字节输入流读取基本类型

   读取基本类型的时候,结束利用异常 EOFException

DataOutputStream

   构造方法,传递字节输出流

从字节输出流写基本类型

   按照基本类占有字节长度,进行写入

三  打印流PrintWriter

特点:

操作数据目的,不操作数据源

不会抛出IOException;

装饰类,方便打印。

自动刷新的前提:

构造方法必须是流对象;

println printf format。 

import java.io.*;public class PrintStreamDemo {public static void main(String[] args) throws Exception{//将数据输出到File对象中//创建打印流对象,构造方法中传递File对象PrintWriter pwFile = new PrintWriter(new File("c:\\a.txt"));//打印流printpwFile.print(false);pwFile.flush();pwFile.print(97);pwFile.flush();pwFile.close();//将数据打印到字节输出流PrintWriter pwStream = new PrintWriter(new FileOutputStream("c:\\b.txt"));pwStream.print(48);pwStream.close();//将数据打印到字符输出流PrintWriter pwWriter = new PrintWriter(new FileWriter("c:\\c.txt"));pwWriter.print("你好");pwWriter.close();//将数据打印到字符串文件名PrintWriter pwString = new PrintWriter("c:\\d.txt");pwString.print(3.2);pwString.close();//打印流输出到文件带换行PrintWriter pwLine = new PrintWriter(new FileOutputStream("c:\\e.txt"),true);pwLine.println("你好");pwLine.close();}}

四  随机访问流

RandomAccessFile类不属于流,是Object类的子类。但它融合了InputStream和OutputStream的功能。支持对随机访问文件的读取和写入。

五  序列化流

    ObjectInputStream

    构造方法,传递字节输入

    readObject()

    ObjectOutputStream

    构造方法,传递字节输出

    writeObject()

序列化,类实现Serializable接口

transient 阻止变量序列化,静态不能序列化

可以自定义序列号

import java.io.*;public class ObjectStringDemo {public static void main(String[] args) throws IOException,ClassNotFoundException{readObj();//writeObj();}/* * ObjectInputStream将对象从文件中读取出来 */public static void readObj()throws IOException,ClassNotFoundException{//创建字节输入流,封装文件FileInputStream fis = new FileInputStream("c:\\person.txt");//创建读取对象的流对象,传递字节输入流ObjectInputStream ois = new ObjectInputStream(fis);//读取方法readObjectObject obj = ois.readObject();System.out.println(obj.toString());}/* * ObjectOutputStream将对象写到文件中保存 */ public static void writeObj()throws IOException{ //创建字节输出流,封装文件 FileOutputStream fos = new FileOutputStream("c:\\person.txt"); //创建写对象的流对象,传递字节输出流 ObjectOutputStream oos = new ObjectOutputStream(fos); Person p = new Person("zhangsan",25); //调用写对象的方法 oos.writeObject(p); oos.close(); }}



0 0
原创粉丝点击