我爱你java语法_Java语法基础学习DayFifteen(IO续)

一、缓冲流(处理流的一种)

1.作用:可以提高文件操作的效率

2.使用BufferedInputStream和BufferedOutputStream实现非文本文件的复制

特点:flush()方法

代码示例:

BufferedOutputStream bos = null;

BufferedInputStream bis= null;try{//1.提供读入、写入的文件

File file1 = new File("awsl.jpg");

File file2= new File("awysl.jpg");//2.创建相应节点流FileInputStream、FileOutputStream

FileInputStream fis = newFileInputStream(file1);

FileOutputStream fos= newFileOutputStream(file2);//3.将节点流的对象作为形参传到缓冲流的构造器中

bis = newBufferedInputStream(fis);

bos= newBufferedOutputStream(fos);//4.具体的文件复制操作

byte[] b = new byte[1024];intlen;while((len = bis.read(b)) != -1){

bos.write(b,0,len);

bos.flush();//把最后一点数据清空出去。

}

}catch(IOException e){

e.printStackTrace();

}finally{//关闭流(只需要关闭缓冲流)

if(bis != null){try{

bis.close();

}catch(IOException e){

e.printStackTrace();

}

}if(bos != null){try{

bos.close();

}catch(IOException e){

e.printStackTrace();

}

}

}

2.使用BufferedReader和BufferedWriter实现纯文本文件的复制(纯文本文件才使用字符流,doc文件不是纯文本,只能使用字节流)

特点:使用BufferedReader的readLine()方法读取文件的一行

BufferedWriter的flush()方法

代码示例:

BufferedWriter bw = null;

BufferedReader br= null;try{

File file1= new File("hello.txt");

File file2= new File("hello2.txt");

FileReader fr= newFileReader(file1);

FileWriter fw= newFileWriter(file2);

br= newBufferedReader(fr);

bw= newBufferedWriter(fw);

String str;//使用bufferedReader的readLine()方法读取文件的一行,返回str类型的值。

while((str = br.readLine()) != null){//注意:读取结束返回null

bw.write(str);//也可以这么换行:bw.write(str+"\n");

bw.newLine();//换行

bw.flush();

}

}catch(IOException e){

e.printStackTrace();

}finally{if(br != null){try{

br.close();

}catch(IOException e){

e.printStackTrace();

}

}if(bw != null){try{

bw.close();

}catch(IOException e){

e.printStackTrace();

}

}

}

二、转换流(处理流的一种)

1.作用:提供了在字节流和字符流之间的转换;字节流中的数据都是字符时,转换成字符流操作更加高效。

2。代码示例:

解码:字节流 - - ->字符流

编码:字符流 - - ->字节流

BufferedWriter bw = null;

BufferedReader br= null;try{//解码

File file1 = new File("hello.txt");

FileInputStream fis= newFileInputStream(file1);

InputStreamReader isr= new InputStreamReader(fis,"GBK");//按GBK的编码标准转换

br = newBufferedReader(isr);//编码

File file2 = new File("hello3.txt");

FileOutputStream fos= newFileOutputStream(file2);

OutputStreamWriter osw= new OutputStreamWriter(fos,"GBK");

bw= newBufferedWriter(osw);

String str;while((str = br.readLine()) != null){

bw.write(str);

bw.newLine();

bw.flush();

}

}catch(IOException e){

e.printStackTrace();

}finally{if(br != null){try{

br.close();

}catch(IOException e){

e.printStackTrace();

}

}if(bw != null){try{

bw.close();

}catch(IOException e){

e.printStackTrace();

}

}

}

三、标准输入输出流

标准的输入流:System.in

标准的输出流:System.out

题目:

从键盘输入字符串,要求将读取的整行字符串转成大写输出,然后继续输入操作,直到输入“e”或“exit”时退出程序。

代码:

BufferedReader br = null;try{

InputStreamis = System.in;//System.in返回一个InputStream类型的对象

InputStreamReader isr = new InputStreamReader(is);

br= newBufferedReader(isr);

String str;while(true){

System.out.println("请输入字符串:");

str=br.readLine();if(str.equalsIgnoreCase("e") || str.equalsIgnoreCase("exit")){break;

}

String str1=str.toUpperCase();

System.out.println(str1);

}

}catch(IOException e){

e.printStackTrace();

}finally{if(br != null){try{

br.close();

}catch(IOException e){

e.printStackTrace();

}

}

}

四、打印流(处理流的一种)

1.分类

字节流:PrintStream

字符流:PrintWriter

2.代码示例

FileOutputStream fos = null;try{

fos= new FileOutputStream(new File("print.txt"));

}catch(IOException e){

e.printStackTrace();

}//创建打印输出流,设置为自动刷新模式(写入换行符或字节"\n"时都会刷新输出缓冲区

PrintStream ps = new PrintStream(fos,true);if(ps != null){

System.setOut(ps);//把标准输出流(控制台输出)改为输出到文件

}for(int i = 0;i <= 255;i++){//输出ASCII字符

System.out.print((char)i);if(i % 50 == 0){

System.out.println();//每50个数据换行

}

}

ps.close();

五、数据流(处理流的一种)

1.作用

为了方便的操作Java语言的基本数据类型、String和字节数组类型的数据,可以使用数据流。

2.特点

DataInputStream和DataOutputStream分别套接在InputStream和OutputStream节点流上。

2.DataOutputStream的使用

注意:输出的物理文件中的内容会变成一堆乱码,需要通过DataInputStream来读取。

DataOutputStream dos = null;try{

dos= new DataOutputStream(new FileOutputStream(new File("data.txt")));

dos.writeUTF("我爱你,而你却不知道!");//writeUTF()用来写入String

dos.writeBoolean(true);

dos.writeLong(310973560);

}catch(IOException e){

e.printStackTrace();

}finally{if(dos != null){try{

dos.close();

}catch(IOException e){

e.printStackTrace();

}

}

}

3.DataInputStream的使用

DataInputStream dis = null;try{

dis= new DataInputStream(new FileInputStream(new File("data.txt")));

String str=dis.readUTF();

System.out.println(str);//输出"我爱你,而你却不知道!"

Boolean b =dis.readBoolean();

System.out.println(b);//输出true

Long l =dis.readLong();

System.out.println(l);//输出310973560

}catch(IOException e){

e.printStackTrace();

}finally{if(dis != null){try{

dis.close();

}catch(IOException e){

e.printStackTrace();

}

}

}

六、对象流(处理流之一)

1.作用

用于存储和读取对象的处理流。

2.特点

序列化(Serialize):用ObejctOutPutStream类将一个Java对象写入IO流中。

反序列化(Deserialize):用ObjectInputStream类从Io流中恢复该Java对象。

对象的序列化机制:

对象的序列化机制允许把内存中的Java对象转换成与平台无关的二进制流,从而允许把这种二进制流持久的保存在磁盘上,或通过网络将这种二进制流传输到另一个网络节点。

当其他程序获取了二进制流,就可以重新恢复成Java对象。

对象支持序列化机制的前提:

(1)类需要实现如下两个接口之一:SerializableExternalizable

(2)类的属性也要实现Serializable。

(3)提供一个版本号:private static final long serialVersionUID

(4)使用static和transient关键字修饰的变量不可实现序列化(运行不会报错,但读取出来属性值为null)

3.序列化过程

classPerson implements Serializable{private static final long SerialVersionUID = 1234561234L;//版本号

String name;

Integer age;static String ability;//static修饰的变量不可序列化

Person(String name, Integer age,String ability){this.name =name;this.age =age;this.ability =ability;

}

@OverridepublicString toString(){return "Person[name="+name+",age="+age+",ability="+ability+"]";

}

}

classTest{public static voidmain(String[] args){

Person p1= new Person("小明",23,"swimming");

Person p2= new Person("超人",21,"flying");

ObjectOutputStream oos= null;try{

oos= new ObjectOutputStream(new FileOutputStream(new File("object.txt")));

oos.writeObject(p1);

oos.flush();

oos.writeObject(p2);

oos.flush();

}catch(IOException e){

e.printStackTrace();

}finally{if(oos != null){try{

oos.close();

}catch(IOException e){

e.printStackTrace();

}

}

}

}

}

4.反序列化过程

classTest{public static voidmain(String[] args){

ObjectInputStream ois= null;try{

ois= new ObjectInputStream(new FileInputStream(new File("object.txt")));

Person p1=(Person)ois.readObject();

System.out.println(p1);//输出Person[name=小明,age=23,ability=null]

Person p2 =(Person)ois.readObject();

System.out.println(p2);//输出Person[name=超人,age=21,ability=null]

}catch(Exception e){

e.printStackTrace();

}finally{if(ois != null){try{

ois.close();

}catch(IOException e){

e.printStackTrace();

}

}

}

}

}

七、RandomAccessFile(处理流)

1. 作用

支持“随机访问”的方式,程序可以直接跳到文件的任意位置来读、写文件;

支持只访问文件的部分内容;

可以向已存在的文件内容后追加内容。

2.特点

创建RandomAccessFile类实例需要指定一个mode参数,指定文件访问模式:

r:以只读方式打开

rw:可读取、写入

rwd:可读取、写入;同步文件内容的更新

rws:可读取、写入;同步文件内容和元数据的更新

RandomAccessFile对象包含一个记录指针,标识当前读写处的位置,这个指针可以自由移动。

long getFilePointer():获取文件记录指针的当前位置。

void seek(long pos):将文件记录指针定位到pos位置。

3.文件的读写操作

RandomAccessFile raf1 = null;

RandomAccessFile raf2= null;try{

raf1= new RandomAccessFile(new File("hello.txt"),"r");//只读方式打开

raf2 = new RandomAccessFile(new File("hello6.txt"),"rw");//读写方式打开

byte[] b = new byte[20];intlen;while((len = raf1.read(b)) != -1){

raf2.write(b,0,len);

}

}catch(IOException e){

e.printStackTrace();

}finally{//关闭流

if(raf1 != null){try{

raf1.close();

}catch(IOException e){

e.printStackTrace();

}

}if(raf2 != null){try{

raf2.close();

}catch(IOException e){

e.printStackTrace();

}

}

}

4.从任意位置写入数据(会覆盖原有的数据,而不是插入数据)

RandomAccessFile raf = null;try{

raf= new RandomAccessFile(new File("hello6.txt"),"rw");

raf.seek(3);//定位到3的位置,默认在0

raf.write("haha".getBytes());//原来的数据:The destination is stars and seas!//写入后的数据:Thehahatination is stars and seas!

}catch(IOException e){

e.printStackTrace();

}finally{//关闭流

if(raf != null){try{

raf.close();

}catch(IOException e){

e.printStackTrace();

}

}

}

5.从任意位置插入数据,不会覆盖插入位置后面的数据

RandomAccessFile raf = null;try{

raf= new RandomAccessFile(new File("hello6.txt"),"rw");

raf.seek(4);byte[] b = new byte[10];intlen;

StringBuffer sb= newStringBuffer();while((len = raf.read(b)) != -1){

sb.append(new String(b,0,len));//把要插入的位置后面的数据都读取到sb中。

}

raf.seek(4);

raf.write("haha".getBytes());//原来的数据:The destination is stars and seas!

raf.write(sb.toString().getBytes());//结果:The haha destination is stars and seas!

}catch(IOException e){

e.printStackTrace();

}finally{//关闭流

if(raf != null){try{

raf.close();

}catch(IOException e){

e.printStackTrace();

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值