java IO流 之 节点流

前言:

首先介绍两个关于路径的名词:

相对路径:在当前文件目录下的文件路径;

绝对路径:包括盘符在内的完整的文件路径;

介绍File

java中凡是与输入输出相关的类 接口都定义在java.io包下;

File是一个类,可以有构造器创建其对象,此对象对应着一个文件或者文件夹,File类的对象是与平台无关的可以是linux windows等等,File类的对象常作为io流的具体类的构造器的形参;

File类中的方法,紧涉及文件的创建 删除 重命名等,只要涉及文件内容的,File类时无能为力的,必须由IO流来完成。

正文:

JAVA IO原理:

输入input : 读取外部数据(磁盘,光盘等存储设备)到程序中(内存)中;

输出output : 将程序(内存)数据输出到磁盘 光盘等存储设备中;


按照操作数据单位不同分为:字节流(8bit)字符流(16bit)

按照数据流的流向不同分为:输入流 输出流

按照流的角色不同分为:节点流(直接作用在文件上的流)和处理流(作用在已有节点流之上,或者作用在已有的处理流之上)

节点流包含:FileInputStream  FileOutputStream ; FileReader  FileWriter.其余基本均为处理流;  

第一Part:

对于非文本文件处理(视屏文件 音频文件 图片) 只能使用字节流 FileInputStream FileOutpputStream,注 对于.doc文件中即含有字符 又含有图片视频等字节文件  要按照字节流进行处理

编码中用到 将磁盘中字节文件写入程序 即FileInputSteam的处理 代码如下:

@Test
public void test() {
File file = new File("readme.txt");
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(file);
byte[] b = new byte[5];
int len;
while (-1 != (len = fileInputStream.read(b))) {
/*
* for(int i = 0 ; i < len ; i++ ){//注意此处的len不是b.length
* System.out.print((char)b[i]); }
*/此处的处理有上下俩种方法
String str = new String(b, 0, len);
System.out.print(str);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (null != fileInputStream) {
try {
fileInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

编码中用到 将程序中的字节文件写入磁盘  即FileOutputSteam的处理 代码如下:

@Test
public void test(){
File file = new File("test.txt");
FileOutputStream fileOutputStream = null;
try {
byte[] b = new String("this is a test").getBytes();
fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(b);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(null != fileOutputStream){
try {
fileOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

最后简单实现一下字节文件复制的工具方法:

public static void copy(String srcFile, String descFile) {
File src = new File(srcFile);
File desc = new File(descFile);

FileInputStream fileInputStream = null;
FileOutputStream fileOutPutStream = null;

try {
fileInputStream = new FileInputStream(src);
fileOutPutStream = new FileOutputStream(desc);
byte[] b = new byte[3];
int len;
while (-1 != (len = fileInputStream.read(b))) {
fileOutPutStream.write(b, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != fileInputStream) {
try {
fileInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

if (null != fileOutPutStream) {
try {
fileOutPutStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

第二part

对于文本文件使用 使用字节流 FileReader FileWriter


编码中用到将文本文件写入程序 即FileReader的处理 代码如下:

@Test
	public void test() {
		File file = new File("readmeCH.txt");
		FileReader fr = null;
		try {
			fr = new FileReader(file);
			char[] c = new char[5];
			int len;
			while (-1 != (len = fr.read(c))) {
				System.out.println(new String(c, 0, len));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != fr) {
				try {
					fr.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
编码中用到将程序中的文本文件写入磁盘 即FileWriter的处理 代码如下:

@Test
	public void test01() {
		File file = new File("readmeCh01.txt");
		FileWriter fw = null;
		try {
			fw = new FileWriter(file);
			char[] c = new char[] { '我', '爱', '你', '中', '国' };
			fw.write(c);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != fw) {
				try {
					fw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

最后简单实现一下文本文件复制的工具方法:
public static void copy(String src, String desc) {
		File fileSrc = new File(src);//对应的src文件一定要存在,否则会报异常
		File fileDesc = new File(desc);//对应的desc可以不存在,会自动创建
		FileReader fr = null;
		FileWriter fw = null;
		try {
			fr = new FileReader(fileSrc);
			fw = new FileWriter(fileDesc);

			char[] b = new char[5];
			int len;
			while (-1 != (len = fr.read(b))) {
				fw.write(b, 0, len);
				System.out.println(b);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (null != fr) {
				try {
					fr.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (null != fw) {
				try {
					fw.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值