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();
    		}
    	}
    }


  • 相关阅读:
    完整约束二(学习笔记)
    完整约束一(学习笔记)
    表的创建与管理二(学习笔记)
    闪回技术(学习笔记)
    表的创建与管理一(学习笔记)
    借助AWR报告分析解决oracleCPU过高的问题(转)
    数据的集合运算(学习笔记)
    SQL:1999基本语法(学习笔记)
    表的连接操作(学习笔记)
    多表查询(学习笔记)
  • 原文地址:https://www.cnblogs.com/dengshiwei/p/4258552.html
Copyright © 2011-2022 走看看