我学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 我走我路 阅读(615) 评论(1)  编辑  收藏

评论

# re: Java的IO的一个实例 2008-08-09 02:36 窦红亮

好,努力继续。  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 
<2008年8月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
31123456

导航

统计

公告

正在学习JAVA技术中。。。。。

常用链接

留言簿

随笔档案

文章档案

搜索

最新评论

阅读排行榜

评论排行榜