Java的基本输出流是java.io.OutputStream:public abstract class OutputStream
这个抽象类提供了一些基本方法:
public abstract void write(int b)throws IOException
public void write (byte[] data)throws IOException
public void write (byte[] data, int offset, int length)throws IOException
public void flush()throws IOException
public void close()throws IOException
值得注意的几点:
①刷新输出流是低成本的操作,应当在关闭流之前立即刷新输出所有流
②利用带资源的try构造(try with resource)try块参数表会自动调用close():
try (OutputStream out = new FileOutputStream("/tmp/data.txt")){
//处理输出流
}catch (IOException ex){
System.err.println(ex.getMessage());
}
OutputStream的子类为:
Java的基本输入流是java.io.InputStream:public abstract class InputStream
这个抽象类提供了一些基本方法:
public abstract int read()throws IOException
public int read (byte[] input)throws IOException
public int read (byte[] input, int offset, int length)throws IOException
public long skip(long n)throws IOException
public int available()throws IOException
public void close()throws IOException
值得注意的几点:
①执行以下两行代码,再也没有任何方法能访问底层的文件输入流:
InputStream in = new FileInputStream("data.txt");
in = new BufferedInputStream(in);
②PrintStream是有害的,网络程序员应当像躲避瘟疫一样避开他