IO流
IO流
主要内容:
java.io.File:计算机操作系统中的文件和文件夹
IO:指的是Input输入和、Output输出
流的理解:通过程序把一个图放到一个文件夹,把图片转化为一个数据集(例如二进制),把这些数据一点一点传到文件夹,这个传递的过程就很类似于水的流动。
可以称整体的数据集是一个数据流(stream)。
文件流:数据流的读写都是基于文件的操作。
- FileInputStream / FileOutputStream / FileReader / FileWriter
缓冲流:数据流的读写操作都是基于内存的操作。
-
BufferedInputStream / BufferedOutputStream /
-
BufferedReader / BufferedWriter
转换流
- InputStreamReader / OutputStreamWriter
标准输入/输出流
打印流
- PrintStream / PrintWriter——>System.out.println();
数据流
- DataInputStream / DataOutputStream
对象流 :把一个对象转化为一个数据流就行读写
----涉及序列化、反序列化
- ObjectInputStream / ObjectOutputStream
随机存取文件流
- RandomAccessFile
例如:一个TXT文件,其中有100行数据,可以直接读取第50行的数据。也可以在第89行插入数据。这个才是随机的概念。
File类
java.io.File类:文件和目录路径名的抽象表示形式,与平台无关
File 能新建、删除、重命名文件和目录,但 File 不能访问文件内容本身。如果需要访问文件内容本身,则需要使用输入/输出流。
File对象可以作为参数传递给流的构造函数
File类的常见构造方法:
- public File(String pathname)
? 以pathname为路径创建File对象,可以是绝对路径或者相对路径,如果pathname是相对路径,则默认的当前路径在系统属性user.dir中存储。
- public File(String parent,String child)
? 以parent为父路径,child为子路径创建File对象。
File的静态属性String separator存储了当前系统的路径分隔符。
在UNIX中,此字段为‘/’,在Windows中,为‘\’
File类只能操作文件本身,但是不能操作文件内容类似于,你可以把一个日记本放在各个地方,但是不能在日记本中写日记。
File 类代表与平台无关的文件和目录。
File 能新建、删除、重命名文件和目录,但 File 不能访问文件内容本身。如果需要访问文件内容本身,则需要使用输入/输出流。
File方法

File f=new File("D:\\test\\abc\\tt.txt");
// File f1=new File("D:\\test","abc\\tt.txt");
File f2=new File("D:/test/abc");
// File f4=new File("D:"+File.separator+"test/abc/tt.txt");
//这个时候对象就能够得到tt.txt文件
//这个f1也是tt.txt文件,这种使用相对比较少
//注意:这个斜杠\在文件中是路径的分隔符,但是在java编程中一个\的意思是转义符
//在java中\\或者/才是文件的分隔符
//也可以File.separator作为分割符
System.out.println(f.getName());//获取文件名
System.out.println(f2.getName());//获取当前的文件名称
File f5=new File("src/io/Demo01.java");//使用相对路径创建File对象
System.out.println(f5.getPath());//获取当前文件或者文件夹的相对路径
//就是new File时候写的路径
System.out.println(f5.getAbsolutePath());
//获取文件的相对路径
//是绝对路径,它会给你补齐盘符
System.out.println(f5);
System.out.println(f5.getAbsoluteFile());
//返回一个当前的文件的绝对路径构建的file对象
System.out.println(f.getParentFile());
//返回当前文件或者文件夹的父级路径
f.renameTo(new File("D:\\test\\abc\\tt1.txt"));
//给文件或文件夹重命名
File f6=new File("D:\\test\\abc1");
System.out.println(f6.exists());
//判断文件或者文件夹是否存在
File f7=new File("D:\\test\\abc\\tt1.txt");
System.out.println(f7.canRead());//判断文件是否可读
System.out.println(f7.canWrite());//判断文件是否可写
System.out.println(f7.isFile());//判断当前的File对象是不是文件
System.out.println(f7.isDirectory());//判断当前对线是不是文件夹或者目录
System.out.println(f7.lastModified());//获取文件的最后修改时间,返回一个毫秒数
System.out.println(f7.length());//返回文件的长度,单位是字节数
File f8=new File("D:\\test\\abc\\tt2.txt");
System.out.println(f8.exists());
if (!f8.exists()) {//判断文件是否存在
try {
f8.createNewFile();//创建新的文件
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
// f8.delete();//删除文件
File f9=new File("D:\\test\\abc\\cc\\dd");
f9.mkdir();//创建单层目录,
//如果使用这样的方法来创建多层目录,就得一层一层的执行mkdir()
File f10=new File("D:\\test\\abc\\a\\b\\c");
f10.mkdirs();//这个方法是直接用来创建多层目录
File f11=new File("D:\\test");
String[] f1=f11.list();//返回当前文件夹的子集名称(String),包括目录和文件
for(String s:f1) {
System.out.println(s);
}
File[] fs=f11.listFiles();//返回当前文件夹的子集对象(File),包括目录和文件
for (File s : fs) {
System.out.println(s);
}
File的简单使用
遍历d盘下的test文件夹,把test文件夹下的所有目录和文件全部遍历出来,
无论层级有多深要全部遍历出来,使用递归的方法
public static void main(String[] args) {
// TODO 自动生成的方法存根
//遍历d盘下的test文件夹,把test文件夹下的所有目录和文件全部遍历出来
//无论层级有多深要全部遍历出来
//使用递归的方法
File f=new File("D:\\test");
new Demo02().test(f);
}
/*
* 递归遍历文件
*/
public void test(File file) {
if(file.isFile()) {
System.out.println(file.getAbsolutePath()+"是文件");
}else {
System.out.println(file.getAbsolutePath()+"是文件夹");
//如果是文件夹,这个文件夹可能有子文件夹
File[] fs=file.listFiles();//获取当前文件下的子文件夹或者文件的File对象
if(fs!=null||fs.length > 0)
//如果子集非空或者长度大于0进行遍历集合fs
{
for(File f : fs) {
test(f);//递归
// if(file.isFile()) {
// System.out.println(f.getAbsolutePath()+"是文件");
// }else {
// System.out.println(file.getAbsolutePath()+"是文件夹");
// //如果是文件夹,这个文件夹可能有子文件夹
//
// File[] fs1=file.listFiles();//获取当前文件下的子文件夹或者文件的File对象
// if(fs!=null||fs.length > 0) {
// for(File f1 : fs1) {
// System.out.println(f1.getAbsolutePath()+"是文件");
// }
// }
// }
}
}
}
}
IO原理
iO流用来处理设备之间的数据传输。
Java程序中,对于数据的输入/输出操作以”流(stream)” 的方式进行。
java.io包下提供了各种“流”类和接口,用以获取不同种类的数据,并通过标准的方法输入或输出数据。
输入input:读取外部数据(磁盘、光盘等存储设备的数据)到程序(内存)中。
输出output:将程序(内存)数据输出到磁盘、光盘等存储设备中。
流的分类:
按操作数据单位不同分为:字节流(8 bit),字符流(16 bit)
按数据流的流向不同分为:输入流,输出流
按流的角色的不同分为:节点流,处理流

Java的IO流共涉及40多个类,实际上非常规则,都是从如下4个抽象基类派生的。
由这四个类派生出来的子类名称都是以其父类名作为子类名后缀。
IO体系

文件字节输入流
public static void testFileInputStream() {
try {
FileInputStream in =new FileInputStream("D:\\test\\abc\\tt1.txt");
byte[] b = new byte[10];//设置一个byte数组去接收in里的文件内容
int len=0;//设置一个读取的长度
//in.read(b);//in.read方法有一个返回值,
//返回值是读取的数据长度,如果读取到最后一个,
//还会向后再读一个,返回值就是-1
//当返回值为-1时,结束读取
while ((len=in.read(b)) != -1) {
System.out.println(new String(b, 0, len));
//一共三个参数
//new String(b, 0, len)
//b是缓冲数据数组,0从数组的哪个位置开始转化字符串,总共转化了几个字节
}
//System.out.println(new String(b));
in.close();//注意:流在使用完之后一定要关闭
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
文件字节输出流
public static void testFileoutputStream() {
try {
FileOutputStream out = new FileOutputStream("D:\\test\\abc\\tt4.txt");
//指定向tt4输出数据
String str ="i am no.1";
out.write(str.getBytes());//把数据写到内存当中
out.flush();//再把内存中的数据写到硬盘上
out.close();
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
文件字节流练习
编写一个程序,把一个文件复制的到指定文件夹下
注意:文件字节流非常通用,可以用来操作字符的文档,还可以操作任何的其他类型文件(图片,压缩包等等),引用字节流直接使用二进制
public static void copyFile(String inpath,String outpath) {
try {
FileInputStream in =new FileInputStream(inpath);//读取源文件路径
//读取的源文件
FileOutputStream out = new FileOutputStream(outpath);//复制到哪里
//复制到路径
byte[] b= new byte[100];
int len = 0;
while((len=in.read(b))!=-1) {
out.write(b, 0, len);
//参数1是写的缓冲数组,参数2是数组的起始位置,参数3获取数组的总长度
}
out.flush();
//把写到内存上的数据,传入到硬盘上
out.close();
in.close();
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
文件字符输入流
读取文件操作步骤:
1.建立一个流对象,将已存在的一个文件加载进流。
FileReader fr = new FileReader(“Test.txt”);
2.创建一个临时存放数据的数组。
char[] ch = new char[1024];
3.调用流对象的读取方法将流中的数据读入到数组中。
fr.read(ch);
public static void testFileReader(String inPath) {
try {
FileReader fr = new FileReader(inPath);//创建字符输入流的对象
char[] c=new char[10];//创建临时存数据的字符数组
int len=0;//定义一个输入流的读取长度
while ((len=fr.read(c))!=-1) {
System.out.println(new String(c,0,len));
}
fr.close();//关闭流
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
文件字符输出流
写入文件步骤
1.创建流对象,建立数据存放文件
FileWriter fw = new FileWriter*(“Test.txt”);
2.调用流对象的写入方法,将数据写入流
fw.write(“text”);
2.1 输出流关闭之前需要清空缓存
fw.flush();
3.关闭流资源,并将流中的数据清空到文件中。
fw.close();
public static void testFileWriter(String text,String outPath) {
try {
FileWriter fw=new FileWriter(outPath);
fw.write(text);//把text写到内存中
fw.flush();//把内存中的字符串刷到硬盘上
fw.close();//关闭流
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
文件字符流复制
public static void copyFile(String inPath,String outPath) {
try {
FileReader fr=new FileReader(inPath);
FileWriter fw=new FileWriter(outPath);
char[] c= new char[100];
int len = 0;
while ((len = fr.read(c))!=-1) {
fw.write(c,0,len);
}
fw.flush();
fw.close();
fr.close();
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
注 意
定义文件路径时,注意:可以用“/”或者“\”。
在写入一个文件时,如果目录下有同名文件将被覆盖。
在读取文件时,必须保证该文件已存在,否则出异常。
FileInputStream,FileOutputStream,FileReader,FileWriter
这些都是计算机与硬盘之间发生的io操作,基于硬盘的读写相对比较慢,这个操作的的速度收到硬盘的读写速度制约,为了能够提供读写速度,一定程度上绕过硬盘的限制,java提供了一种缓冲流来实现。
处理流之一:缓冲流
为了提高数据读写的速度,Java API提供了带缓冲功能的流类,在使用这些流类时,会创建一个内部缓冲区数组
根据数据操作单位可以把缓冲流分为:
BufferedInputStream 和 BufferedOutputStream
BufferedReader 和 BufferedWriter
缓冲流要“套接”在相应的节点流之上,对读写的数据提供了缓冲的功能,提高了读写的效率,同时增加了一些新的方法
对于输出的缓冲流,写出的数据会先在内存中缓存,使用flush()将会使内存中的数据立刻写出
缓冲流就是先把数据缓冲到内存中,在内存中去做io操作,基于内存的io操作大概能比基于硬盘的io操作快75000多倍。
缓冲字节输入流
public static void testBufferedInputStream()throws Exception {
//文件字节输入流对象
FileInputStream in=new FileInputStream("F:\\eclipse-workspace\\IO\\src\\io\\tt.txt");
//缓冲字节输入流对象
BufferedInputStream br=new BufferedInputStream(in);
byte[] b =new byte[10];
int len = 0;
while((len=br.read(b))!=-1) {
System.out.println(new String(b, 0, len));
}
//关闭流的时候,本着一个晚开的最早关,依次关
br.close();
in.close();
}
缓冲字节输出流
public static void testBufferOutputStream() throws Exception {
//创建字节输出流对象
FileOutputStream out = new FileOutputStream("F:\\eclipse-workspace\\IO\\src\\io\\tt1.txt");
BufferedOutputStream bo = new BufferedOutputStream(out);
String s="hello world";
bo.write(s.getBytes());//写到内存中
bo.flush();
//关闭流的时候,本着一个晚开的最早关,依次关
bo.close();
out.close();
}
缓冲字节复制
public static void copyFile() throws Exception{
//缓冲输入流
BufferedInputStream br=new BufferedInputStream(new FileInputStream("F:\\eclipse-workspace\\IO\\src\\io\\tt1.txt"));
//缓冲输出流
BufferedOutputStream bo = new BufferedOutputStream(new FileOutputStream("F:\\eclipse-workspace\\IO\\src\\io\\tt2.txt"));
byte[] b = new byte[1024];
int len = 0;//设置一个每次读取到的数据长度,直到br.read方法执行到最后
//(比如说文件中只有hello world,所谓的执行到最后一个d的后面,这个时候的返回值就是-1)
while((len = br.read(b))!=-1) {
bo.write(b, 0, len);//写到内存中
}
bo.flush();
bo.close();
br.close();
}
缓冲字符输入流
public static void testBufferReader() throws Exception{
FileReader r =new FileReader("F:\\eclipse-workspace\\IO\\src\\io\\tt.txt");
BufferedReader br = new BufferedReader(r);
char[] c =new char[100];
int len=0;
while ((len = br.read(c))!=-1) {
System.out.println(new String(c, 0, len));
}
br.close();
r.close();
}
缓冲字符输出流
public static void testBufferWritter()throws Exception {
FileWriter fw=new FileWriter("F:\\eclipse-workspace\\IO\\src\\io\\tt3.txt");
BufferedWriter bw= new BufferedWriter(fw);
String s="hello World!!!";
bw.write(s);
bw.flush();
bw.close();
fw.close();
}
处理流之二:转换流
转换流提供了在字节流和字符流之间的转换
Java API提供了两个转换流:
InputStreamReader和OutputStreamWriter
字节流中的数据都是字符时,转成字符流操作更高效。
InputStreamReader
用于将字节流中读取到的字节按指定字符集解码成字符。需要和InputStream“套接”。
构造方法
? InputStreamReader(InputStream in)
public InputSreamReader(InputStream in,String charsetName)
public static void testInputStreamReader() throws Exception{
FileInputStream fs=new FileInputStream("F:\\eclipse-workspace\\IO\\src\\io\\tt5.txt");
//把字节流转换为字符流
InputStreamReader in=new InputStreamReader(fs, "UTF-8");//参数1是字节流,参数2是编码
char[] c=new char[100];
int len=0;
while ((len=in.read(c))!=-1) {
System.out.println(new String(c,0,len));
}
in.close();
fs.close();
}
OutputStreamWriter
用于将要写入到字节流中的字符按指定字符集编码成字节。 需要和OutputStream“套接”。
构造方法
public OutputStreamWriter(OutputStream out)
public OutputSreamWriter(OutputStream out,String charsetName)*
public static void testOutputStreamWriter() throws Exception{
FileOutputStream out=new FileOutputStream("F:\\eclipse-workspace\\IO\\src\\io\\tt6.txt");
OutputStreamWriter os=new OutputStreamWriter(out,"UTF-8");
os.write("你好你好");
os.flush();
os.close();
out.close();
}
处理流之三:标准输入输出流
System.in和System.out分别代表了系统标准的输入和输出设备
默认输入设备是键盘,输出设备是显示器
System.in的类型是InputStream
System.out的类型是PrintStream,其是OutputStream的子类FilterOutputStream 的子类
通过System类的setIn,setOut方法对默认设备进行改变。
public static void setIn(InputStream in)
public static void setOut(PrintStream out)
public static void testSystemIn() throws Exception{
//获取一个接收键盘输入数据的输入流
InputStreamReader is= new InputStreamReader(System.in);
//把输入流放到缓冲流里
BufferedReader br = new BufferedReader(is);
String str ="";//定义一个接收临时数据的字符串
while((str=br.readLine())!=null) {
System.out.println(str);
}
br.close();
is.close();
}
把控制台写的内容写到指定的txt文件中,当接受到字符串over,就结束程序的运行
public static void write2TXT() throws Exception{
InputStreamReader is = new InputStreamReader(System.in);
//创建对象去接收键盘的输入
BufferedReader br = new BufferedReader(is);
//把输入流缓冲到内存中
BufferedWriter bw = new BufferedWriter(new FileWriter("F:\\eclipse-workspace\\IO\\src\\io\\tt7.txt"));
String str = "";
while((str = br.readLine()) != null) {
//读取每一行都写到指定的TXT文件中
if (str.equals("over")) {
break;
}
bw.write(str);
}
bw.flush();
bw.close();
br.close();
is.close();
}
处理流之四:打印流
在整个IO包中,打印流是输出信息最方便的类。
PrintStream(字节打印流****)和PrintWriter(字符打印流****)提供了一系列重载的print和println方法,用于多种数据类型的输出
PrintStream和PrintWriter的输出不会抛出异常
PrintStream和PrintWriter有自动flush功能
System.out返回的是PrintStream的实例
处理流之五:数据流
为了方便地操作Java语言的基本数据类型的数据,可以使用数据流。
数据流有两个类:(用于读取和写出基本数据类型的数据)
DataInputStream 和 DataOutputStream
分别“套接”在 InputStream 和 OutputStream 节点流上
DataInputStream 中的方法
? boolean readBoolean() byte readByte()
? char readChar() float readFloat()
? double readDouble() short readShort()
? long readLong() int readInt()
? String readUTF() void readFully(byte[] b)
? DataOutputStream 中的方法
将上述的方法的read改为相应的write即可。
/**
* 数据输出流
* 用数据输出流写到文件中的基本数据类型,是乱码的,不能直接辨认出
* 需要数据输入流来读取
* 用数据输入流读取数据输出流写到文件中的数据时,要保证使用和当时写的数据类型一致的类型来读取
* 也就是说,写的时候是Writedouble,读的时候就得是Readdouble
* DataOutputStream
*/
public static void testDataOutputStream() throws Exception{
DataOutputStream dos = new DataOutputStream(new FileOutputStream("F:\\eclipse-workspace\\IO\\src\\io\\tt8.txt"));
dos.writeBoolean(true);
// dos.writeDouble(1.35);
// dos.writeInt(100);
dos.flush();
dos.close();
}
/**
* 数据输入流
* DataInputStream
* 用数据输出流写到文件中的基本数据类型,是乱码的,不能直接辨认出
* 需要数据输入流来读取
*/
private static void testDataInputStream()throws Exception {
DataInputStream ps = new DataInputStream(new FileInputStream("F:\\eclipse-workspace\\IO\\src\\io\\tt8.txt"));
System.out.println(ps.readBoolean());
ps.close();
}
处理流之六:对象流
对象流的目的是
1、假如有一个对象,把这个对象存到电脑硬盘里,硬盘的基础是什么?二进制,呢就需要把对象转化为二进制的字节流,把这个流保存到电脑上。
要使用这个对象,得把流转换为对象再使用。
2、把这个对象通过网络传到另一条机器上,网络通信的基础是二进制,也需要把一个对象转换为二进制的数据流,把这个流通过网络进行传输。
在接受者如果要使用接收的对象得先把对象的流转换为对象才行。
正是因为保存对象到硬盘(对象的持久化)和对象的网络传输,需要做两件事,就产生了对象的输入流和输出流。
ObjectInputStream和OjbectOutputSteam
用于存储和读取对象的处理流。它的强大之处就是可以把Java中的对象写入到数据源中,也能把对象从数据源中还原回来。
序列化(Serialize):用ObjectOutputStream**类将一个Java对象写入IO流中
反序列化 (Deserialize):用ObjectInputStream类从IO流中恢复该Java对象
ObjectOutputStream和ObjectInputStream不能序列化static和transient修饰的成员变量
序列化和反序列化都是针对的是对象的各种属性,不包括类的属性。
对象的序列化
对象序列化机制允许把内存中的Java对象转换成平台无关的二进制流,从而允许把这种二进制流持久地保存在磁盘上,或通过网络将这种二进制流传输到另一个网络节点。当其它程序获取了这种二进制流,就可以恢复成原来的Java对象
序列化的好处在于可将任何实现了Serializable接口的对象转化为字节数据,使其在保存和传输时可被还原
序列化是 RMI(Remote Method Invoke – 远程方法调用)过程的参数和返回值都必须实现的机制,而 RMI 是 JavaEE 的基础。因此序列化机制是 JavaEE 平台的基础
如果需要让某个对象支持序列化机制,则必须让其类是可序列化的,为了让某个类是可序列化的,该类必须实现如下两个接口之一:
Serializable
? 凡是实现Serializable接口的类都有一个表示序列化版本标识符的静态变量:
private static final long serialVersionUID;
serialVersionUID用来表明类的不同版本间的兼容性
如果类没有显示定义这个静态变量,它的值是Java运行时环境根据类的内部细节自动生成的。若类的源代码作了修改,serialVersionUID 可能发生变化。故建议,显示声明
显示定义serialVersionUID的用途
希望类的不同版本对序列化兼容,因此需确保类的不同版本具有相同的serialVersionUID
不希望类的不同版本对序列化兼容,因此需确保类的不同版本具有不同的serialVersionUID
若某个类实现了 Serializable 接口,该类的对象就是可序列化的:
创建一个 ObjectOutputStream
调用 ObjectOutputStream 对象的 writeObject(对象) 方法输出可序列化对象。注意写出一次,操作****flush()
反序列化
创建一个 ObjectInputStream
调用 readObject() 方法读取流中的对象
强调:如果某个类的字段不是基本数据类型或 String 类型,而是另一个引用类型,那么这个引用类型必须是可序列化的,否则拥有该类型的 Field 的类也不能序列化
/**
* 对象的序列化
*/
public static void testSerialize() throws Exception{
//定义对象的输出流,把对象的序列化之后的流放到指定的文件中
ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream("F:\\eclipse-workspace\\IO\\src\\io\\tt9.txt"));
Person person = new Person();
person.name="张三";
person.age=11;
out.writeObject(person);
out.flush();//刷写数据到硬盘
out.close();
}
public static void testDeSerialize()throws Exception {
//创建对象输入流,从指定的文件中把序列化的流读取出来
ObjectInputStream in = new ObjectInputStream(new FileInputStream("F:\\eclipse-workspace\\IO\\src\\io\\tt9.txt"));
Object object=in.readObject();
Person p = (Person)object;
System.out.println(p.name);
System.out.println(p.age);
}
Externalizable
RandomAccessFile 类
RandomAccessFile 类支持 “随机访问” 的方式,程序可以直接跳到文件的任意 地方来读、写文件
支持只访问文件的部分内容
可以向已存在的文件后追加内容
RandomAccessFile 对象包含一个记录指针,用以标示当前读写处的位置。 RandomAccessFile 类对象可以自由移动记录指针:
long getFilePointer():获取文件记录指针的当前位置
void seek(long pos):将文件记录指针定位到 pos 位置
构造器
public RandomAccessFile(File file, String mode)
public RandomAccessFile(String name, String mode)
创建 RandomAccessFile 类实例需要指定一个 mode 参数,该参数指定 RandomAccessFile 的访问模式:
r: 以只读方式打开
rw:打开以便读取和写入**
rwd:打开以便读取和写入;同步文件内容的更新
rws:打开以便读取和写入;同步文件内容和元数据的更新
读取文件内容
public static void testRandomAccessFileRead() throws Exception{
//RandomAcessFile的构造有两个路径,参数1是读写文件的路径,
//参数二是指定RandomAcessFile的访问模式
/*
* **r:** **以只读方式打开**
**rw****:打开以便读取和写入**
**rwd****:****打开以便读取和写入;同步文件内容的更新**
**rws****:****打开以便读取和写入;同步文件内容和元数据的更新**
*/
//最常用的是r和rw
RandomAccessFile ra=new RandomAccessFile("F:\\eclipse-workspace\\IO\\src\\io\\tt10.txt","r");
ra.seek(5);//通过设置读取文件内容的起始点,来达到从文件的任意位置读取
byte[] b=new byte[1024];
int len=0;
while ((len=ra.read(b))!=-1) {
System.out.println(new String(b, 0, len));
}
ra.close();
}
写入文件内容
public static void testRandomAcessFileWrite() throws Exception{
RandomAccessFile ra= new RandomAccessFile("F:\\eclipse-workspace\\IO\\src\\io\\tt10.txt","rw");
// ra.seek(6);//设置写的起始点
//0代表从开头开始写
ra.seek(ra.length());
//ra.length代表从文件的追加开始写
ra.write("你好".getBytes());
ra.close();
}
流的基本应用小节
流是用来处理数据的。
-
处理数据时,一定要先明确数据源,与数据目的地
数据源可以是文件,可以是键盘。
数据目的地可以是文件、显示器或者其他设备。
-
而流只是在帮助数据进行传输,并对传输的数据进行处理,比如过滤处理、转换处理等。
字节流-缓冲流(重点)
? 输入流InputStream-FileInputStream-BufferedInputStream
? 输出流OutputStream-FileOutputStream-BufferedOutputStream
字符流-缓冲流(重点)
? 输入流Reader-FileReader-BufferedReader
? 输出流Writer-FileWriter-BufferedWriter
转换流
? InputSteamReader和OutputStreamWriter
对象流ObjectInputStream和ObjectOutputStream(难点)
? 序列化
? 反序列化
随机存取流RandomAccessFile(掌握读取、写入)