黑马程序员---IO流FileReader和 FileWriter

---------------------- android培训java培训、期待与您交流! ----------------------

IO

IO常用基类

字节流的常用基类:

InputStream OutputStream

字符流的常用基类:

ReaderWriter

这四个类都是抽象类,需要被子类实现其中的抽象方法。

InputStream 用于向程序输入数据,数据的单位为8bit

OutputStream 用于程序输出数据,数据的单位为8bit

 

继承Reader的流都是向程序输入数据,且数据单位16bit

继承Writer的流都是程序输出数据,且数据单位16bit

代码示例:

//文件的在硬盘的创建和异常的处理
//创建一个文件并以字符方式写入一些数据
import java.io.*;
public class FileWriterDemo {
	public static void main(String[] args) {
		FileWriter fw = null;
		try {
			fw = new FileWriter("f:\\demo.txt");
			fw.write("tihs is  a FileWriter test!");// 将文件写入到流中
			fw.flush();// 刷新流的缓冲
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fw != null)
				try {
					fw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
	}
}
//从指定文件中以字符方式读取数据,
public class FileReaderDemo {

	public static void main(String[] args) {
		FileReader fr = null;

		try {
			fr = new FileReader("f:\\demo.txt");
			int ch = 0;
			while ((ch = fr.read()) != -1)
				System.out.print((char) ch);

		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			if (fr != null)
			try {
				fr.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}


//通过字符数组读取数据
import java.io.*;

public class FileReaderDemo2 {

	public static void main(String[] args) throws IOException {
		FileReader fr = null;

		fr = new FileReader("f:\\demo.txt");

		char[] chArray = new char[1024];//顶一个数组用于存储读取的数据
		int num = 0;
		while ((num = fr.read(chArray)) != -1)//返回读取的字符数 用num表示
			System.out.print("num=" + num + "...."
					+ new String(chArray, 0, num));//将字符数组转为字符串
		if (fr != null)
			fr.close();
	}
}
//注意:该代码没有进行异常处理

//读取文件存取到数组中然后打印到控制台
import java.io.*;

public class FileReaderTest {

	public static void main(String[] args) {
		FileReader fr = null;
		try {
			fr = new FileReader("f:\\wifi.bat");
			char[] chArray = new char[1024 * 2];
			int num = 0;
			while ((num = fr.read(chArray)) != -1) {
				System.out.print(new String(chArray,0,num));
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			if(fr != null) {
				try {
					fr.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
//文件的拷贝
//读取文件并使用数组存储读取道的数据,然后再write到指定的文件中
public static void copyFile() {
		FileWriter fw = null;
		FileReader fr = null;
		char[] buff = new char[1024];
		try {
			fw = new FileWriter("f:\\copy2.txt");
			fr = new FileReader("e:\\codeBox.txt");
			int len = 0;
			while ((len = fr.read(buff)) != -1) {
				fw.write(buff, 0, len);
			}

		} catch (IOException e) {

			e.printStackTrace();
		} finally {
			if (fw != null)
				try {
					fw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			if (fr != null)
				try {
					fr.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}

	}

---------------------- android培训java培训、期待与您交流! ----------------------

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值