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

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

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

    我學(xué)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("==從內(nèi)存獲得輸入==");
      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("==格式化數(shù)據(jù)輸出==");
      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("==格式化數(shù)據(jù)輸入==");
       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 竇紅亮

    好,努力繼續(xù)。  回復(fù)  更多評論   


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


    網(wǎng)站導(dǎo)航:
     
    <2025年7月>
    293012345
    6789101112
    13141516171819
    20212223242526
    272829303112
    3456789

    導(dǎo)航

    統(tǒng)計

    公告

    正在學(xué)習JAVA技術(shù)中。。。。。

    常用鏈接

    留言簿

    隨筆檔案

    文章檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 粉色视频成年免费人15次| 亚洲欧洲国产成人综合在线观看 | 亚洲色图在线播放| 亚洲永久无码3D动漫一区| 亚洲精品成人网久久久久久| 国产精品久久久久影院免费| 国产精品自在自线免费观看| 国产成人一区二区三区免费视频| 成人永久免费福利视频网站| 国产大片51精品免费观看| 国产高清视频在线免费观看| 国产一级淫片视频免费看| 亚洲黄色网址在线观看| 四虎在线视频免费观看视频| 午夜亚洲乱码伦小说区69堂| 亚洲av日韩精品久久久久久a| 成a人片亚洲日本久久| 国产成人亚洲精品无码AV大片| 污视频网站在线免费看| 无码免费又爽又高潮喷水的视频 | 亚洲娇小性xxxx色| 亚洲自偷自偷在线成人网站传媒| 亚洲欧美日韩自偷自拍| 特级毛片免费观看视频| 国产精品九九久久免费视频| 国产精品无码永久免费888| 国产无遮挡无码视频免费软件| 99re这里有免费视频精品| 在线观看av永久免费| 国产精品色午夜免费视频| 永久亚洲成a人片777777| 婷婷精品国产亚洲AV麻豆不片| 亚洲理论精品午夜电影| 亚洲人成无码网站在线观看| 四虎国产精品永免费| 暖暖日本免费中文字幕| WWW免费视频在线观看播放| 无码中文字幕av免费放dvd| 97视频免费在线| 国产一级特黄高清免费大片| 亚洲爆乳精品无码一区二区三区|