java IO流

一、IO类图

                 从图中可知流分为字节流和字符流。

                字符流:处理的单元为 2 个字节的 Unicode 字符,分别操作字符、字符数组或字符串、文本。

                字节流:处理单元为 1 个字节,操作字节和字节数组。

                                        


    1. 输入流的基本方法:

     abstract int read() :读取一个字节数据,并返回读到的数据,如果返回-1,表示读到了输入流的末尾。
      int read(byte[] b) :将数据读入一个字节数组,同时返回实际读取的字节数。如果返回-1,表示读到了输入流的末尾。
      int read(byte[]b, int off, int len) :将数据读入一个字节数组,同时返回实际读取的字节数。如果返回-1,表示读到了输入流的末尾。off指定在数组b中存放数据的起始偏移位         置;len指定读取的最大字节数。
     其它方法
      long skip(long n) :在输入流中跳过n个字节,并返回实际跳过的字节数。
      int available() :返回在不发生阻塞的情况下,可读取的字节数。
      void close() :关闭输入流,释放和这个流相关的系统资源。
      void mark(int readlimit) :在输入流的当前位置放置一个标记,如果读取的字节数多于readlimit设置的值,则流忽略这个标记。
      void reset() :返回到上一个标记。
      boolean markSupported() :测试当前流是否支持mark和reset方法。

     2.输出流的基本方法:

     abstract void write(int b) :往输出流中写入一个字节。
     void write(byte[] b) :往输出流中写入数组b中的所有字节。
     void write(byte[] b, int off, int len) :往输出流中写入数组b中从偏移量off开始的len个字节的数据。
      其它方法
     void flush() :刷新输出流,强制缓冲区中的输出字节被写出。
     void close() :关闭输出流,释放和这个流相关的系统资源。

   二、相关类使用的简单例子

package com.io.test;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.Reader;
import java.io.Writer;

public class ReaderTest {
	/**
	 * 字符流的形式的拷贝txt文件
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Reader in=null;
		Writer out=null;
		String path="C:\\Users\\Administrator\\Desktop\\aaa.txt";//桌面文件路径
		String copypath="C:\\Users\\Administrator\\Desktop\\copy.txt";//将存放的的路径
     try {
		 in=new FileReader(new File(path));
		 out=new FileWriter(new File(copypath));
		 char ch[]=new char[8];
		 //设置八个字符读取		
		 while((in.read(ch))!=-1){	
			 //如果in.read(ch)返回-1,文件读取结束 退出while循环
			 out.write(ch);	//开始写的操作		 
		 }
		 in.close();
		 out.close();
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} 
	}

}

Test2

package com.test;

import java.io.FileReader;
import java.io.LineNumberReader;

public class Test {

	/**
	 * 一行一行的形式 拷贝和打印字符流文件
	 * 一行行读取的好处就是 可以操作文件内容 比如去空格,去注释,去关键字等
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		LineNumberReader line=null;
		String filepath="E:\\workspace\\Core\\src\\com\\test\\TTTTest.java";
		try{
        line=new LineNumberReader(new FileReader(filepath));
		String str=null;		
		while((str=line.readLine())!=null){		
			str=str.trim();
			if(str.contains("//"))
				System.out.println("去注释的");
			else
			{
				str=str.replaceAll(" ", "");//去空格的
				System.out.println(line.getLineNumber()+"  "+str);
				}

		}
		line.close();
		}catch(Exception e){
                   System.out.println("失败了");
		}
	}

}

test3

package com.thread;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
public class Testio {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int count=0;
	FileInputStream str =null;
        FileOutputStream out=null;
   try {
	byte b[]=new byte[64];
	str=new FileInputStream(new File("C:/Users/Administrator/Desktop/pwd.txt"));
	out=new FileOutputStream(new File("C:/Users/Administrator/Desktop/pwd2.txt"));
	
	while((count=str.read(b))!=-1){
		out.write(b,0,count);		
	}
	System.out.println("文件字节长度为:"+count);
		
} catch (Exception e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}

	}

}



Test4  文件夹复制



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值