1.无论何时使用readLine().都不应该使用DataInputStream,而应该使用BufferedReader.
2.RandomAccessFile,只有RandomAccessFile支持搜索操作,并且只适用于文件。
到jdk1.4中,它的大部分功能被nio存储映射文件所取代。
3.读文件的几种方式:
缓冲输入文件
public class BufferedInputFile {
public static String read(String file) throws IOException{
StringBuffer sb = new StringBuffer();
FileReader fileReader = new FileReader(file);
BufferedReader br = new BufferedReader(fileReader);
String s = null;
while((s = br.readLine()) != null) {
sb.append(s + "\n");
}
return sb.toString();
}
public static void main(String[] args) {
try {
System.out.println(read("D:\\RSA75WP\\test\\src\\com\\io\\BufferedInputFile.java"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
从内存中输入:
public class MemoryInput {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
StringReader in = new StringReader(BufferedInputFile
.read("D:\\RSA75WP\\test\\src\\com\\io\\MemoryInput.java"));
int c;
while((c = in.read()) != -1) {
System.out.print((char)c);
}
}
}
格式化的内存输入:
public class FormattedMemoryInput {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
DataInputStream dis = new DataInputStream(
new ByteArrayInputStream(
(BufferedInputFile
.read("D:\\RSA75WP\\test\\src\\com\\io\\FormattedMemoryInput.java")
.getBytes())));
try {
while(true) {
System.out.print((char)dis.readByte());
}
} catch (Exception e) {
System.out.println("End of stream");
}
}
}
使用available的工作方式会随着所读取的媒介类型不同而有所不同,需要小心使用:
public class TestEof {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
DataInputStream in = new DataInputStream(new BufferedInputStream(
new FileInputStream(
"D:\\RSA75WP\\test\\src\\com\\io\\TestEof.java")));
while (in.available() != 0) {
System.out.println((char)in.readByte());
}
}
}
4.写文件:
DataOutputStream:
public class DateOutputStreamTest {
public static void main(String[] args) throws IOException {
DataOutputStream dops = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("Data.txt")));
dops.writeDouble(3.32);
dops.writeUTF("test outputStream");
dops.writeInt(4);
dops.close();
DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream("Data.txt")));
System.out.println(dis.readDouble());
System.out.println(dis.readUTF());
System.out.println(dis.readInt());
}
}
[b]
RandomAccessFile[/b]
public class UsingRandomAccessFile {
static String fileNme = "rtest.dat";
static void display() throws IOException{
RandomAccessFile raf = new RandomAccessFile(fileNme, "r");
for ( int i = 0; i< 7 ;i++) {
System.out.println("Value:" + i + ":" + raf.readDouble());
}
System.out.println(raf.readUTF());
raf.close();
}
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
RandomAccessFile rf = new RandomAccessFile(fileNme,"rw");
for(int i = 0; i < 7; i++)
rf.writeDouble(i*1.414);
rf.writeUTF("this end of the file");
rf.close();
display();
rf = new RandomAccessFile(fileNme,"rw");
rf.seek(5*8);
rf.writeDouble(47.0001);
rf.close();
display();
}
}
读取二进制文件:
public class BinaryFile {
public static byte[] read(File bFile) throws IOException {
BufferedInputStream bf = new BufferedInputStream(new FileInputStream(bFile));
try {
byte[] data = new byte[bf.available()];
bf.read(data);
return data;
}finally{
bf.close();
}
}
public static byte[] read(String fileName) throws IOException{
return read(new File(fileName).getAbsoluteFile());
}
}