黑马程序员_IO流_IO包中的其他类
来源:互联网 发布:java中异常处理机制 编辑:程序博客网 时间:2024/05/17 00:52
------------------------------android培训、java培训、期待与您交流!----------------------------------------------------------------
IO包中的其他类
打印流 •PrintWriter与PrintStream
• 可以直接操作输入流和文件。
序列流 •SequenceInputStream
• 对多个流进行合并。
操作对象
• ObjectInputStream与ObjectOutputStream
• 被操作的对象需要实现Serializable (标记接口);
1. java.lang.Object
java.io.OutputStream
java.io.FilterOutputStream
java.io.PrintStream
字节打印流:
PrintStream
构造函数可以接收的参数类型:
1,file对象。File
2,字符串路径。String
3,字节输出流。OutputStream
java.lang.Object
java.io.Writer
java.io.PrintWriter
字符打印流:
PrintWriter
构造函数可以接收的参数类型:
1,file对象。File
2,字符串路径。String
3,字节输出流。OutputStream
4,字符输出流,Writer。
importjava.io.*;
class PrintStreamDemo
{
public static void main(String[] args)throws IOException
{
BufferedReader bufr =
new BufferedReader(newInputStreamReader(System.in));
PrintWriterout = new PrintWriter(new FileWriter("a.txt"),true);//用来自动刷新.
/*public PrintWriter(Writer out, boolean autoFlush
autoFlush - boolean 变量;如果为 true,刷新输出缓冲区*/
String line = null;
while((line=bufr.readLine())!=null)
{
if("over".equals(line))//结束标记
break;
out.println(line.toUpperCase());//相当于BufferedWriter中的newLine()方法.换行的标记.
//out.flush();放在缓冲区中,要刷新.
}
out.close();
bufr.close();
}
}
2. java.lang.Object
java.io.InputStream
java.io.SequenceInputStream
没有SequenceOutputStream,
需求:将多个文件中的数据合并成一个文件.
SequenceInputStream
(Enumeration<? extendsInputStream> e)
通过记住该参数初始化新创建的 SequenceInputStream
,该参数必须是生成运行时类型为 InputStream
对象的Enumeration
型参数。
importjava.io.*;
importjava.util.*;
classSequenceDemo
{
public static void main(String[] args)throws IOException
{
Vector<FileInputStream>v = new Vector<FileInputStream>();//只有Vector中才有返回Enumeration
的方法.
Enumeration<E>
elements()
返回此向量的组件的枚举。
v.add(newFileInputStream("c:\\1.txt"));
v.add(newFileInputStream("c:\\2.txt"));
v.add(newFileInputStream("c:\\3.txt"));
Enumeration<FileInputStream>en = v.elements();
SequenceInputStream sis = newSequenceInputStream(en);//多个读取流对象变成一个读取流.
FileOutputStream fos = newFileOutputStream("c:\\4.txt");//字节流 不涉及到编码.
byte[] buf= new byte[1024];//建立缓冲区.
int len =0;
while((len=sis.read(buf))!=-1)
{
fos.write(buf,0,len);
}
fos.close();
sis.close();
}
}
需求:将一个文件中的数据切割成多个文件.
importjava.io.*;
importjava.util.*;
classSplitFile
{
public static void main(String[] args)throws IOException
{
//splitFile();
merge();
}
public static void merge()throws IOException//首先要创建集合,Vertor集合效率低,
{
ArrayList<FileInputStream>al = new ArrayList<FileInputStream>();
for(int x=1; x<=3; x++)
{
al.add(new FileInputStream("c:\\splitfiles\\"+x+".part"));//多个流对象添加进去,和读取流对象相关联,然后在添加到集合中.
}
finalIterator<FileInputStream> it = al.iterator();//叠代器里有Enumeration
//final修饰匿名内部类.
Enumeration<FileInputStream>en = new Enumeration<FileInputStream>()
{
public booleanhasMoreElements()
{
return it.hasNext();
}
public FileInputStreamnextElement()
{
return it.next();
}
};
SequenceInputStreamsis = new SequenceInputStream(en);//需要传入Enumeration
FileOutputStream fos = newFileOutputStream("c:\\splitfiles\\0.bmp");
byte[] buf = new byte[1024];
int len = 0;
while((len=sis.read(buf))!=-1)//往buf中读数据.
{
fos.write(buf,0,len);
}
fos.close();
sis.close();
}
public static void splitFile()throwsIOException
{
FileInputStream fis = new FileInputStream("c:\\1.bmp");//源只有一个.
FileOutputStream fos = null;
byte[] buf = new byte[1024*1024];//刚好1M的数据.
int len = 0;
int count= 1;//从1开始
while((len=fis.read(buf))!=-1)//每循环一次,生成一个文件.
{
fos = new FileOutputStream("c:\\splitfiles\\"+(count++)+".part");//碎片文件.part, 目的有多个,在循环里new
fos.write(buf,0,len);//往流里写数据.
fos.close();
}
fis.close();
}
}
3. java.lang.Object
java.io.InputStream
java.io.ObjectInputStream
java.lang.Object
java.io.OutputStream
java.io.ObjectOutputStream
对象的持久化存储.或者叫对象的序列化.
需求:将一个对象存储到文件中.
ObjectInputStream和ObjectOutputStream成对出现.
importjava.io.*;
classObjectStreamDemo
{
public static void main(String[] args)throws Exception
{
//writeObj();
readObj();
}
public static void readObj()throwsException//将封装的对象中的数值读出来.
{
ObjectInputStream ois = newObjectInputStream(new FileInputStream("obj.txt"));//能直接操作对象的流.
Person p =(Person)ois.readObject();
System.out.println(p);
ois.close();
}
public static void writeObj()throwsIOException
{
ObjectOutputStream oos =
new ObjectOutputStream(newFileOutputStream("obj.txt"));
oos.writeObject(newPerson("lisi0",399,"kr"));//将对象写入硬盘中.
oos.close();
}
}
importjava.io.*;
classPerson implements Serializable//是个标记接口,没有方法来实现.
{
public static final long serialVersionUID= 42L;//自定义一个UID,不让编译器生成.给类定义一个固定标识.
private String name;
transientint age;//对非静态成员 也不想被序列化.虽然在堆内存中被序列化,但在文本中不被序列化.
static String country=”cn”;//静态不能被序列化.不在堆内存中
Person(String name,int age,Stringcountry)
{
this.name = name;
this.age = age;
this.country = country;
}
public String toString()
{
returnname+":"+age+":"+country;
}
}
4. java.lang.Object
java.io.InputStream
java.io.PipedInputStream
管道流
• PipedInputStream和PipedOutputStream
• 输入输出可以直接进行连接,通过结合线程使用。
• 涉及到多线的IO流对象. 集合当中涉及到IO流的是Properties
需求: 用多线程演示管道流.
importjava.io.*;
class Read implements Runnable//读的线程.
{
private PipedInputStream in;
Read(PipedInputStream in)
{
this.in = in;
}
public void run()
{
try
{
byte[] buf = newbyte[1024];
System.out.println("读取前。。没有数据阻塞");
int len = in.read(buf);
System.out.println("读到数据。。阻塞结束");
String s= newString(buf,0,len);
System.out.println(s);//输出读到的数据.
in.close();
}
catch (IOException e)
{
throw newRuntimeException("管道读取流失败");
}
}
}
class Write implements Runnable//写的线程.
{
private PipedOutputStream out;
Write(PipedOutputStream out)
{
this.out = out;
}
public void run()
{
try
{
//System.out.println("开始写入数据,等待6秒后。");//演示效果.
//Thread.sleep(6000);
out.write("piped lai la".getBytes());//用字节写入.
out.close();
}
catch (Exception e)
{
throw newRuntimeException("管道输出流失败");
}
}
}
class PipedStreamDemo
{
public static void main(String[] args)throws IOException
{
PipedInputStream in = newPipedInputStream();
PipedOutputStream out = newPipedOutputStream();
in.connect(out);
Read r = new Read(in);//自定义的类Read.
Write w = new Write(out);
new Thread(r).start();
new Thread(w).start();
}
}
5. java.lang.Object
java.io.InputStream
java.io.FilterInputStream
java.io.DataInputStream
java.lang.Object
java.io.OutputStream
java.io.FilterOutputStream
java.io.DataOutputStream
可以用于操作基本数据类型的数据的流对象。
操作基本数据类型
• DataInputStream与DataOutputStream
需求:用DataInputStream与DataOutputStream演示操作基本数据类型.
importjava.io.*;
classDataStreamDemo
{
public static void main(String[] args)throws IOException
{
}
publicstatic void writeData()throws IOException//基本数据类型写入.
{
DataOutputStream dos = newDataOutputStream(new FileOutputStream("data.txt"));
dos.writeInt(234);
dos.writeBoolean(true);
dos.writeDouble(9887.543);//8个字节. 该long
值以 8-byte 值形式写入基础输出流
dos.close();
ObjectOutputStream oos = null;
oos.writeObject(new O());
}
publicstatic void readData()throws IOException//将写进的数据读出来.
{
DataInputStream dis = newDataInputStream(new FileInputStream("data.txt"));
int num = dis.readInt();
boolean b = dis.readBoolean();
double d = dis.readDouble();
System.out.println("num="+num);
System.out.println("b="+b);
System.out.println("d="+d);
dis.close();
}
writeUTF
(String str)
以与机器无关方式使用UTF-8 修改版编码将一个字符串写入基础输出流。
publicstatic void writeUTFDemo()throws IOException
{
DataOutputStream dos = newDataOutputStream(new FileOutputStream("utfdate.txt"));
dos.writeUTF("你好");//英文不涉及UTF-8编码.
dos.close();
}
static String
readUTF(DataInput in)
从流 in 中读取用 UTF-8 修改版格式编码的 Unicode 字符格式的字符串;然后以 String 形式返回此字符串。
publicstatic void readUTFDemo()throws IOException//
{
DataInputStream dis = newDataInputStream(new FileInputStream("utf.txt"));
String s = dis.readUTF();//writeUTF()写进的要readUTF()读出来.
System.out.println(s);
dis.close();
}
}
6. java.lang.Object
java.io.InputStream
java.io.ByteArrayInputStream
java.lang.Object
java.io.OutputStream
java.io.ByteArrayOutputStream
操作字节数组的流对象:用流的思想操作思想.
• ByteArrayInputStream与ByteArrayOutputStream
• 这个流对象没有调用底层的资源.
• 关闭ByteArrayInputStream 无效。此类中的方法在关闭此流后仍可被调用, 不会产生任何 IOException。
ByteArrayInputStream:在构造的时候,需要接收数据源,。而且数据源是一个字节数组。
ByteArrayOutputStream: 在构造的时候,不用定义数据目的,因为该对象中已经内部封装了可变长度的字节数组。
这就是数据目的地。
因为这两个流对象都操作的数组,并没有使用系统资源。
所以,不用进行close关闭。
importjava.io.*;
classByteArrayStream
{
public static void main(String[] args) 没有异常发生.
{
//数据源。
ByteArrayInputStream bis = newByteArrayInputStream("ABCDEFD".getBytes());
//数据目的
ByteArrayOutputStream bos = newByteArrayOutputStream();//构造方法可以没有参数.
int by = 0;
while((by=bis.read())!=-1)//InputStream的子类,并不用判断数组长度.
{
bos.write(by);
}
System.out.println(bos.size());
System.out.println(bos.toString());
// bos.writeTo(newFileOutputStream("a.txt"));
}
}
在流操作规律讲解时:
源设备,
键盘System.in,硬盘 FileStream,内存 ArrayStream。
目的设备:
控制台System.out,硬盘FileStream,内存 ArrayStream。
用流的读写思想来操作数据。
void
writeTo(OutputStream out)
将此字节数组输出流的全部内容写入到指定的输出流参数中,这与使用 out.write(buf, 0, count) 调用该输出流的 write 方法效果一样。
操作字符数组
• CharArrayReader与CharArrayWrite
操作字符串
• StringReader 与 StringWriter
-----------------------------android培训、java培训、期待与您交流!----详细请查看:http://edu.csdn.net/heima---------------
- 黑马程序员_IO流_IO包中的其他类
- 黑马程序员_IO包中的其他类
- 黑马程序员_IO包中的其他类
- 黑马程序员_IO流中的其他对象
- 黑马程序员_IO中其他流对象
- 黑马程序员_java语言_IO其他流
- 黑马程序员_IO流(二)_File类、其他流
- 黑马程序员_IO流(二)_File类、其他流
- 黑马程序员_IO流
- 黑马程序员_IO流
- 黑马程序员_IO流
- 黑马程序员_IO流
- 黑马程序员_IO流
- 黑马程序员_IO流
- 黑马程序员_IO流
- 黑马程序员_IO流
- 黑马程序员_IO流
- 黑马程序员_IO流
- 黑马程序员_IO流_Properties学习
- 关于程序员的几个笑话
- 黑马程序员_IO流_File类的使用
- dev 控件常见操作,收藏备用
- 【集体智慧编程 学习笔记】 Euclidean距离和Pearson相关系数
- 黑马程序员_IO流_IO包中的其他类
- BASH脚本(ok2makefile):生成带有makefile的C++ "Hello world"程序
- 表达式和流程控制语句
- 新买的笔记本电脑,已经装了正版win7,但是只有一个C盘怎么办?
- 办公室的事儿
- php/文件数组转换
- 关于 UML 模型(Visio的说明)
- git 基本命令使用总结
- 关于 UML 模型(Visio的说明)