首页 技术 正文
技术 2022年11月13日
0 收藏 613 点赞 3,688 浏览 2369 个字

InputStream
此抽象类是表示字节输入流的所有类的超类。需要定义 InputStream 的子类的应用程序必须始终提供返回下一个输入字节的方法。

int available()
返回此输入流方法的下一个调用方可以不受阻塞地从此输入流读取(或跳过)的字节数。
void close()
关闭此输入流并释放与该流关联的所有系统资源。
void mark(int readlimit)
在此输入流中标记当前的位置。
boolean markSupported()
测试此输入流是否支持 mark 和 reset 方法。
abstract int read()
从输入流读取下一个数据字节。
int read(byte[] b)
从输入流中读取一定数量的字节并将其存储在缓冲区数组 b 中。
int read(byte[] b, int off, int len)
将输入流中最多 len 个数据字节读入字节数组。
void reset()
将此流重新定位到对此输入流最后调用 mark 方法时的位置。
long skip(long n)
跳过和放弃此输入流中的 n 个数据字节。
OutputStream
此抽象类是表示输出字节流的所有类的超类。输出流接受输出字节并将这些字节发送到某个接收器。需要定义OutputStream 子类的应用程序必须始终提供至少一种可写入一个输出字节的方法。

void close()
关闭此输出流并释放与此流有关的所有系统资源。
void flush()
刷新此输出流并强制写出所有缓冲的输出字节。
void write(byte[] b)
将 b.length 个字节从指定的字节数组写入此输出流。
void write(byte[] b, int off, int len)
将指定字节数组中从偏移量 off 开始的 len 个字节写入此输出流。
abstract void write(int b)
将指定的字节写入此输出流。
进行I/O操作时可能会产生I/O例外,属于非运行时例外,应该在程序中处理。如:FileNotFoundException, EOFException, IOException等等,下面具体说明操作JAVA字节流的方法。

读文件:
本例以FileInputStream的read(buffer)方法,每次从源程序文件OpenFile.java中读取512个字节,存储在缓冲区buffer中,再将以buffer中的值构造的字符串new String(buffer)显示在屏幕上。程序如下(本例程序放在包biz.1cn.stream里面,另外请在根目录下建立TestFile.txt文件,以便正常运行):

     package biz.1cn.stream;
import java.io.FileInputStream;
import java.io.IOException;
/**
* @author chenrz(simon)
* @date 2006-6-29
* <p>
* JAVA字节流例子-读文件(www.1cn.biz)
* </p>
*/
public class ReadFile {
public static void main(String[] args) {
try {
// 创建文件输入流对象
FileInputStream is = new FileInputStream("TestFile.txt");
// 设定读取的字节数
int n = 512;
byte buffer[] = new byte[n];
// 读取输入流
while ((is.read(buffer, 0, n) != -1) && (n > 0)) {
System.out.print(new String(buffer));
}
System.out.println();
// 关闭输入流
is.close();
} catch (IOException ioe) {
System.out.println(ioe);
} catch (Exception e) {
System.out.println(e);
}
}
}

写文件:
本例用System.in.read(buffer)从键盘输入一行字符,存储在缓冲区buffer中,再以FileOutStream的write(buffer)方法,将buffer中内容写入文件WriteFile.txt中,程序如下(本例程序放在包biz.1cn.stream里面,另外运行后会在根目录下建立WriteFile.txt文件):

     package biz.1cn.stream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @author chenrz(simon)
* @date 2006-6-29
* <p>
* JAVA字节流例子-写文件(www.1cn.biz)
* </p>
*/
public class WriteFile {
public static void main(String[] args) {
try {
System.out.print("输入要保存文件的内容:");
int count, n = 512;
byte buffer[] = new byte[n];
// 读取标准输入流
count = System.in.read(buffer);
// 创建文件输出流对象
FileOutputStream os = new FileOutputStream("WriteFile.txt");
// 写入输出流
os.write(buffer, 0, count);
// 关闭输出流
os.close();
System.out.println("已保存到WriteFile.txt!");
} catch (IOException ioe) {
System.out.println(ioe);
} catch (Exception e) {
System.out.println(e);
}
}
}

转载自:http://ily0123456.iteye.com/blog/689568

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,494
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295