Java基础学习笔记(十一)I/O 文件访问

昨天只看了点线程部分的内容,今天还是按计划学习I/O部分,线程的其他部分等I/O学完了在继续看。

I/O分类: 

从不同的角度分类:

按数据流的方向不同可以分为 输入流 和 输出流。
按处理数据单位不同可以分为 字节流 和 字符流。
按照功能不同可以分为 节点流 和 处理流
所有的流类型位于java.io内都分别继承以下四种抽象类;
字节流       字符流
输入流  :InputStream    Reader
输出流  :OutputStream   Writer
I/O类中以Stream结尾的都是字节流,Reader/Writer结尾的都是字符流。

访问文件:FileInputStream与FileOutputStream

FileInputStream和FileOutputStream分别继承自InputStream和OutputStream用于向文件中输入和输出字节。
构造方法:
FileInputStream(String name)  throws  FileNotFountException
FileInputStream(File file) 
throws  FileNotFountException
FileOutputStream(String name) 
throws  FileNotFountException
FileOutputStream(File file) 
throws  FileNotFountException
FileOutputStream(File file, 
boolean  append)  throws  FileNotFountException

 

FileInputStream和FileOutputStream类支持其父类InputStream和OutputStream所提供的数据读写方法。
在实例化FileInputStream和FileOutputStream流时要用try-catch语句处理其可能抛出的FileNotFoundException,
在读写数据时也要用途try-catch语句处理可能抛出的IOException。
FileInputStream类的使用实例:
public   class  TestFileInputStream{
    
public   static   void  main(String[] args){
        
int  b  =   0 ;
        FileInputStream in 
=   null ;
        
try {
            in 
=   new  FileInputStream( " c:/TestFileInputStream.java " );
        }
catch (FileNotFoundException e){
            System.out.println(
" 找不到指定文件 " );
            System.exit(
- 1 );
        }
    
        
try {
            
long  num  =   0 ;
            
while ((b  =  in.read()) !=- 1 ){    // read()方法读取一个字节,返回类型是int
                System.out.print(( char )b); // read()方法读取中文字符时是乱码,因为中文是两个字节的。
                num ++ ;
            }
            in.close();
            System.out.println();
            System.out.println(
" 共读取了 "   +  num  +   " 个字节 " );
        }
catch (IOException e1){
            System.out.println(
" 文件读取错误 " );
            System.exit(
- 1 );
        }
    }
}

 

FileOutputStream使用实例:
import  java.io. * ;
public   class  TestFileOutputStream{
    
public   static   void  main(String[] args){
        
int  b  =   0 ;
        FileInputStream in 
=   null ;
        FileOutputStream out 
=    null ;
        
try {
            in 
=   new  FileInputStream( " c:/TestFileInputStream.java " );
            out 
=   new  FileOutputStream( " c:/test.txt " );  // 没有的时候自动创建文件
             while ((b  =  in.read()) !=   - 1 ){
                out.write(b);
            }
            in.close();
            out.close();
        }
catch  (FileNotFoundException e2){
            System.out.println(
" 找不到指定文件 " );
            System.exit(
- 1 );
        }
catch  (IOException e1){
            System.out.println(
" 文件复制错误 " );
            System.exit(
- 1 );
        }
        System.out.println(
" 文件已复制 " );
    }
}

 FileReader和FileWriter

 FileReader、FileWriter与上面两个类的方法基本相同,只是在后两者里,读取和写入的是字符,即两字节。

FileReader例子:
import  java.io. * ;
public   class  TestFileReader{
    
public   static   void  main(String[] args){
        FileReader r 
=   null ;
        
int  c;
        
try {
            r 
=   new  FileReader( " c:/TestFileReader.java " );
            
while ((c  =  r.read()) !=   - 1 ){   // read()方法读取字符返回int类型
                System.out.print(( char )c);
            }
            r.close();
        }
catch (FileNotFoundException e1){
            System.out.println(
" 找不到指定文件 " );  // 中文不会出现乱码
        } catch (IOException e2){
            System.out.println(
" 文件读取错误 " );
        }
    }
}

 

FileWriter例子:


import java.io.*;
public  class  TestFileWriter{
    
public  static  void  main(String[] args){
        FileWriter fw 
=  null ;
        
try {
            fw 
=  new  FileWriter( " c:/unicode.txt " );
            
for ( int  i  =  0 ;i  <  5000 ;i  ++ ){
                fw.write(i);  
// 写字符,两个字节
            }
            fw.close();
        }
catch (IOException e1){
            e1.printStackTrace();
            System.out.println(
" 文件写入错误 " );
            System.exit(
- 1 );
        }
        System.out.println(
" 文件写入成功 " );
    }

 

 

 

 

转载于:https://www.cnblogs.com/moupeng/archive/2010/12/30/1921654.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值