这节课的内容:
理解流概念
InputStream与OutputStream类
FileInputStream 与FileOutputStream类
Reader与Writer类
PipedInputStream与PipedOutputStream类
流是字节序列的抽象概念。流是指数据传输时的形态。分为节点流和过滤流。
InputStream类
InputStream类的方法
int read() //从输入流中读取一个字节的内容,以int形式返回,如果流结束就返回-1,如果没结束,却又没数据,就会阻塞程序。
int read(byte[] b)//返回实际读取的个数。当一个也读不到的时候程序也会阻塞 。
int read(byte[] b,int off,int len) 返回实际读入的字节数,可以读len长度个。
long skip(long n) 跳过N字节,返回实际跳过的数据数。用于包装类
int available()返回当前输入流中可读字节数。
void mark(int readlimit) 在输入流中建立一个标记,从标记位置开始最多还能读取多少内容。
void reset() 和mark方法配合使用。
boolean markSupported()返回当前流对象是否支持mark和reset操作
void close() 关闭流对象,释放相关资源。
OutputStream类
程序可以向其中连续写入字节的对象叫输出流,在JAVA中用OutputStream类来描述所有输出流的抽象概念。
void write(int b) 写入最低字节内容,最高字节内容扔掉。
void write(byte[]b) 将字节数组b的内容全部写到输出流中
void write(byte[]b,int off,int len)
void flush()清空输出所有内容
void close()关闭输出流对象
FileInputStream与FileOutputStream类
它们分别用来创建磁盘文件输入流和输出流对象
创建FileInputStream实例时,指定的文件应当是存在且可读的,创建FileOutputStream实例时,文件如果已经存在,原内容则将被覆盖。
对同一个磁盘文件创建FileInputStream对象有两种方式。
(1)FileInputStream inOne=new FileInputStream("hello.test");
(2)File f=new File("hello.test");
FileInputStream inTwo=new FileInputStream(f);
创建FileOutputStream实例时,可以指定不存在的文件名,但不能指定已被其它程序打开了的文件。
编程举例:用FileOutputStream类向文件中写一个字符串,然后用FileInputStream读出内容。
import java.io.*;
public class FileStream {
public static void main(String[] args) throws Exception
{
FileOutputStream out=new FileOutputStream("hello.txt");
out.write("www.it315.org".getBytes());
out.close();
File f=new File("hello.txt");
FileInputStream in=new FileInputStream(f);
byte[] buf=new byte[1024];
int len=in.read(buf);
System.out.println(new String(buf,0,len));
}
}
//===========================================
Reader与Writer类
Reader和Writer是对所有字符流类的抽象基类。用于简化字符串的输入输出编程。
编程举例:用FileWriter类向文件中写入一个串字符,再用FileReader读出写入的内容。
import java.io.*;
public class FileStream2
{
/**
* Method main
*
*
* @param args
*
*/
public static void main(String[] args) throws Exception
{
// TODO: 在这添加你的代码
FileWriter out=new FileWriter("hello2.txt");
out.write("www.it315.org");
out.close(); //关闭的时候才会将内容写入到文件。
char[] buf=new char[1024];
FileReader in=new FileReader("hello2.txt");
int len=in.read(buf);
System.out.println(new String(buf,0,len));
in.close();
}
}
//====================================
PipedInputStream类与PipedOutputStream类
用于在应用程序中创建管道通信。主要用于线程之间的通信。
编程实例:
Sender.java
import java.io.*;
public class Sender extends Thread {
private PipedOutputStream out=new PipedOutputStream();
public PipedOutputStream getOutputStream()
{
return out;
}
public void run()
{
String strInfo=new String("hello,receiver!");
try{
out.write(strInfo.getBytes());
out.close();
}catch(Exception e)
{
e.printStackTrace();
}
}
}
//=====================
Receiver.java
import java.io.*;
public class Receiver extends Thread {
private PipedInputStream in=new PipedInputStream();
public PipedInputStream getInputStream()
{
return in;
}
public void run()
{
byte[] buf=new byte[1024];
try{
int len=in.read(buf);
System.out.println("the following message comes form sender:/n"+
new String(buf,0,len));
in.close();
}catch(Exception e)
{
e.printStackTrace();
}
}
}
//=======================
PipedStreamTest.java
import java.io.*;
public class PipedStreamTest {
/**
* Method main
*
*
* @param args
*
*/
public static void main(String[] args) throws Exception {
// TODO: 在这添加你的代码
Sender t1=new Sender();
Receiver t2=new Receiver();
PipedOutputStream out=t1.getOutputStream();
PipedInputStream in=t2.getInputStream();
out.connect(in);
t1.start();
t2.start();
}
}
ByteArrayInputStream与ByteArrayOutputStream类
这两个类用于IO流的方式来完成对字节数组内容的读写,从而实现内存虚拟文件或者内存映像文件的功能。
编程举例,把输入流中的英文字母变成大写字母,再写到输出流中。
import java.io.*;
public class ByteArrayTest {
/**
* Method main
*
*
* @param args
*
*/
public static void main(String[] args) {
// TODO: 在这添加你的代码
String tmp="abcdefghijklmnopqrstuvwxyz";
byte[] src=tmp.getBytes();
ByteArrayInputStream input=new ByteArrayInputStream(src);
ByteArrayOutputStream output=new ByteArrayOutputStream();
transform(input,output);
byte[] result=output.toByteArray();
System.out.println(new String(result));
}
public static void transform(InputStream in,OutputStream out)
{
int ch=0;
try{
while((ch=in.read())!=-1)
{
int upperCh=(int)Character.toUpperCase((char)ch);
out.write(upperCh);
}
}catch(Exception e) {
e.printStackTrace();
}
}
}
重视IO程序代码的复用
System.in 连接到键盘,是InputStream类型的实例对象。System.out连接到显示器,是PrintStream类的实例对象。
不管各种底层物理设备用什么方式实现数据终止点,InputStream的read方法总是返回-1表示输入流结束。
本文详细介绍了Java中的IO流概念及其实现方式,包括InputStream与OutputStream的基本使用方法,FileInputStream与FileOutputStream的操作磁盘文件,以及Reader与Writer类的字符流处理。通过多个编程示例展示了如何进行文件读写及线程间通信。

1万+

被折叠的 条评论
为什么被折叠?



