zoukankan      html  css  js  c++  java
  • JAVA基础篇—文件与流

    处理字节流的抽象类

    InputStream 是字节输入流的所有类的超类,一般我们使用它的子类,如FileInputStream等.

    OutputStream是字节输出流的所有类的超类,一般我们使用它的子类,如FileOutputStream等.

    2.InputStreamReader  OutputStreamWriter

    处理字符流的抽象类

    InputStreamReader 是字节流通向字符流的桥梁,它将字节流转换为字符流.

    OutputStreamWriter是字符流通向字节流的桥梁,它将字符流转换为字节流.

    3.BufferedReader BufferedWriter

    BufferedReader 由Reader类扩展而来,提供通用的缓冲方式文本读取,readLine读取一个文本行,

    从字符输入流中读取文本,缓冲各个字符,从而提供字符、数组和行的高效读取。

    BufferedWriter  由Writer 类扩展而来,提供通用的缓冲方式文本写入, newLine使用平台自己的行分隔符,

    将文本写入字符输出流,缓冲各个字符,从而提供单个字符、数组和字符串的高效写入。

    InputStream能从來源处读取一個一個byte,
    所以它是最低级的,
    例:

    package 文件操作;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    public class TestCopy {
        public void copyFile(File file1,File file2)throws Exception{
        	FileInputStream in=new FileInputStream(file1);
        	FileOutputStream out=new FileOutputStream(file2);
        	byte[]bs=new byte[1024];
        	int n=0;
        	while ((n=in.read(bs))!=-1) {
        		out.write(bs, 0, bs.length);
    			
    		}
        	in.close();
        	out.close();
        }
        public static void main(String[] args) {
        	File file1=new File("D:/APP/copysrc.doc");
        	File file2=new File("D:/APP/copydes.doc");
    		if (file2.exists()) {
    			file2.delete();
    		}else {
    			try {
    				file2.createNewFile();
    			} catch (IOException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		}
    		if (!file1.exists()) {
    			try {
    				file1.createNewFile();
    			} catch (IOException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		}
    	   TestCopy testCopy=new TestCopy();
    	   try {
    		testCopy.copyFile(file1, file2);
    		System.out.println("文件复制完成");
    	} catch (Exception e) {
    		// TODO Auto-generated catch block
    		e.printStackTrace();
    	}
    	}
    }
    

       InputStreamReader
     InputStreamReader封裝了InputStream在里头,
     它以较高级的方式,一次读取一个一个字符,

    public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {     
                         
                FileInputStream fis=new FileInputStream("d://desktop//test.txt");     
                try {     
                    InputStreamReader isr=new InputStreamReader(fis,"utf8");     
                    int i;     
                    while((i=isr.read()) != -1){     
                        System.out.println((char)i);  
                    }     
                } catch (Exception e) {     
                    // TODO Auto-generated catch block     
                    e.printStackTrace();     
                }                                            
                         
        }
    

      BufferedReader
    BufferedReader则是比InputStreamReader更高级,
    它封裝了StreamReader类,
    一次读取取一行的字符

    public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {     
                         
                FileInputStream fis=new FileInputStream("d://desktop//test.txt");     
                     
                try {     
                    InputStreamReader isr=new InputStreamReader(fis,"utf8");                     
                    BufferedReader br=new BufferedReader(isr);     
                         
                    String line;     
                    while((line=br.readLine()) != null){     
                        System.out.println(line);     
                    }     
                } catch (Exception e) {     
                    // TODO Auto-generated catch block     
                    e.printStackTrace();     
                }   
    

      

  • 相关阅读:
    采用NAND Flash启动时出现Kernel panic not syncing: No init found错误
    ubuntu 11.10 安装小企鹅fcitx输入法
    UBoot中设定的bootdelay参数不起作用
    python 数据类型
    python运算符
    python符号//、%和/运算
    pytho 基本数据类型
    Python 开篇
    mmsplayer v2 java 之(mmsPlayer 播放类)
    mmsplayer v2 java 之(mmsTrack 音频类)
  • 原文地址:https://www.cnblogs.com/lc-java/p/7397836.html
Copyright © 2011-2022 走看看