码迷,mamicode.com
首页 > 编程语言 > 详细

黑马程序员——Java I/O基础知识之I/O流

时间:2014-05-20 02:00:35      阅读:376      评论:0      收藏:0      [点我收藏+]

标签:des   style   class   c   java   color   

I/O流基础知识——字节流和字符流
     文件存储在硬盘中,是以二进制表示的,只有内存中才能形成字符。数据的来源可以有硬盘,内存,控制台,网络,Java把数据从一个地方转到另一个地方的现象称为流,用InputStream和OutputStream接口来表示,这两个流里面的都是以字节为单位的,后来加入了Reader和Writer,里面操作的是字符,是两个字节为单位的。
字节流
字节流将数据写入文件
         
           try {
                   File file =new File("d:" +File .separator+"my.txt") ;
                   OutputStream ops =new FileOutputStream(file );
                    byte[] buff ="白日依山尽\r\n" .getBytes ()//加上\r\n可换行             
                    //一个字节一个字节地写
                    for(byte bufbuff){
                             ops .write (buf) ;
                    }
                    //只要程序还没有关闭,就可以继续写。                    
                    //一次写入一个字节数组。
                   ops .write (buff) ;
                   ops .close ();
           //文件中的结果      
/*                白日依山尽
                   白日依山尽
*/
字节流往文件中追加内容
        
           try {
                     File file = new File ("d:" +File . separator+ "my.txt") ;
                    //只需要在这里添加上参数true就行了,前提是文件中有内容
                   OutputStream ops =new FileOutputStream( filetrue);
                    byte[] buff ="黄河入海流\r\n" .getBytes ()//加上\r\n可换行
              //文件中的结果  
/*                白日依山尽
                   黄河入海流*/
字节流读取文件内容
           try {
                   File file =new File("d:" +File .separator+"my.txt") ;
                    byte[] buff =new byte[ 1024];
                   InputStream ips =new FileInputStream(file );
                    int len;
                    //file.length()获取文件大小
          System .out.println( "文件大小:" +file .length ());
                    while((len =ips .read (buff)) !=-1 ){
                             System .out.println( new String( buff0len));
/*                文件大小:24
          白日依山尽
          黄河入海流*/
 
字符流写入和写出
字符流的写入和写出与字节流基本一致,只用来处理纯文本的文件,单位是char字符;
                    try {
                             File file =new File("d:" +File .separator+"mychar.txt") ;
                              if(!file.exists()) filecreateNewFile();
                             Writer ww =new FileWriter(file ,true);
                             ww .write ("\n我还是字符流")//字符流不需要转成byte。
                             ww .close ();
                              char [] charsnew char [200] ;
                             Reader rr=new FileReader (file) ;
                              int len;
                              while((len =rr .read (chars)) !=-1 )System.out.println( new String(chars,0,len)) ;
/*                         我是字符流
                             我还是字符流
                             我还是字符流*/
复制文件
通过IO将文件从源写到目标去,这时可能需要使用到其他的装饰流,扩展功能,使流可以在多种源和目的之间转移。
 
class CopyDemo{
           public static void copydir (File dir,File dest){
                    //文件夹部分
                    if(!dir.exists()) Systemoutprintln("别逗" );
                    if(dir .isDirectory ()){
                              if(!dest.exists()) destmkdir();
                                       for(String files :dir .list ())
                                       {//将目录下的每一个文件名罗列出来,通过递归筛选文件夹或者文件执行相关代码
                                                File dirfile=new File (dir,files) ;                                         
                                                File destfile=new File (dest,files) ;
                                                 copydir( dirfiledestfile);
                                       }
                    }
                    //文件部分
                    else{
                              byte[] buff =new byte[ 1024];
                              int len;
                              try {
                                       //文件复制就是把数据从硬盘的一个地方写到另一个地方
                                      InputStream ips =new FileInputStream(dir );
                                      OutputStream ops =new FileOutputStream(dest );
                                       while((len =ips .read (buff)) !=-1 )ops.write( buff0len);
                                      ips .close ();
                                      ops .close ();
                              } catch (Exception e ) {
                                       // TODO: handle exception
                                      e .printStackTrace ();
                              }
                    }
           }
}
public class CopyTest {
           public static void main (String[] args) {
                    // TODO Auto-generated method stub
CopyDemo cd=new CopyDemo();
File dir=new File(arg[0]);
File destnew File(arg[1]);
try {
           cd.copydir(dirdest);
} catch (Exception e ) {
           // TODO: handle exception
          e .printStackTrace ();
}
           }
 
}
字节流和字符流相互转换。InputStreamReader和OutputStreamWriter分别将字节输入流转换成字符流和将字节输出流转换成字符流。
try {
           //字符转换流可以将原本是字节流输出,转成字符流输出
          File file = new File ("d:" +File . separator+ "singer.txt") ;          
           if (! file. exists()) filecreateNewFile ();
          Writer ww = new OutputStreamWriter (new FileOutputStream (file ));
          ww .write ( "我是歌手" );
          ww .close ();
           //同理也可以将字节输入流转换成字符流
           //最常用的是把从控制台获得的字节流转换转换成字符流以便操作
          Reader rr = new InputStreamReader (System . in) ;
           char [] chsnew char [ 100] ;
           int len;
           while ((len =rr .read ( chs)) !=-1 )
          System . out. println( new String( chs0len ));

黑马程序员——Java I/O基础知识之I/O流,布布扣,bubuko.com

黑马程序员——Java I/O基础知识之I/O流

标签:des   style   class   c   java   color   

原文地址:http://www.cnblogs.com/shuawang/p/3731090.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!