黑马程序员----IO流(二)

来源:互联网 发布:centos设置ip地址命令 编辑:程序博客网 时间:2024/05/18 08:03

------- android培训、java培训、期待与您交流! ----------

一、基本数据类型操作流

 DataInputStream   继承InputStream

 DataOutputStream  继承OutputStream

DataOutputStream 构造方法 ,传递字节输出流,字节输出流包装文件。DataOutputStream,将基本数据类型写入到文件中,writeInt。

DataInputStream 于机器无关,读取JAVA基本类型,任何机器上JAVAint类型都是4字节。构造方法:传递字节输入流,字节输入流包装文件。DataInputStream,将数据从字节流中,读取基本类型,int readInt 读取int 4字节,读取文件结尾的时候 抛出异常 java.io.EOFException。

public class DataStreamDemo {
public static void main(String[] args) throws Exception {
readWhile();
//write();
}
// 定义方法,循环读取基本类型,包装,基本类型有规律的
public static void readWhile() throws Exception {
// 创建基本数据类型输入流,传递字节输入流,包装文件
DataInputStream dis = new DataInputStream(new FileInputStream("c:\\data.txt"));
//循环读取基本类型,不能依赖-1停止循环,利用异常
while(true){
 try{
int x = dis.readInt();
System.out.println(x);
 }catch(IOException e){
 break;
 }
}

// 定义方法,读取文件中基本数据类型,int 4个字节
public static void read() throws Exception {
// 创建基本数据类型输入流,传递字节输入流,包装文件
DataInputStream dis = new DataInputStream(new FileInputStream("c:\\data.txt"));
// 调用方法 readInt读取基本类型
int x = dis.readInt();
System.out.println(x);

// 调用方法readBoolean读取布尔值
boolean b = dis.readBoolean();
System.out.println(b);
dis.close();
}
// 定义方法,将基本类型int,4个字节写入到文件中
public static void write() throws Exception {
// 创建基本数据类型输出流,传递字节输出流,包装文件
DataOutputStream dos = new DataOutputStream(new FileOutputStream("c:\\data.txt"));
// 写一个基本数据类型 writeInt
dos.writeInt(97);
dos.writeInt(98);
dos.writeInt(99);
// dos.writeBoolean(false);
dos.close();
}
}

二、内存流

内存流读写都在内存中进行。内存流,不占用操作系统资源 不会抛出异常 ,关闭无效。

读写字节数组

ByteArrayInputStream 读取字节数组
ByteArrayOutputStream 写字节数组

ByteArrayOutputStream 继承OutputStream,标准字节输出流。

ByteArrayOutputStream() 空参数,不需要文件,根本不操作文件,因为写的是字节数组到内存中。

方法  byte[] toByteArray() 直接从内存流中,获取字节数组。

         String toString()将流中的字节数组,变成字符串,但是查询编码表。

ByteArrayInputStream  构造方法中,传递字节数组,就是他的数据源。ByteArrayInputStream(byte[] buf)。

import java.io.*;
public class ByteArrayStreamDemo {
public static void main(String[] args) throws Exception{

//method();

method_1();

method_2();
}
/*
* 流对象配合以下,从内存中获取字节数组
* 字节输出流,写文件中
*/
public static void method_2()throws Exception{
//定义字节数组
byte[] bytes = "welcome to ".getBytes();
//内存流,将数组写在内存中
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bos.write(bytes);

//创建字节输出流
FileOutputStream fos = new FileOutputStream("c:\\welcome.txt");
//fos写字节数组,数组从 bos内存流中获取
fos.write(bos.toByteArray());

fos.close();
}

/*
* 创建字节数组输入流,直接读取构造方法中的字节数组
*/
public static void method_1()throws Exception{
ByteArrayInputStream bis = new ByteArrayInputStream("abcd".getBytes());
/*int len = bis.read();
System.out.println(len);*/
int len = 0 ;
byte[] bytes = new byte[1024];
while((len = bis.read(bytes))!=-1){
System.out.print(new String(bytes));
}
/*while((len = bis.read())!=-1){
System.out.print((char)len);
}*/
}

/*
* 创建字节数组输出流,将字节数组写入内存
* 实际使用数据,临时保存
*/
public static void method()throws Exception{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bos.write("abcde".getBytes());
bos.close();
bos.write("汉字".getBytes());

//调用toByteArray()获取流中的数组
byte[] bytes = bos.toByteArray();
for(byte b : bytes){
System.out.println(b);
}

String s = bos.toString();
System.out.println(s);
}
}

三、打印流

PrintStream   PrintWriter

打印流,作用,为其他输出流添加打印功能  打印流装饰流。特点:打印流不操纵数据源,只操作数据的目的,永远不会抛出IO异常。

PrintStream 字节输出流  继承 OutputStream

PrintWriter 字符输出流  继承 Writer

区别,两个打印流支持的数据目的不同。

PrintStream 构造方法接受 File 数据目的 OutputStream 字节输出流目的  String 文件名目的。

PrintWriter 构造方法接受 File 数据目的  OutputStream 字节输出流目的 String 文件名目的  Writer 字符输出流目的。

输出方法 print     println 完全一样。打印流:在服务器端,使用打印流,将数据打印到客户端的浏览器。如果遇到输出,优先考虑打印流,方便简单。

import java.io.*;
public class PrintWriterDemo {
public static void main(String[] args)throws Exception {
method_2();
}
/*
* 打印流复制文本
* 打印目的,数据目的
* 数据源c:\\ print.txt  目的 d:\\print.txt
*  readLine()            println()
*/
public static void method_2()throws Exception{
BufferedReader bfr = new BufferedReader(new FileReader("c:\\print.txt"));
PrintWriter pw = new PrintWriter(new FileWriter("d:\\print.txt"),true);
String line = null;
while((line = bfr.readLine())!=null){
pw.println(line);
}
pw.close();
bfr.close();
}
/*
* 打印流,可以开启自动刷新
* 要求:打印流构造方法中,封装的数据目的,必须是流对象,第二参数 加true
*     启动自动刷新,必须调用println,printf format 三个方法,才有效
*/
public static void method_1()throws Exception{
PrintWriter pw = new PrintWriter(new FileOutputStream("c:\\print.txt"),true);
pw.println("哈哈") ;
pw.println("嘻嘻") ;
pw.println(true) ;
pw.println(100) ;

pw.close();
}

/*
* 打印流,将数据打印到目的地
* 目的地,由打印流构造方法决定
*/
public static void method()throws Exception{
//创建打印流对象,传递数据目的
PrintWriter pw = new PrintWriter(new File("c:\\print.txt"));
//打印流打印方法,print方法,向目的中打印数据
pw.println("字符串");
pw.flush();

pw.print("下一行");
pw.flush();

pw.close();
}
}

四、标准输入流、输出流

System.in :InputStream 字节输入流,BufferedInputStream

System.out:PrintStream 打印流 , OutputStreamWriter 转换流

import java.io.*;
public class TranseDemo {
public static void main(String[] args) throws Exception{
InputStream in = System.in;
//创建转换流,字节转成字符,传递字节输入流
InputStreamReader isr = new InputStreamReader(in);
//字符输入流缓冲区 BufferedReader缓冲所有Reader子类
BufferedReader bfr = new BufferedReader(isr);
String line = null;
while((line = bfr.readLine())!=null){
if("over".equals(line))
break;
System.out.println(line);
}
bfr.close();
}
public static void method()throws Exception{
StringBuilder builder = new StringBuilder();
InputStream in = System.in;
//调用字节流read方法读取键盘  read() 返回int值,文件末尾-1
int len = 0 ;
while((len = in.read())!=-1){
if(len == '\r')
continue;
if(len == '\n'){
System.out.println(builder);
builder.delete(0, builder.length());
}else{
builder.append((char)len);
}
}
in.close();
}
}

import java.io.*;
public class TranseDemo1 {
public static void main(String[] args)throws Exception {
InputStream in = new FileInputStream("src\\cn\\itcast\\iostream\\transedemo1.java");
//创建转换流,字节转成字符,传递字节输入流
InputStreamReader isr = new InputStreamReader(in);
//字符输入流缓冲区 BufferedReader缓冲所有Reader子类
BufferedReader bfr = new BufferedReader(isr);

//创建标准输出流对象
OutputStream out = new FileOutputStream("c:\\a.txt");
//创建转换流,字符转成字节流,OutputStreamWriter,包装输出流
OutputStreamWriter osw = new OutputStreamWriter(out);
//字符输出流,缓冲转换流,实现newLine
BufferedWriter bfw = new BufferedWriter(osw);

String line = null;
while((line = bfr.readLine())!=null){
if("over".equals(line))
break;
bfw.write(line.toUpperCase());
bfw.newLine();
bfw.flush();
 
//System.out.println(line);
}
bfw.close();
bfr.close();

}
}

五、随机读写流

随机读写流不属于流对象,即可读,还可以写。

以前的流对象,读取从文件开头读,写开头写,末尾追加写。而RandomAccessFile,在文件任意位置上,开始读和写。

RandomAccessFile 隐含大型字节数组,文件都是由字节组成,文件二进制,放在自己的数组中。大型字节数组,指针,索引,文件指针,操作文件指针,达到随机读写的目的。seek(long l)设置大型数组指针。

构造器:RandomAccessFile(File file, String mode)

      RandomAccessFile(String name, String mode)

传递File对象,包装文件
   传递字符串文件名
   字符串的模式:
    r: 只能读取
   RandomAccessFile("abc.txt","r")
   write 抛异常
   rw:可以读,可以写。

import java.io.*;
public class RandomAccessFileDemo {
public static void main(String[] args)throws Exception {

//method();
method_1();
}
/*
* 随机读写流
* 指定字节数进行读取
* 如果读取有规律的文件,使用循环读取
* 停下循环,捕获EOF异常
*/
public static void method_1()throws Exception{
//创建随机读写对象,写文件名rw模式
RandomAccessFile raf = new RandomAccessFile("c:\\random.txt", "rw");
while(true){
try{
//读取李四
//raf.seek(16);
byte[] bytes = new byte[4];
int len = raf.read(bytes);
System.out.println(new String(bytes));

int age = raf.readInt();
System.out.println(age);
}
catch(Exception e){
break;
}
}
raf.close();
}


/*
* 随机读写流,写数据
* 有规律的数据 张三  65  李四 66  王五 67
* 王五写在李四前面  李四9-16  王五站在9字节上
* seek(8) 移动文件指针,王五移动9字节上
* 李四应该在17字节 seek(16)
*/
public static void method()throws Exception{
//创建随机读写对象,写文件名rw模式
RandomAccessFile raf = new RandomAccessFile("c:\\random.txt", "rw");
//写数据 张三,字节数组
raf.write("张三".getBytes());
//年龄 65 基本数据类型
raf.writeInt(65);

raf.seek(16);
raf.write("李四".getBytes());
raf.writeInt(66);

raf.seek(8);
raf.write("王五".getBytes());
raf.writeInt(67);

raf.close();
}
}















0 0
原创粉丝点击