动态网站制作指南 [  QQ表情  ]
[ 投票调查 ]
[ 企业邮箱 ]
[ 网站空间 ]
网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
ASP源码 | .Net源码 | PHP源码 | JSP源码 | JAVA源码 | CGI源码 | VB源码 | C++源码 | Delphi源码 | PB源码 | VF源码 | 汇编 | 服务器
电脑书籍下载:程序设计书籍 | 数据库教程书籍 | 平面与多媒体书籍 | 网络通讯书籍 | 系统管理书籍 | 网络安全书籍 | 认证考试书籍
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > JSP技巧
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
文章搜索服务
邮件订阅
输入你的邮件地址,
你将不会错过任何关于:
[ JSP技巧 ]的信息



本月文章推荐
.快速应用JDBC控件访问数据库资源.
.通过Jsp发送动态图像.
.攻破JNDI连接池(Tomcat5.5下通过.
.JSP中文乱码问题解决方法小结.
.从数据库读出的JPG文件的字符流,.
.JSP连接mysql数据库攻略.
.如何利用JSP的9种基本内置组件.
.修改Tomcat的主配置文件,增加一个.
.JSP应用语法详解大全 (1).
.实例讲解JSP调用SQL Server的存储.
.JSP通过JDBC与Oracle相连.
.建立JSP操作以提高数据库访问的效.
.jsp中文问题解决方案(完整版).
.jsp中forword和sendRedirect的区.
.tomcat 5.09 中文问题解决全攻略.
.实现session登陆时间的验证,验证.
.JSP中tomcat的SQL Server2000数据.
.如何用Jsp读取Mysql数据库.
.Jdbc连Sybase数据库的几种方法.
.在 Linux 上安装Apache+ApacheJS.

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

发表日期:2001-2-16 |


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();
     }
}
  
}    
上一篇:怎样设置 JSP 的虚拟目录 人气:18534
下一篇:javamail在jsp中调用 人气:13449
浏览全部的内容 Dreamweaver插件下载 常用网页广告代码全集
  最新网站源码 最新软件下载
2008-7-19 Roclog v3.1.4 SP1
2008-7-19 马克斯CMS(Maxcms) v1.1 Build 0
2008-7-19 乐学LBBS无限级目录图片直读系统
2008-7-19 PHP在线考试系统PPExam v1.2.5
2008-7-19 APJE私服发布系统 v2.0
2008-7-19 寻觅图书管理系统 v1.12
2008-7-19 寻觅学校管理系统
2008-7-19 义乌网站目录baidu新闻订阅 v1.0
2008-7-19 牛仔IIS日志蜘蛛爬行记录分析器
2008-7-19 UltraEdit 简体中文增强版 14.10
2008-7-19 CentOS 5.2 i386 LiveCD
2008-7-19 Snapture多功能相机 v1.4
2008-7-19 iAcces中文输入法 v1.0Build016
2008-7-19 Cookbook烹饪秘籍 v2.5
2008-7-19 苹果专用DVD转换工具 v1.1.59汉化
2008-7-19 Modem修复软件ZiPhone修改版04.0
2008-7-19 AgileMessenger即时通讯工具美化
2008-7-19 Sketches画图软件 v0.7b6破解版


  发表评论
姓 名: 验证码:
内 容:
[ 汉字翻译拼音 ] [ 广告代码 ] [ 符号对照表 ] [ 进制转换 ] [ 经典小工具 ] [ 个税计算 ] [ 汉字简繁转换 ] [ 普通单位换算 ] [ 公制单位换算 ]
[ 生辰老黄历 ] [ 国内电话区号 ] [ 国家代码与域名缩写 ] [ 文字加密解密 ] [ 健康查询 ] [ 万年历 ] [ 手机号码查询 ] [ ip搜索 ] [ Google PR查询 ]
业务联系 | 广告刊登 | 频道合作 | 投稿荐稿 | 联系方式 | 加入收藏 | RSS订阅
Copyright © 2000-2008 www.knowsky.com All rights reserved | 网络实名:动态网站制作指南 | 沪ICP备05001343号
ホームページ制作 不動産検索システム 求人情報
防水工事·改修工事 フットサル大会 探偵