随笔-124  评论-49  文章-56  trackbacks-0

1 输入/输出流
           字节流      字符流
 输入流  InputStream    Reader
 输出流  OutputStream   Writer
2 字节流和处理流
------------------------------------------------------------------------
3 InputStream 向程序中输入数据
InputStream---FileInputStream
InputStream---StringBufferInputStream
InputStream---ObjectInputStream
基本方法
-读取一个字节并以整数的形式返回
-如果返回-1已到输入流的末尾
int read() throws IOException
-读取一系列字节并存储到一个数组buffer
int read(byte[] buffer) throws IOException
-读取length个字节并存到一个字节数组buffer
int read(byte[] buffer,int offset[数组的那个下标开始存],int lenght) throws IOException
-关闭流
void close() throws IOException
-------------------------------------------------------------------------------
4 outputStream 输出数据
OutputStream---FileOutputStream
OutputStream---ObjectOutputStream
-向输出流中写一个字节数据
void write(int b)throws IOException
-将一个字节类型的数组中的数据写入输出流
void write(byte[] b)throws IOException
-将一个字节类型的数组中的从指定位置off开始的len个字节写入到输出流
void write(byte[] b,int off,int len)throws IOException
-关闭流
void close()throws IOException
-将输出流中缓冲的数据全部写出到目的地(重要:先flush再close)
void flush() throws IOException
-----------------------------------------------------------------------------------
5 Reader/Writer
---------------------------------------------------
6 FileInputStream/OutputStream
long num=0;
try{
 FileInputStream in=new FileInputStream("d:\\test.txt");
 FileOutputStream out=new FileOutputStream("d:/test1.text");
 while(int b=in.read()!=-1){
  out.write(b);
 }
 in.close();
 out.close();
}catch(){}
7 FileReader/FileWriter
  FileReader fr=null;
  FileWriter fw=null;
  int c=0;
  try{
 fr=new FileReader ("d:\\test.java");
 fw=new FileWriter ("d:/test1.java");
 while((c=fr.read)!=-1){
    System.out.print((char)c);
    fw.write(c);
 }
      fr.close();
      fw.close();
  }
 8 缓冲流
 常用构造方法
 BufferedReader(Reader in)
 BufferedReader(Reader in,int sz)
 BufferedWreter(Writer out)
 BufferedWreter(Writer out,int sz)
 BufferedInputStream(InputStream in)
 BufferedInputStream(InputStream in,int size)
 BufferedOutputStream(OutputStream out)
 BufferedOutputStream(OutputStream out,int size)
 *BufferedReader提供了readLine方法用于读取一行字符串
 *BufferedWreter提供了newLine用于写入一个行分隔符
 *可以使用flush方法将输出到缓冲流的数据在内存中清空立刻写入硬盘
try{
    FileInputStream fis=new FileInputStream("d:\\share\\HelloWorld.java");
    BufferedInputStream bis=new BufferedInputStream (fis);
    bis.mark(100);从第100开始读
}

try{
     BufferedWriter bw=new BufferedWriter (new FileWriter("d:\\test.txt"));
     BufferedReader br=new BufferedReader(new FileReader("d:\\test.txt"));
     String s=null;
     for(int i=0;i<=100;i++){
 s=String.valueOf(Math.random());
 bw.write(s);
        bw.newLine();//写一个换行符
     }
     bw.flush();//清空缓存写入文件
     while((s=br.readLine())!=null){
 System.out.println(s);
     }
     bw.close();
     br.close();
}
9 转换流
  InputStreamReader/OutputStreamWriter
  用于字节数据到字符数据之间的转换
  InputStreamReader 需要和 InputStream "套接"
  OutputStreamWriter 需要和 OutputStream "套接"
  InputStream isr=new InputStreamReader(System.in,"ISO8859-1");

try{
  OutputStreamWriter osw=new OutputStreamWriter (new FileOutputStream("d:\\test.txt"));
  osw.write("ssssssssssssssss");
  osw.close();
  osw=new OutputStreamWriter(new FileOutputStream("d:\\test.txt",true),"ISO8859-1");//true是指在原来的基础上添加
  osw.write("ddddddddddddddddddd");
  osw.close();
}

InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
String s=br.readLine();

10 流据流,存储和读取java原始类型
DataInputStream(InputStream in)/DataOutputStream(OutputStream out)
boolean readBoolean()
double readDouble()
String readUTF()

11 PrintStream / PrintWriter
printStream ps=null;
try{
   FileOutputStream fos=new FileOutStream("d:\\test.txt");
   ps=new PrintStream(fos);
}
if(ps!=null){
   System.setOut(ps);
}
int ln=0;
for(char c=0;c<=60000;c++){
   System.out.print(c+" ");
   if(ln++>=100){
 System.out.println();
 ln=0;
   }
}

12 ObjectInputStream/ObjectOutputStream
对象读写必须序列化,transient关键字表示该必性不被序列化
class t implements Serializable{
  int i=10;
  transient int j=9;
  double d=2.3;
}

T t=new T();
FileOutputStream fos=new FileOutputStream ();
ObjectOutputStream oos=new ObjectOutputStream (fos);
oos.writeObject(t);
oos.flush();
oos.close();
13 Externalizable接口
方法:
   void readExternal(ObjectInput in)
   void writeExternal(ObjectOutput out)

posted on 2009-11-29 21:18 junly 阅读(203) 评论(0)  编辑  收藏 所属分类: java

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


网站导航: