java中的Io(二)

Java.IO流类库

    java.io包中包含了流式I/O所需要的所有类。在java.io包中有四个基本类:InputStream、OutputStream及Reader、Writer类.


一. 字节流InputStream/OutputStream

 1.   InputStream 抽象类

   InputStream 为字节输入流,它本身为一个抽象类,必须依靠其他子类实现各种功能,此抽象类是表示字节输入流的所有类的超类。

    InputStream为输入字节数据用的类,它提供了三种重载的read方法。

   (1) public abstract int read()

    (2) public int read(byte b[])

    (3)public int  read (byte b[],int off,int len):从输入流中len个字节的数据,放到偏移量为off的数组中。

    (4) public int close()

2.   OutputStream抽象类

   (1).public void write(byte b[]);

    (2) public  void write(byte b[] ,int off,int len);

   (3)  public abstract  void write(int b);

3.文件输入流:FileInputStream类

  使用方法1:

  File fin = new File("d:/abc.text");

  FileInputStream in = new FileInputStream(fin);

  使用方法2:

  FileInputStream in = new FileInputStream("d:/abc.txt");

     程序举例:将InputFromFile.java的程序的内容显示在显示器上

import java.io.FileInputStream;
import java.io.IOException;


public class Test {
	public static void main(String[] args) {
		try{      
            FileInputStream rf = new FileInputStream("F:/User/zss/Desktop/new1.txt");  
            int n=512;  
            byte  buffer[]=new  byte[n];     
            while((rf.read(buffer,0,n)!=-1)&&(n>0)){ 
                System.out.println(new String(buffer));  
             }  
             System.out.println();  
             rf.close();  
		} catch(IOException  IOe){        
           System.out.println(IOe.toString());  
		}  	
	}
}
4.文件输出流:FileOutputStream类

  方式1:

 File f = new File("d:/myjava/write.txt");

FileOutputStream out = new FileOutputStream("d:/myjava/write.txt");

 方式2:

FileOuputStream out = new FileOutputStream("d:/myjava/write.txt");

方式3:构造函数将 FileDescriptor()对象作为其参数。

FileDescriptor() fd=new FileDescriptor(); 
FileOutputStream f2=new FileOutputStream(fd);


方式4:构造函数将文件名作为其第一参数,将布尔值作为第二参数。 

     * @param      file               the file to be opened for writing.
     * @param     append      if <code>true</code>, then bytes will be written
     *                   to the end of the file rather than the beginning
FileOutputStream f=new FileOutputStream("d:/abc.txt",true); 

程序举例:使用键盘输入一段文章,将文章保存在文件write.txt中

import java.io.FileOutputStream;
import java.io.IOException;

public class Test {
	public static void main(String[] args) {
		try{      
           System.out.println("please input from keyboard:");
           int count,n=512;
           byte buffer[] = new byte[n];
           count=System.in.read(buffer);
           FileOutputStream wf= new FileOutputStream("F:/User/zss/Desktop/new1.txt");//创建输出流
           wf.write(buffer, 0, count);//从偏移量为0开始的count个字节写到输出流
           wf.close();//关闭输出流
           System.out.println("save to the new1.txt");//每次保存会覆盖掉上次的内容
		} catch(IOException  IOe){        
           System.out.println("file write error!");  
		}  	
	}
}
5.缓冲输入输出流BufferInputStream/BufferOutputStream



BufferInputStream:当向缓冲区写入数据时,数据先写入缓冲区,待缓冲区写满后,系统一次性将数据发送给设备。

BufferOutputStream:当从缓冲区读取数据时,系统先从缓冲区读取数据,待缓冲区为空时候,系统再从输入设备读取数据到缓冲区。

1)将文件读入内存

将BufferInputStream与FileInputStream相连

FileInputStream in = new FileInputStream("fie.txt");

BufferInputStream bin = new BufferInputStream(in);

2)将内存写入文件

FileOutputStream out= new FileOutputStream("file.txt");

BufferOutputStream bin = new BufferOutputStream(out);

3)键盘输入流读到内存

将BufferReader与标准的的数据流相接

InputStreamReader sin = new InputStreamReader(System.in);

BufferReader  bin = new BufferReader(sin);

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

public class Test {
	public static void main(String[] args) {
		try{
			  InputStreamReader sin = new InputStreamReader(System.in);  
		        BufferedReader bin = new BufferedReader(sin);  
		        FileWriter out = new FileWriter("F:/User/zhushuangshuang/Desktop/new1.txt");  
		        BufferedWriter bout = new BufferedWriter(out);  
		        String s;  
		        while ((s = bin.readLine()).length() > 0) {  
		            bout.write(s, 0, s.length());  
		        }  
		  
		  
		} catch(IOException  IOe){        
           System.out.println("file write error!");  
		}  	
	}
}
二. 字符流Writer/Reader

1. Reader抽象类

    用于读取字符流的抽象类,子类必须实现的方法只有read(char[] ,int ,int)和close()。但是,多数子类将重写此定义的方法。

  代表类:FileReader:用来读取字符文件

2.

2. Writer抽象类

  写入字符流的抽象类。子类必须实现的方法仅有write(char[],int,int),flush()和close().
代表类:FileWriter:将字符类型写入文件,使用缺省字符编码和缓冲器大小。
FileWriter fw = new FileWriter(string filename);//创建字符输出流和已存在的文件关联
FileWriter fw = new FileWriter(string filename,boolean append); //append的参数为是否续写,即会在文件的结尾续写,不会覆盖已有的内容
void flush();//刷新缓冲,将缓冲区的字符数据保存到目的文件中
void close();//关闭流。关闭后写入会异常











 





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值