1.文件读写
(1) java 文件模型
在硬盘上的文件是byte byte byte存储的,是数据的集合
(2)打开文件
有两种模式“rw”读写 “r”只读
RandomAssessFile raf=new RandomAssessFile(file,"rw");
文件指针,打开文件时,指针在开头 pointer=0
(3)写方法
raf.write('s')--->只写一个字节(后8位),同时指针移向下一个位置,准备再次写入
(4)读方法
int b = raf.read()---->只读一个字节
(5)文件读写完成后一定要关闭
2.IO流
1.字节流
1)InputStream,OutPutStream
InputStream抽象了应用程序读取数据的方式
OutputStream抽象了应用程序写数据的方式
2)EOF=End 读到-1就是到了结尾
3)输入流的基本方法
int b =in.read();读取一个字节无符号填充到int低八位
in.read(byte[] buf) 读取数据填充到字节数组
in.read(byte[]buf,int start,int size)读取数据到字节数组buf。从buf的start位置开始,存放size长度
4)输出流基本方法
out.write(int b) 写出一个byte到流,b的低八位
out.write(byte[] buf) 将buf 字节数组都写入到流
out.write(byte[] buf,int start,int size) 字节数组buf从start位置开始写size长度法的字节到流
5)FileInputStream---->具体实现了在文件上读取数据
package com.wisdomtraffic.common.MyTest;
import static org.junit.Assert.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.junit.Test;
public class IOUtil {
/**
* 读取指定文件类容,按照16进制输出到控制台
* 并每输出10个byte换行
* @param FileName
* @throws Exception
*/
private void printHex(String fileName) throws Exception {
//把文件作为字节流进行读操作
FileInputStream in=new FileInputStream(fileName);
int b;
int i=1;
while ((b=in.read())!=-1) {
// System.out.println(b);
System.out.print(Integer.toHexString(b)+" ");
if (i++%10==0) {
System.out.println();
}
}
}
/**
* 从in中批量读取字节,放入到buf这个字节数组中,
* 从第0个位置开始放,最多放buf.length个
* 返回的是读到的字节的个数。
* @param fileName
* @throws IOException
*/
private void printHexByByteArray(String fileName) throws IOException {
FileInputStream in =new FileInputStream(fileName);
byte[] bytes =new byte[1024*3];
int b=0;
while ((b=in.read(bytes, 0, bytes.length))!=-1) {
for (int i = 0; i < b; i++) { //b:读到的有效字节的个数
System.out.println(Integer.toHexString(bytes[i]&0xff)+" ");//int 32位,byte 8位,为避免数据转换出问题,通过按位与将高24位清零
}
}
// int
}
@Test
public void testName() throws Exception {
printHexByByteArray("c:\\hyl\\hhh.txt");
}
}
6)FileOutputStream--->实现了在文件上写出byte数据的方法
//文件copy
private void copyFile(File srcFile,File dFile) throws Exception {
if (!srcFile.exists()) {
throw new IllegalArgumentException("文件"+srcFile+"不存在");
}
FileInputStream in=new FileInputStream(srcFile);
FileOutputStream out =new FileOutputStream(dFile);//若该文件不存在则创建
byte[] bytes=new byte[1024];
int b; //将字节流读到bytes数组中
while((b=in.read(bytes, 0, bytes.length))!=-1){
out.write(bytes, 0, bytes.length); //将字节流从bytes数据组写入新的文件中
out.flush();
}
}
7)DateOutputStream/DateInputStream----->对“流”功能的扩展,可以更方便的读取int,long,字符等类型的数据
DateOutputStream writeInt()/writeDouble()/writeUtf()......
8)BufferedInputStream&BufferedOutputStream 关闭流后要刷新缓冲区 bos.flush();
这两个流类为IO提供了带缓冲区的操作,一般打开文件进行写入或读取操作时,都会加上缓冲,这种流模式提高了IO的性能
从应用程序中把数据放入文件,相当于把一缸水倒入另一缸中
FileOutputStream---->write()方法相当于把水一滴一滴的转移
DataUotputStream---->WriteXxx()方法更方便些,相当于一瓢一瓢转移
BufferedOutputStream---->write()相当于一瓢水先放入桶中,再从桶倒入另一缸中,性能提高了
2.字符流
1)编码问题
2)认识文本和文本文件
java的文本(char)是16位无符号整数,是字符的unicode编码(双字节编码)
文本是byte byte byte。。的数据数列
文本文件是文本(char)序列按照某种编码方案utf-8,gbk等序列化为byte的存储结果
3)字符流(Reader,Writer)
字符的处理:一次处理一个字符,字符的底层仍然是基本的字节序列
InputStreamReader完成byte流解析为char流。按照编码解析
OutputStreamWriter提供char流到byte流,按照编码处理
如需要改变编码,new InputStream(in,"utf-8")
FileWriter/FileReader
参数为TRUE 追加,不会删除文件中原有的内容
写入文件时不能换行,需要调用newLine()方法换行
对象的序列化与反序列化
1)序列化:将Object转换成byte序列,反之叫对象的反序列化。
2)序列化流:(ObjectOutputStream)是过滤流---writeObject
反序列化流:(ObjectInputStream)readObject
3)序列化接口(Serializable)
对象必须实现序列化接口才能实现序列化,
transient关键字:该元素不会进行jvm默认的序列化,也可以自己进行这个元素的序列化