<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    我學JAVA

    Java的IO的一個實例

    import java.io.*;
    import java.util.zip.*;
    public class JavaIODemo{
     String temp=new String();
     String t=new String();
     public void readByLineFromConsoleAndPrint(){
      System.out.println("==從控制臺獲得輸入==");
      try{
       BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
       System.out.println("請輸入一行字符:");
       System.out.println(temp=in.readLine());
      }
      catch(IOException e){
       e.printStackTrace();
      }
     }
     public void readByLineFromFileAndPrint(){
      System.out.println("==從文件獲得輸入==");
      try{
       BufferedReader in=new BufferedReader(new FileReader("demo.out"));
       temp="";
       String s=new String();
       while((s=in.readLine())!=null)
          temp+=s+"\n";
          in.close();
          System.out.println(temp);
         }
         catch(FileNotFoundException e){
         e.printStackTrace();
         }
         catch(IOException e){
         e.printStackTrace();
         }
     }
     public void readFromMemoryAndPrint(){
      System.out.println("==從內存獲得輸入==");
      try{
       StringReader stringReader=new StringReader(temp);
       int t;
       while((t=stringReader.read())!=-1)
       System.out.println((char)t);
      }
      catch(IOException e){
       e.printStackTrace();
      }
     }
     public void writeToFile()
     {
      System.out.println("==文件輸出流==");
      try{
       BufferedReader in=new BufferedReader(new StringReader(temp));
       PrintWriter out=new PrintWriter(new BufferedWriter(new FileWriter("demo.out")));
       while((t=in.readLine())!=null)
       out.println(t);
       out.close();
      }
      catch(IOException e){
       e.printStackTrace();
      }
     }
     public void randomAccessFile(){
      try{
       System.out.println("==隨機訪問文件==");
       RandomAccessFile file=new RandomAccessFile("rand.out","rw");
       for(int i=0;i<10;i++)
       file.writeDouble(i*1.12);
       file.seek(100);
       file.writeDouble(577.4545);
       file.close();
       file=new RandomAccessFile("rand.out","r");
       for(int i=0;i<10;i++)
       System.out.println("value"+i+"="+file.readDouble());
       file.seek(100);
       System.out.println(file.readDouble());
       file.close();
      }
      catch(IOException e){
       e.printStackTrace();
      }
     }
     public void formattedInputAndOutput()
     {
      System.out.println("==格式化數據輸出==");
      try{
       DataOutputStream out=new DataOutputStream(new BufferedOutputStream(new FileOutputStream("data.data")));
       out.writeDouble(54.7769);
       out.writeBytes("this is bytes\n");
       out.writeChars("this is chars\n");
       out.writeFloat(100.6f);
       out.writeUTF("this is utf encoding string\n");
       out.close();
       
       System.out.println("==格式化數據輸入==");
       DataInputStream in=new DataInputStream(new BufferedInputStream(new FileInputStream("data.data")));
       BufferedReader reader=new BufferedReader(new InputStreamReader(in));
       System.out.println(in.readDouble());
       System.out.println(in.readLine());
       System.out.println(in.readLine());
       System.out.println(in.readFloat());
       System.out.println(in.readLine());
      }
      catch(EOFException e){
       System.out.println("end of stream");
      }
      catch(IOException e){
       e.printStackTrace();
      }
     }
     /*public void readAndWriteSerialObject(){
      System.out.println("==讀寫序列化對象==");
      Test test=new Test("init value");
      try{
       ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream("test.ser"));
       out.writeObject("保存序列化對象");
       out.writeObject(test);
       out.close();
       ObjectInputStream in=new ObjectInputStream(new FileInputStream("test.ser"));
       System.out.println(in.readObject());
       in.close();
       System.out.println(t.getValue());
      }
      catch(IOException e){
       e.printStackTrace();
      }
      catch(ClassNotFoundException e){
       e.printStackTrace();
      }
     }*/
     public void zipFile(){
      System.out.println("==使用java壓縮文件==");
      try{
       FileOutputStream file=new FileOutputStream("demo.zip");
       CheckedOutputStream csum=new CheckedOutputStream(file,new Adler32());
       ZipOutputStream zip=new ZipOutputStream(new BufferedOutputStream(csum));
       zip.setComment("Zip輸出流測試");
       BufferedReader reader=new BufferedReader(new FileReader("Test.java"));
       zip.putNextEntry(new ZipEntry("Test.java"));
       int c;
       while((c=reader.read())!=-1)
          zip.write(c);
       zip.close();
      }
      catch(IOException e){
       e.printStackTrace();
      }
     }
     public static void main(String []args){
      JavaIODemo demo=new JavaIODemo();
      demo.readByLineFromConsoleAndPrint();
      demo.readFromMemoryAndPrint();
      demo.writeToFile();
      demo.formattedInputAndOutput();
      demo.randomAccessFile();
      demo.readByLineFromFileAndPrint();
      file://demo.readAndWriteSerialObject();
      demo.zipFile();
     }
    }

    posted on 2005-01-18 15:11 我走我路 閱讀(706) 評論(1)  編輯  收藏

    評論

    # re: Java的IO的一個實例 2008-08-09 02:36 竇紅亮

    好,努力繼續。  回復  更多評論   


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    <2008年8月>
    272829303112
    3456789
    10111213141516
    17181920212223
    24252627282930
    31123456

    導航

    統計

    公告

    正在學習JAVA技術中。。。。。

    常用鏈接

    留言簿

    隨筆檔案

    文章檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲成AV人片在线观看WWW| 四虎影视在线永久免费看黄| 亚洲欭美日韩颜射在线二| 国产亚洲高清在线精品不卡| 午夜一区二区免费视频| 亚洲欧洲无卡二区视頻| 久久久久国产精品免费免费搜索| 久久综合亚洲色HEZYO社区| 91精品手机国产免费| 亚洲激情电影在线| 日韩中文字幕精品免费一区| 亚洲国产日韩综合久久精品| 成熟女人特级毛片www免费| 亚洲精品无码久久久久久| 日韩精品免费电影| 男女猛烈激情xx00免费视频| 国产亚洲av人片在线观看| 国产色无码精品视频免费| 久久久无码精品亚洲日韩蜜臀浪潮 | 亚洲一区爱区精品无码| 最近中文字幕大全免费版在线| 亚洲AV无码国产在丝袜线观看| 免费无码一区二区三区| 亚洲国产电影在线观看| 免费看片免费播放| 一区二区三区免费视频播放器| 亚洲成AV人片在线播放无码| 亚洲免费观看在线视频| 亚洲AV永久无码精品一福利| 亚洲成av人在片观看| 免费在线看黄网站| 亚洲一区二区三区免费观看 | 成人一级免费视频| 亚洲A∨无码无在线观看| 国产1024精品视频专区免费| 免费福利资源站在线视频| 亚洲成AV人片在线观看| 国内一级一级毛片a免费| 三级毛片在线免费观看| 亚洲国产福利精品一区二区| 亚洲国产综合精品中文字幕|