Java 文件读写流
java 文件读写流
java.io 包含了用于文件读写的两个流类: fileinputstream 和 fileoutputstream。
1. fileinputstream 读文件流
fileinputstream 用于从文件读取数据,它的对象可以用关键字 new 来创建。
1)inputstream 构造方法
可以使用字符串类型的文件名来创建一个输入流对象来读取文件:
outputstream f = new fileoutputstream("c:/java/hello")
也可以使用一个文件对象来创建一个输入流对象来读取文件。我们首先得使用 file() 方法来创建一个文件对象:
file f = new file("c:/java/hello");
outputstream fout = new fileoutputstream(f);
2)inputstream 操作方法
创建了 inputstream 对象,就可以使用下面的方法来读取流或者进行其它操作。
| 序号 | 方法及描述 |
|---|---|
| 1 |
public void close() throws ioexception{} 关闭此文件输入流并释放与此流有关的所有系统资源。抛出ioexception异常。 |
| 2 |
protected void finalize()throws ioexception {} 这个方法清除与该文件的连接。确保在不再引用文件输入流时调用其 close 方法。抛出ioexception异常。 |
| 3 |
public int read(int r)throws ioexception{} 这个方法从 inputstream 对象读取指定字节的数据。返回为整数值。返回下一字节数据,如果已经到结尾则返回-1。 |
| 4 |
public int read(byte[] r) throws ioexception{} 这个方法从输入流读取r.length长度的字节。返回读取的字节数。如果是文件结尾则返回-1。 |
| 5 |
public int available() throws ioexception{} 返回下一次对此输入流调用的方法可以不受阻塞地从此输入流读取的字节数。返回一个整数值。 |
除了 inputstream 外,还有一些其他的输入流
2. fileoutputstream 写文件流
该类用来创建一个文件并向文件中写数据。如果该流在打开文件进行输出前,目标文件不存在,那么该流会创建该文件。
1)fileoutputstream 构造方法
有两个构造方法可以用来创建 fileoutputstream 对象。使用字符串类型的文件名来创建一个输出流对象:
outputstream f = new fileoutputstream("c:/java/hello")
也可以使用一个文件对象来创建一个输出流来写文件。我们首先得使用file()方法来创建一个文件对象:
file f = new file("c:/java/hello");
outputstream fout = new fileoutputstream(f);
2)outputstream 操作方法
创建 outputstream 对象完成后,就可以使用下面的方法来写入流或者进行其他的流操作。
| 序号 | 方法及描述 |
|---|---|
| 1 |
public void close() throws ioexception{} 关闭此文件输入流并释放与此流有关的所有系统资源。抛出ioexception异常。 |
| 2 |
protected void finalize()throws ioexception {} 这个方法清除与该文件的连接。确保在不再引用文件输入流时调用其 close 方法。抛出ioexception异常。 |
| 3 |
public void write(int w)throws ioexception{} 这个方法把指定的字节写到输出流中。 |
| 4 |
public void write(byte[] w) 把指定数组中w.length长度的字节写到outputstream中。 |
除了outputstream外,还有一些其他的输出流,更多的细节参考下面链接:
3. 文件读写范例
下面是一个演示 inputstream 和 outputstream 用法的例子:
import java.io.*;
public class filestreamtest {
public static void main(string[] args) {
try {
byte bwrite[] = { 11, 21, 3, 40, 5 };
outputstream os = new fileoutputstream("test.txt");
for (int x = 0; x < bwrite.length; x++) {
os.write(bwrite[x]); // writes the bytes
}
os.close();
inputstream is = new fileinputstream("test.txt");
int size = is.available();
for (int i = 0; i < size; i++) {
system.out.print((char) is.read() + " ");
}
is.close();
} catch (ioexception e) {
system.out.print("exception");
}
}
}
上面的程序首先创建文件test.txt,并把给定的数字以二进制形式写进该文件,同时输出到控制台上。
以上代码由于是二进制写入,可能存在乱码,你可以使用以下代码范例来解决乱码问题:
//文件名 :filestreamtest2.java
import java.io.*;
public class filestreamtest2 {
public static void main(string[] args) throws ioexception {
file f = new file("a.txt");
fileoutputstream fop = new fileoutputstream(f);
// 构建fileoutputstream对象,文件不存在会自动新建
outputstreamwriter writer = new outputstreamwriter(fop, "utf-8");
// 构建outputstreamwriter对象,参数可以指定编码,默认为操作系统默认编码,windows上是gbk
writer.append("中文输入");
// 写入到缓冲区
writer.append("\r\n");
// 换行
writer.append("english");
// 刷新缓存冲,写入到文件,如果下面已经没有写入的内容了,直接close也会写入
writer.close();
// 关闭写入流,同时会把缓冲区内容写入文件,所以上面的注释掉
fop.close();
// 关闭输出流,释放系统资源
fileinputstream fip = new fileinputstream(f);
// 构建fileinputstream对象
inputstreamreader reader = new inputstreamreader(fip, "utf-8");
// 构建inputstreamreader对象,编码与写入相同
stringbuffer sb = new stringbuffer();
while (reader.ready()) {
sb.append((char) reader.read());
// 转成char加到stringbuffer对象中
}
system.out.println(sb.tostring());
reader.close();
// 关闭读取流
fip.close();
// 关闭输入流,释放系统资源
}
}


