zoukankan      html  css  js  c++  java
  • Java中文件流笔记

    1、数据流分输入流(InputStream)和输出流(OutputStream),其中输入流只能进行数据的读取而不能进行数据的写出,输出流只能写入数据而不能读取数据。

    2、可以进行读取的字节的类被称为输入类流。


    import java.io.*;
    public class Test
    {
    	public static void main(String args[])
    	{
    		InputStream is = null;
    		OutputStream os = null;
    		try
    		{
    			is = new FileInputStream("C:\test.txt");
    			os = new FileOutputStream("C:\testcopy.txt");
    			int bt = 0;
    			long num = 0;
    			while((bt = is.read())!=-1)
    			{
    				num++;
    				os.write(bt);
    			}
    			
    			System.out.println("读取字节数:" + num);             //一个汉字是占两个字节。
    			is.close();
    			os.close();
    		}
    		catch (FileNotFoundException ex)
    		{
    			System.out.println("文件不存在。");
    			ex.printStackTrace();
    		}
    		catch(IOException ex)
    		{
    			System.out.println("文件读取错误。");
    			ex.printStackTrace();
    		}
    	}
    }


    import java.io.*;
    public class Test
    {
    	public static void main(String args[])
    	{
    		InputStream is = null;
    		OutputStream os = null;
    		BufferedInputStream bis = null;
    		BufferedOutputStream bos = null;
    		try
    		{
    			String sFilePath = "C:\test.txt";
    			String sFileCopyPath = "C:\testcopy.txt";
    			is = new FileInputStream(sFilePath);
    			bis = new BufferedInputStream(is);
    			os = new FileOutputStream(sFileCopyPath);
    			bos = new BufferedOutputStream(os);
    			int bt = 0;
    			long num = 0;
    			while((bt = bis.read())!=-1)
    			{
    				num++;
    				bos.write(bt);
    			}
    			
    			System.out.println("读取字节数:" + num);             //一个汉字是占两个字节。
    			is.close();
    			os.close();
    		}
    		catch (FileNotFoundException ex)
    		{
    			System.out.println("文件不存在。");
    			ex.printStackTrace();
    		}
    		catch(IOException ex)
    		{
    			System.out.println("文件读取错误。");
    			ex.printStackTrace();
    		}
    	}
    }


  • 相关阅读:
    Java I/O (1)
    hadoop集群添加新节点
    [kuangbin带你飞]专题三 Dancing Links
    Codeforces Round #580 (Div. 2)(A、B、C)
    2019 年百度之星·程序设计大赛
    [kuangbin专题] KMP
    Codeforces Round #578 (Div. 2)(A、B、C、D、E)
    二维前缀和、差分习题集
    [kuangbin带你飞]专题七 线段树
    Codeforces Round #577 (Div. 2) (A、B、C)
  • 原文地址:https://www.cnblogs.com/dengshiwei/p/4258552.html
Copyright © 2011-2022 走看看