处理流类型:

1、缓冲流

例1:

import java.io.*;
public class TestBufferStream{
public static void main(String args[]){
try{
FileInputStream fis = new FileInputStream("TestCopyByMyself.java");
BufferedInputStream bis = new BufferedInputStream(fis);
int c = 0 ;
System.out.println((char)bis.read());
System.out.println((char)bis.read());
bis.mark(100);//做标记,没看懂API
for(int i=0; i<=10&&(c=bis.read()) != -1; i++){
System.out.print((char)c+" ");
}
System.out.println();
bis.reset();//回到标记的位置
for(int i=0; i<=10&&(c=bis.read()) != -1; i++){
System.out.print((char)c+" ");
}
bis.close();
}catch(IOException e){
e.printStackTrace();
}
}
}

例2:BufferedWriter BufferedRead 应用非常普遍(记住)

import java.io.*;
public class TestBufferStream{
public static void main(String args[]){
try{
BufferedWriter bw = new BufferedWriter(new FileWriter("TestCopyByMyself.java"));
BufferedReader br = new BufferedReader(new FileReader("TestCopyByMyself.java"));
String s = null ;
for(int i=0; i<=100; i++){
s = String.valueOf(Math.random());//返回字符串表示形式
bw.write(s);
bw.newLine();
}
bw.flush();
while((s=br.readLine())!= null){
System.out.println(s);
}
bw.close();
br.close();
}catch(IOException e){
e.printStackTrace();
}
}
}

2、转换流(重要)

例1:

import java.io.*;
public class TestTransForm{
public static void main(String args[]){
try{
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("TestCopyByMyself.java"));
osw.write("fgatwesdghruklotueo");
System.out.println(osw.getEncoding());
osw.close();
osw = new OutputStreamWriter(new FileOutputStream("TestCopyByMyself.java",true),"ISO8859_1");//true是在原文件基础上追加,否则覆盖原内容
//ISO8859_1是字符编码,默认编码GBK
osw.write("fgatwesdghruklotueo");
System.out.println(osw.getEncoding());
osw.close();
}catch(IOException e){
e.printStackTrace();
}
}
}

例2、典型用法

import java.io.*;
public class Test{
public static void main(String args[]){
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String s = null ;
try{
while((s = br.readLine())!=null){
if(s.equalsIgnoreCase("exit"))
break;
System.out.println(s.toUpperCase());
}
br.close();
}catch(IOException e){
e.printStackTrace();
}
}
}

3、数据流

例1

import java.io.*;
public class TestDataStream{
public static void main(String args[]){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
try{
dos.writeDouble(Math.random());
dos.writeBoolean(true);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
System.out.println(bais.available());//字节数=9
DataInputStream dis = new DataInputStream(bais);
System.out.println(dis.readDouble());
System.out.println(dis.readBoolean());
dos.close();
dis.close();
}catch(IOException e){
e.printStackTrace();
}
}
}

输出:

9
0.5303779743857895
true

练习:自己读写文件,注意文件中显示的并非double+boolean

import java.io.*;
public class Test{
public static void main(String [] args){
String filename = "data.txt";
try{
DataOutputStream dos = new DataOutputStream(new FileOutputStream(filename));
DataInputStream dis = new DataInputStream(new FileInputStream(filename));
dos.writeDouble(6.7);
dos.writeInt(3);
System.out.println(dis.available());
System.out.println(dis.readDouble());
System.out.println(dis.readInt());
dos.close();
dis.close();
}catch(IOException e){
e.printStackTrace();
} }
}

4、Print流

例1:

import java.io.*;
public class TestPrintStream{
public static void main(String args[]){
PrintStream ps = null ;
try{
FileOutputStream fos = new FileOutputStream("log.dat");
ps = new PrintStream(fos);
}catch(IOException e){
e.printStackTrace();
}
if(ps!=null){
System.setOut(ps);//System.out中的out默认是命令行,setOut可以改变out的值,这里out指向ps。
}
int ln = 1;
for(char c=0;c<=6000;c++){
System.out.print(c+" ");
if(ln++>=10){
System.out.println();
ln = 1;
}
}
} }

例2:

import java.io.*;
//输入文件名,读该文件内容
public class TestPrintStream{
public static void main(String args[]){
if(args.length<=0)
return;
String filename = args[0];
if(filename!=null){
list(filename,System.out);
}
}
public static void list(String f, PrintStream fs){
try{
BufferedReader br = new BufferedReader(new FileReader(f));
String s = null;
while((s = br.readLine())!=null){
fs.println(s);
}
br.close();
}catch(IOException e){
fs.println("无法读取文件");
}
}
}

例3:日志

import java.io.*;
import java.util.*;
public class TestPrintStream{
public static void main(String args[]){
String s = null;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try{
FileWriter fw = new FileWriter("logfile.log",true);
PrintWriter log = new PrintWriter(fw);
while((s = br.readLine())!=null){
if(s.equalsIgnoreCase("exit"))
break;
System.out.println(s.toUpperCase());
log.println("-----");
log.println(s.toUpperCase());
log.flush();
}
log.println("==="+new Date()+"===");
log.flush();
log.close();
}catch(IOException e){
e.printStackTrace();
}
}
}

日志的另一种写法(by myself):

import java.io.*;
import java.util.*;
public class Test{
public static void main(String [] args){
String filename = "log.txt" ;
String line = null ;
try{
InputStreamReader fr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(fr);
FileWriter fw = new FileWriter(filename);
/*
PrintWriter pw = new PrintWriter(fw); while((line = br.readLine()) != null){
if(line.equalsIgnoreCase("exit")){
break;
}
pw.println(line);
}
pw.write("==="+new Date()+"===");
pw.flush();
pw.close();
*/
BufferedWriter bw = new BufferedWriter(fw);
while((line=br.readLine()) != null){
if(line.equalsIgnoreCase("exit")){
break;
}
bw.write(line);
bw.newLine();
}
bw.write("==="+new Date()+"===");
bw.flush();
bw.close();
}catch(IOException e){
e.printStackTrace();
} }
}

5、Object流

直接将Object写入或读出。

必须实现seriallzable接口(序列化),是标记性接口,JDK控制序列化过程。

transient关键字修饰的成员变量在序列化时不考虑,即只写入其他三个,而忽略该成员变量。

externalizable接口(外部化),是seriallzable的子接口。此外还有两个方法,自己控制序列化过程。

例1:

import java.io.*;
public class Test{
public static void main(String [] args){
String filename = "obj.txt";
Obj o = new Obj();
try{
FileOutputStream fos = new FileOutputStream(filename);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(o);
oos.flush();
oos.close();
FileInputStream fis = new FileInputStream(filename);
ObjectInputStream ois = new ObjectInputStream(fis);
Obj obj = (Obj)ois.readObject();
System.out.println(obj.a+" "+obj.b+" "+obj.c);
}catch(IOException e){
e.printStackTrace();
}catch(ClassNotFoundException e){
e.printStackTrace();
}
}
} class Obj implements Serializable{
int a = 1 ;
String b = "good" ;
transient int c = 9 ;
}

输出:

1 good 0

总结:

JAVA笔记25-IO流(3)-处理流举例的更多相关文章

  1. JAVA笔记12__字节、字符缓冲流/打印流/对象流/

    /** * !!:以后写流的时候一定要加入缓冲!! * 对文件或其它目标频繁的读写操作,效率低,性能差. * 缓冲流:好处是能更高效地读写信息,原理是将数据先缓冲起来,然后一起写入或读取出来. * * ...

  2. Java笔记(二十六)……IO流上 字节流与字符流

    概述 IO流用来处理设备之间的数据传输 Java对数据的操作时通过流的方式 Java用于操作流的对象都在IO包中 流按操作的数据分为:字节流和字符流 流按流向不同分为:输入流和输出流 IO流常用基类 ...

  3. Java编程思想学习笔记_5(IO流)

    一.用DataInputStream读取字符 可以使用available方法查看还有多少可供存取的字符.示例如下: public class Test1 { public static void ma ...

  4. Java基础知识强化之IO流笔记65:序列化流 和 反序列化流

    1. 什么是 序列化 和 反序列化 ?     序列化 (Serialization):将对象的状态信息转换为可以存储或传输的形式的过程.比如转化为二进制.xml.json等的过程. 在序列化期间,对 ...

  5. Java学习笔记29(IO字符流,转换流)

    字符流:只能操作文本文件,与字节流的区别是,字节流是按照字节来读取文件,而字符流是按照字符来读取,因此字符流的局限性为文本文件 字符输出流:Write类,使用时通过子类   每一次写入都要刷新 pac ...

  6. Java基础知识_毕向东_Java基础视频教程笔记(19-21 IO流)

    18天-06-IO流 字节流和字符流 字节流两个基类:InputStream,FileInputStream,BufferedInputStream OutputStream,FileOutputSt ...

  7. java学习笔记之IO编程—内存流、管道流、随机流

    1.内存操作流 之前学习的IO操作输入和输出都是从文件中来的,当然,也可以将输入和输出的位置设置在内存上,这就需要用到内存操作流,java提供两类内存操作流 字节内存操作流:ByteArrayOutp ...

  8. java 笔记(4) —— java I/O 流、字节流、字符流

    Java中使用流来处理程序的输入和输出操作,流是一个抽象的概念,封装了程序数据于输入输出设备交换的底层细节.JavaIO中又将流分为字节流和字符流,字节流主要用于处理诸如图像,音频视频等二进制格式数据 ...

  9. 详细讲解JAVA中的IO流

    一.流的概念        流(stream)的概念源于UNIX中管道(pipe)的概念.在UNIX中,管道是一条不间断的字节流,用来实现程序或进程间的通信,或读写外围设备.外部文件等.        ...

  10. Java基础:IO流之字节流和字符流

    1. 流的概念 流(stream)的概念源于UNIX中管道(pipe)的概念.在UNIX中,管道是一条不间断的字节流,用来实现程序或进程间的通信,或读写外围设备.外部文件等. 一个流,必有源端和目的端 ...

随机推荐

  1. Matlab学习笔记1—MATLAB基础知识

    1.1  MATLAB系统环境 1.MATLAB操作界面的组成 (1)MATLAB主窗口 (2)命令行窗口:命令行窗口用于输入命令并显示命令的执行结果. (3) 当前文件夹窗口 如何设置当前文件夹呢? ...

  2. C++类大小的计算

    这里记录一下怎么计算类对象的大小. 大概总结下,类的大小需要考虑以下内容: 非静态成员变量大小 数据对齐到多少位 有无虚函数(即需不需要指向虚函数表的指针,如果考虑继承的情况,则还需要看继承了多少个指 ...

  3. Spring cloud 项目———酷派手机商城 (话术)1.0

     酷派电商网站 描述: 随着电子商务的发展,网上购物正在趋于一种时尚,电子商务网站也逐渐成为企业顺应潮流的标配.大多数人知道可能在电子商务网站前端有查询,注册登录,购物车等等功能.可是您知道建设电子商 ...

  4. 深入理解java:4.2. 框架编程之Spring框架的设计理念

    什么是Spring呢? Spring是为了解决企业应用开发的复杂性而创建的一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架. Spring优点 简单了解Spring之后,我们看一下Spri ...

  5. Android 透明主题

    转至:https://blog.csdn.net/zhangwenchaochao/article/details/78654128 Activity采用透明主题有两种方式: 重要的内容说三遍: 采用 ...

  6. 异常1(Exception)

    父类 :Throwable(可抛出的) 有两个子类:Error(错误)       Exception(异常) Error是所有错误类的父类,Exception是所有异常类的父类. 如图所示: 格式: ...

  7. Python 用户交互程序(day1)

    一: 变量 变:变化,重在变字,量:计量,衡量,表示一种状态 变量赋值 : number = 1 变量的规则: 数字,字母,下划线, 任意组合,数字不能开头,python 的关键字不能用,变量名尽量有 ...

  8. AKKA学习(一)

    AKKA简介 什么是AKKA Akka是一个由Scala编写的,能兼容Sacala和JAVA的,用于编写高可用和高伸缩性的Actor模型框架.它基于了事件驱动的并发处理模式,性能非常的高,并且有很高的 ...

  9. 03: saltstack和ansible的区别和原理

    1.1 SaltStack.Ansible.Puppet比较 1.SaltStack 1. saltStack由Python编写,为server-client模式的系统,自己本身支持多master. ...

  10. shell with hadoop

    shell 命令操作 hadoop 之前多少提及过,这里做个总结. shell with hdfs 基本命令 bin/hadoop fs 大于下面的命令 bin/hdfs dfs dfs 是 fs 的 ...