javaIO操作下(内存流、打印流、System的支持)

来源:互联网 发布:如何搭建数据库 编辑:程序博客网 时间:2024/06/08 06:54

1、  内存操作流ByteArrayOutputStream和ByteArrayInputStream

注意:在上一篇中聊到的FileOutputStream、Writer(从程序写入文件)和FileInputStream、Reader(从文件读出程序)是把内容对文件的操作

而现在ByteArrayOutputStream(从内存读出程序)和ByteArrayInputStream(从程序写入内存)是把内容对内存的操作。

ByteArrayOutputStream、ByteArrayInputStream的操作与文件流的操作一样只是目标不同罢了,构成方法:public ByteArrayInputStream(byte [] buf)

AyteArrayOutputStream来说,基本作用域OutputStream一样,一个个的读取数据。

范例:使用内存操作流,完成一个字符串保存拿出操作。

public class ByteArrayTest {

    public static void main(String[] args) {

       String str = "hello";

       ByteArrayInputStream bin = new ByteArrayInputStream(str.getBytes());//实例化输入流

       ByteArrayOutputStream bout = new ByteArrayOutputStream();//实例化输出流

       int temp = 0;

       while((temp = bin.read())!=-1){//直到末尾

           bout.write(temp);

       }

       String result = bout.toString();//拿出缓存中的数据

       System.out.print(result);

    }

}

PrintStream 永远不会抛出IOException,可以输出所有的类型数据。

 

2、  打印流:PrintStream、PrinWriter

OutputStream可以完成数据的输出,但是并不是对所有的输出都方便,例如输出一个float型的数据都要把float变为byte类型,比较麻烦。所有打印流就实现了对输出变为方便。

printStream是OutputStream的子类,其构造方法,public PrintStream(OutputStream out);

呵呵,是否看出,实际上PrintStream是属于装饰模式,根据不同的OutPutStream子类的实例化,输出位置不同。

范例:使用PrintStream向文件输出。

public class PrintStreamTest {

    public static void main(String[] args) throws FileNotFoundException{

       File file = new File("d:"+File.separator+"demodemo.txt");

       PrintStream out = new PrintStream(new FileOutputStream(file));

       out.print("woanid");

       out.println("是不是与平时一样啊!!呵呵");

       out.println("Gout把");

        out.println(12.25);

    }

}

记住:以后所有的输出操作都应该有PrintStream实现。

 

3、  System类对IO的支持:

三个常量作用:

static PrintStream

err “标准”错误输出流。

static InputStream

in “标准”键盘的输入流。

static PrintStream

out “标准”输出流。

所有System对IO的支持都是PrintStream实例;

public class SystemTest {

    public static void  main(String[] args) throws IOException{

       System.out.print("请输入你的姓名:");

       byte[] name = new byte[20];

       System.in.read(name);

       System.out.println(name.toString());

    }

}

/*

请输入你的姓名:liangrendy

[B@dc8569

*/

上面的打印会有乱码:解决办法;看下面BufferedReader

BufferedReader表示的是在缓冲区中读取,可以一次性将内容全部读取进来。

构造方法:public BufferedReader(Reader in)

因为System.in输入的内容是InputStream类型。在java中提供了字节流转换成字符流的转换类.。

         ·InputStreamReader类:表示将字节的输入变为字符流。

         ·OutputStreamReader类:表示将字符的输出流变为字节的输出。

BufferedReader提供了一个读取数据的方法:publicString readLine()。

所有上面的代码可以改为:

package com.jtlyuan.system;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

public class SystemTest {

    public static void  main(String[] args) throws IOException{

       BufferedReader buf = null;

//此方式是标准的键盘输入方式

       buf = new BufferedReader(new InputStreamReader(System.in));

       System.out.print("请输入你的姓名:");

       String name = buf.readLine();

       System.out.println("你的名字是"+name);

    }

}

/*

请输入你的姓名:梁任元

你的名字是梁任元*/

以上的方式是标准的键盘输入方式

 

输入输出的重定向:

System.out、System.err的输出目标是屏幕。

System.in是从键盘中输入。

但是System类中提过了输入输出的重定向修改方法,如下:

System.out重定向:public  static void setout(PrintStreamout)

System.err重定向:public staticvoid setErr(PrintStream err)

System.in 重定向:public staticvoid setIn(InputStream in);

 

范例:把输出的位置设置为输出到文件。

package com.jtlyuan.system;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.PrintStream;

public class SystemTest {

    public static void  main(String[] args) throws IOException{

       File file = new File("d:"+File.separator+"test.txt");

       System.setOut(new PrintStream(new FileOutputStream(file)));

       System.out.println("内容输出到文件中");

    }

}