zoukankan      html  css  js  c++  java
  • IO流,字节流复制文件,字符流+缓冲复制文件

    JAVAIO如果按流向分:输入流和输出流两种

              输入流的基类:InputStream   Reader

              输出流的基类:OutputStream   Writer

        如果按数据单元划分:字节流和字符流

              字节流输入输出的基类:InputStream  OutputStream

              字符流输入输出的基类:Reader   Writer

    字节流复制文件内容

    public static void main(String[] args) {
    		//字节流复制文件内容
    		InputStream io=null;
    		OutputStream os=null;
    		try {
    			io=new FileInputStream("D:/a.txt");
    			os=new FileOutputStream("D:/c.txt");
    			int a=0;
    			byte[] b=new byte[1024];
    			while((a=io.read(b))!=-1){
    				os.write(b,0,a);
    			}
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}finally{
    			try {
    				os.close();
    				io.close();
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    }
    

      注意事项:java 的路径符号是:“/” “\”  而我们的系统路径符号是:“”  读取和写入文件完毕时记得关机流,否则会读取不了文件内容

    字符流+缓冲复制文件内容

    public static void main(String[] args) {
    		//字符流+缓冲复制文件文件内容
    		Reader read=null;
    		BufferedReader br=null;
    		Writer write=null;
    		BufferedWriter bw=null;
    		
    		try {
    			read=new FileReader("D:/a.txt");
    			br=new BufferedReader(read);
    			write=new FileWriter("D:/d.txt");
    			bw=new BufferedWriter(write);
    			String s="";
    			while((s=br.readLine())!=null){
    				bw.write(s);
    			}
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}finally{
    			try {
    				bw.close();
    				write.close();
    				br.close();
    				read.close();
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    }
    

      注意事项:关流时记得按顺序:先开的后关,后开的先关

  • 相关阅读:
    C# 关于类的事件和委托
    C# 多态
    C# 声明方法的语法
    C# 面向对象基础
    近期发现的一些.net资源
    asp.net 2.0学习资源
    设置VSS使支持通过Internet访问
    大型社区设计:提高用户体验的10个细节
    委托的用法
    有滚动条、固定Header的ASP.Net DataGrid实现
  • 原文地址:https://www.cnblogs.com/LittleBoys/p/12090904.html
Copyright © 2011-2022 走看看