InputStream的方法:
三个read方法
int read():读取一个字节,以整数的形式返回,如返回-1,则表示以到输入流的末尾。
int read(byte[]):读取一系列字节,返回读取的字节数,如返回-1,则表示以到输入流的末尾。
int read(byte[], int, int):读取一系列字节,返回读取的字节数,如返回-1,则表示以到输入流的末尾。Int,int用于定义读取字节数组的范围。
Reader的方法:
三个read方法
int read():读取一个字节,以整数的形式返回,如返回-1,则表示以到输入流的末尾。
int read(byte[]):读取一系列字节,返回读取的字节数,如返回-1,则表示以到输入流的末尾。
int read(byte[], int, int):读取一系列字节,返回读取的字节数,如返回-1,则表示以到输入流的末尾。Int,int用于定义读取字节数组的范围。
代码示例:如何使用随机文件读写类来读写文件内容
RW表示文件时可读写的
读:
try{
RandomAccessFile f = new RandomAccessFile("test.txt", "rw");
long len = 0L;
long allLen = f.length();
int i = 0;
while (len < allLen) {
String s = f.readLine();
if (i > 0) {
col.add(s);
}
i++;
//游标
len = f.getFilePointer();
}
}catch(Exception err){
err.printStackTrace();
}
写:
try{
RandomAccessFile f = new RandomAccessFile("test.txt", "rw");
StringBuffer buffer = new StringBuffer("\n");
Iterator it = col.iterator();
while (it.hasNext()) {
buffer.append(it.next() + "\n");
}
f.writeUTF(buffer.toString());
}catch(Exception err){
err.printStackTrace();
}
代码示例:如何使用流的基本接口来读写文件内容
import java.io.*;
public class Test{
public static void main(String args[]){
String currentLine;
try{
DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream("Test.java")));