java需要关注的知识点---IO流

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.写文件:
[color=red][b]DataOutputStream:[/b][/color]

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]

[color=red]RandomAccessFile[/color][/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());
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值