milan ID:milan25429688
13075次访问,排名8627(5)好友0人,关注者0
milan25429688的文章
原创 14 篇
翻译 0 篇
转载 1 篇
评论 2 篇
最近评论
alan:感觉有点复杂化
文章分类
收藏
    相册
    存档
    软件项目交易
    订阅我的博客
    XML聚合  FeedSky
    订阅到鲜果
    订阅到Google
    订阅到抓虾
    订阅到BlogLines
    订阅到Yahoo
    订阅到GouGou
    订阅到飞鸽
    订阅到Rojo
    订阅到newsgator
    订阅到netvibes

    原创 FileTest收藏

    新一篇: BeanTest | 

    这是我第一个文件File 相关类的测试例子,分为两部分:MyFirstIOFile 和MyFirstRAFile

    1。MyFirstIOFile

    import java.io.*;

    class MyFirstIOFile {
      public static void main(String[] args){
        char ch = ' ';
        System.out.println("plz input some characters.");     
        try{
          FileOutputStream my_outfile = new FileOutputStream("..\\myfile.txt");
          while( (ch=(char)System.in.read()) != '#' ){
            my_outfile.write(ch);
          }
          my_outfile.close();
        }catch(IOException e){};

        System.out.println("show the file characters:");
        try{
          FileInputStream my_infile = new FileInputStream("..\\myfile.txt");
          //byte[] k = {}; 错误:这种所谓动态分配无法读取文件中的字节。
          byte[] k = new byte[20];
          my_infile.read(k);
          System.out.write(k);
          my_infile.close();
        }catch(FileNotFoundException e){}
        catch(IOException e){};
      }
     
    }

    2。MyFirstRAFile

    //由于 RandomAccessFile(String/File, String mode)的第二个参数有两种情况:rw(写) 和r(读)
    //所以在RandomAccessFile 对象进行写入或者读取的时候就要区分开来,必须要定义两个不同的对象!
    //下面的my_rafile 和my_rafile_r 才能分别准确使用RandomAccessFile 对象自带的写入和读取的方法。

    import java.io.*;
    //import java.lang.*;
    //import java.util.*;

    class MyFirstRAFile{
      public static void main(String[] args){
        //String test = new String("Longtest");
        //long ml = Long.parseLong("ewt");
        //Long my_Long = new Long(test);
        //long my_long = my_Long.longValue();
        //String ms = my_Long.toString();
        //System.out.println(my_Long);
        RandomAccessFile my_rafile = null;
        try{
        my_rafile = new RandomAccessFile("..\\myfile.txt","rw");
        for(int i=0;i<10;i++)
          my_rafile.writeBytes("RandomAccessFile Test Line:" + i + "\r\n");
        //my_rafile.close();
        }catch(FileNotFoundException e){}
        catch(IOException e){};
       
        try{
        //my_rafile = new RandomAccessFile("..\\myfile.txt","rw");
        my_rafile.seek(my_rafile.length());
        my_rafile.writeBytes("append line \r\n");
        my_rafile.close();
        }catch(FileNotFoundException e){}
        catch(IOException e){};

        try{
        RandomAccessFile my_rafile_r = new RandomAccessFile("..\\myfile.txt","r");
        String record = "";
        int i = 0;
        while((record = my_rafile_r.readLine()) != null){
          i++;
          System.out.println("Value "+ i + ":" + record);
        }
        my_rafile_r.close();
        }catch(IOException e){};
           

      }//main
    }//class

    发表于 @ 2005年02月02日 12:03:00|评论(loading...)|编辑

    新一篇: BeanTest | 

    评论:没有评论。

    发表评论  


    当前用户设置只有注册用户才能发表评论。如果你没有登录,请点击登录
    Csdn Blog version 3.1a
    Copyright © milan25429688