Java 课堂记录 04 (文件和输入输出流)

1、File :

实现的接口:Serializable, Comparable . 是文件或目录的抽象表示。
构造方法:File(URI uri): URI 是统一资源标识符,具体分析详见 Java中URI,URL和URN的使用
                 File(String pathname);  File(String directoryPathname, String filename); File(File directory, String fileName)。
常用方法: getName();   getPath();  getAbsolutePath();  getParent();   length();  lastModified();
                  canRead();   canWrite();   exists();  isFile();  isDirectory();  createNewFile();  delete();  mkdir();  mkdirs().
                 String[] list();    File[] listFiles();     boolean renameTo( File dest );   boolean setReadOnly(); ....
// File 对象只是一个引用,并不表示硬盘上的一个存在的文件或目录。

2、Stream :

File类主要用来描述系统中的文件在磁盘上的存储情况。Steam类主要是对文件的内容进行操作,我们必须通过FileInputStream, FileOutputStream或者 RandomAccessFile流来存取文件。
流分为字节流(InputStream, OutPutStream)和字符流(Reader, Writer , 后来引入) 。



Java 中由于是面向对象的,所以对流操作时是同过不断的对象的“转换”(靠构造函数的参数和返回值的变化),也称“Wraps”。要在实际中正确转换,需要知道一下流的个子类的基本命名规则:比如 FileInputStream ,我们拆为两部分,A=File, B=InputStream. B表明这个类实现的是一个字节流(InputStream),A表明这个类的构造函数的参数跟File的构造函数的参数类型一致,可以为File对象,也可以为String对象。
理解了此规则,就很容易地知道要从InputStream流(如 System.in)转换到一个可以缓冲的字节流的代码为:BufferedReader br = new BufferedReader( new    InputStreamReader(System.in) ).

四个基类都提供了相应的简单地读写函数read(); read(byte[]); read( byte[], int off, int len ); 这几个函数并不能满足我们的需要,所以我们会经常使用 BufferedReader (提供了readLine(),等)和 PrintWriter ( 可以格式化输出, println()等)。

下面是我的一个小练习:
实现从1》键盘读入一行并输出; 2》拷贝一个文件到另一个文件。 分别用ByByte(字节流操作)和ByChar(字符流操作)来实现。

import java.io.*;
import java.util.*;

public class Practice
{
    public static void main( String[] args ) throws IOException
    {
        ByByte sb1 = new ByByte();
        ByChar sc1 = new ByChar();
        String str = "test.txt";
        String str1 = "copy_by_char.txt";
        String str2 = "copy_by_byte.txt";       
        System.out.println( "**************** Stream Operation by Byte :  ******************" );
        sb1.showOneLine(); System.out.println(  '/n' + "Show File"  + str + '/n');
        sb1.showFile(str); System.out.println( '/n' + "Copy File to " + str1 +'/n' );
        sb1.copyFile( str, str1);   

        System.out.println( "**************** Stream Operation by Char :  ******************" );       
        sc1.showOneLine(); System.out.println(  '/n' + "Show File " + str + '/n' );
        sc1.showFile(str); System.out.println(  '/n' + "Copy File to " + str2 +'/n');
        sc1.copyFile( str, str2);
    }
}

class ByByte
{
    public ByByte(){}
    public void showOneLine() throws IOException
    {
        System.out.println( "Input a line of , WHATEVER : " );
        byte[] buffer = new byte[20000];
        int count = System.in.read( buffer );
        System.out.println( "Show by ASC|| code " );
        for (int i=0;i                System.out.print(" "+buffer[i]);
        System.out.println();
        System.out.println( "Show by char " );
        for (int i=0;i                System.out.print((char) buffer[i]);
    }
    public void showFile( String name) throws IOException
    {
        BufferedInputStream fis =  new BufferedInputStream( new FileInputStream( name ) ) ;
        int size = fis.available() ;       
    for(int i=0;i          System.out.print( (char)fis.read() );
    fis.close(); 
    }
    public void copyFile( String name1, String name2 ) throws IOException
    {
        BufferedInputStream fis =  new BufferedInputStream( new FileInputStream( name1 ) ) ;
        BufferedOutputStream bos =  new BufferedOutputStream( new FileOutputStream( name2 ) ) ;       
        int size = fis.available() ;       
    for(int i=0;i          bos.write( fis.read() );
    fis.close();   bos.close();
    }

}

class ByChar
{
    public ByChar(){}
    public void showOneLine() throws IOException
    {
        BufferedReader in = new BufferedReader( new InputStreamReader( System.in )); 
        System.out.println( "Input a line of , WHATEVER : " );
    String str = in.readLine();
        System.out.println( "Show by char " );   
    System.out.print( str );
    }
    public void showFile( String name) throws IOException
    {
        FileReader fr = new FileReader( name );
    BufferedReader br = new BufferedReader( fr );
    String read;
    while((read = br.readLine())!= null)
               System.out.println(read);
    fr.close();   br.close();  // 先关里层流对象
    }
    public void copyFile( String name1, String name2 ) throws IOException
    {
    BufferedReader br = new BufferedReader( new FileReader( name1 ) );
    BufferedWriter bw = new BufferedWriter( new FileWriter( name2 ) );
    PrintWriter pw = new PrintWriter(bw);
    String read;
    while((read = br.readLine())!= null)   // BufferedReader 的readLine()
                 pw.println(read);   // PrintWriter 的格式化输出
    br.close();     pw.close();
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值