动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
  
   Jsp → 阅读文章

 Java中对文件的读写操作之比较

作者来源: 
阅读 2130 人次 , 2006-3-29 4:23:00 


java 中对文件的读写操作之比较

作者:jeru liu
日期:november 29,2000
版本:1.0

纪念在chinaasp积分过一百呕心原创一篇(java 中对文件的读写操作之比较)拿分好难呀,555~~~,不知道那些几千分的老妖们是怎么灌水的。

java 对文件进行读写操作的例子很多,让初学者感到十分困惑,我觉得有必要将各种方法进行
一次分析,归类,理清不同方法之间的异同点。

一.在 jdk 1.0 中,通常是用 inputstream & outputstream 这两个基类来进行读写操作的。
inputstream 中的 fileinputstream 类似一个文件句柄,通过它来对文件进行操作,类似的,在
outputstream 中我们有 fileoutputstream 这个对象。

用fileinputstream 来读取数据的常用方法是:
fileinputstream fstream = new fileinputstream(args[0]);
datainputstream in = new datainputstream(fstream);
用 in.readline() 来得到数据,然后用 in.close() 关闭输入流。
完整代码见 example 1。

用fileoutputstream 来写入数据的常用方法是:
fileoutputstream out out = new fileoutputstream("myfile.txt");
printstream p = new printstream( out );
用 p.println() 来写入数据,然后用 p.close() 关闭输入。
完整代码见 example 2。


二.在 jdk 1.1中,支持两个新的对象 reader & writer, 它们只能用来对文本文件进行操作,而
jdk1.1中的 inputstream & outputstream 可以对文本文件或二进制文件进行操作。

用filereader 来读取文件的常用方法是:
filereader fr = new filereader("mydata.txt");
bufferedreader br = new bufferedreader(fr);
用 br.readling() 来读出数据,然后用br.close() 关闭缓存,用fr.close() 关闭文件。
完整代码见 example 3。

用 filewriter 来写入文件的常用方法是:
filewriter fw = new filewriter("mydata.txt");
printwriter out = new printwriter(fw);
在用out.print 或 out.println 来往文件中写入数据,out.print 和 out.println的唯一区别是后者写
入数据或会自动开一新行。写完后要记得 用out.close() 关闭输出,用fw.close() 关闭文件。  
完整代码见 example 4。

-------------------------------------------------------------- following is the source code of examples------------------------------------------------------

example 1:
// fileinputdemo
// demonstrates fileinputstream and datainputstream
import java.io.*;

class fileinputdemo {
public static void main(string args[]) {
// args.length is equivalent to argc in c
if (args.length == 1) {
try {
// open the file that is the first command line parameter
fileinputstream fstream = new fileinputstream(args[0]);
// convert our input stream to a datainputstream
datainputstream in = new datainputstream(fstream);
// continue to read lines while there are still some left to read
while (in.available() !=0) {
// print file line to screen
system.out.println (in.readline());
}
in.close();
} catch (exception e) {
system.err.println("file input error");
}
}
else
system.out.println("invalid parameters");
}
}

example 2:
// fileoutputdemo
// demonstration of fileoutputstream and printstream classes
import java.io.*;

class fileoutputdemo
{
public static void main(string args[]) {
fileoutputstream out; // declare a file output object
printstream p; // declare a print stream object

try {
// connected to "myfile.txt"
out = new fileoutputstream("myfile.txt");
// connect print stream to the output stream
p = new printstream( out );
p.println ("this is written to a file");
p.close();
} catch (exception e) {
system.err.println ("error writing to file");
}
}
}

example 3:
// filereadtest.java
// user filereader in jdk1.1 to read a file
import java.io.*;

class filereadtest {
public static void main (string[] args) {
filereadtest t = new filereadtest();
t.readmyfile();
}

void readmyfile() {
string record = null;
int reccount = 0;
try {
filereader fr = new filereader("mydata.txt");
 bufferedreader br = new bufferedreader(fr);
 record = new string();
 while ((record = br.readline()) != null) {
 reccount++;
 system.out.println(reccount + ": " + record);
}
br.close();
fr.close();
 } catch (ioexception e) {
 system.out.println("uh oh, got an ioexception error!");
 e.printstacktrace();
 }
}

}

example 4:
// filewritetest.java
// user filewriter in jdk1.1 to writer a file
import java.io.*;

class filewritetest {
public static void main (string[] args) {
filewritetest t = new filewritetest();
t.writemyfile();
}

void writemyfile() {
try {
filewriter fw = new filewriter("mydata.txt");
printwriter out = new printwriter(fw);
out.print(“hi,this will be wirte into the file!”);  
out.close();
fw.close();
 } catch (ioexception e) {
 system.out.println("uh oh, got an ioexception error!");
 e.printstacktrace();
 }
}

}

 
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:javamail在jsp中调用
· 下一篇:怎样设置JSP的虚拟目录
· apache+resin
· JSP由浅入深(1)
· JSP/Servlet构建三层管理信息系统
· JSP及语法概要
· 建立JSP操作以提高数据库访问的效率


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077