您的位置 首页 java

Java中的File类和IO流

Flie类

1.什么是File类

File类就是当前系统中,文件或文件夹的抽象表示。

通过使用 File 类的实例对象,我们就可以通过代码实现计算机的文件控制,即文件的增删改查等操作。

2.File类的使用

首先为了保证其他文件的安全性,建议先建立一个Test文件夹专门用于File对象的联系。

public static void main(String[] args) throws IO Exception {

File f1 = new File(“D:/AAAkejian/作业/test”);

File f2 = new File(“D:\\AAAkejian\\作业\\test1”);

f1. mkdir ();

f2.mkdir();

f1 = new File(“D:/AAAkejian/作业/test/test.txt”);

f1.createNewFile();

File f3 = new File(“D:”+File.separator+

“AAAkejian”+File.separator+

“作业”+File.separator+”test1”);

}

其中:

D:/AAAkejian/作业/test,代表路径名,“ / ”和“\”都表示路径分隔符;

在windows系统下,两种分隔符都支持,在 Linux 和Mac系统下只支持“ / ”

使用File.separator,表示动态分隔符。但在日常开发中,由于windows系统中两者都支持,而且在使用“ \ ”时,还需要考虑 转义字符 ,因此我们常用的路径分隔符为“ / ”;

3.File对象的使用

1)创建目录或文件

File f1 = new File(“D:/AAAkejian/作业/test”);

//创建目录

f1.mkdir();

f1 = new File(“D:/AAAkejian/作业/test/test.txt”);

//创建文件

f1.createNewFile();

f1 = new File(“D:/AAAkejian/作业/test/test2”);

//创建多级目录

f1.mkdirs();

2)删除目录或文件

//删除目录或文件

f1.delete();

//当程序结束时再进行删除

f2.deleteOnExit();

3)修改目录或文件

IO流

1.什么是 IO

IO分别是两个单词的首字母缩写,I: InputStream 输入流 O: OutputStream 输出流

2.IO流的作用:对文件中的内容进行操作;

输入:读操作(读取文件的内容) 输出:写操作(向文件中写内容)

这里的输入输出是针对 Java 程序而言,从文件中读取内容到Java程序即为输入;

同理,从Java程序向文件中写入内容就叫输出。

3.IO流的方向

1)根据流的方向

输入流:程序可以从中读取数据的流

输出流:程序可以从中写入数据的流

2)根据流的单位

字节流:以字节为单位传输数据的流

字符流:以字符为单位传输数据的流

3)根据功能

节点流:直接和文件进行交互

处理流:不直接作用在文件上

IO流中有(字节输入/输出流、字符输入/输出流)四个基本流,其他的流都是再这四个流的基础上进行拓展的

4.Writer字符输出流

1)Writer类是所有字符输出流的跟类

2)使用Writer向文件中添加内容

public static void main(String[] args) throws IOException {

Writer w1 = new FileWriter(“D:/AAAkejian/WriterTest/test2.txt”);

String str = “这是第二遍练习”;

w1.write(str);

//该方式则可以进行添加而不覆盖

Writer w2 = new FileWriter(“D:/AAAkejian/WriterTest/test2.txt”,true);

str = “这是添加的内容”;

w2.write(str);

//append也可用于添加

w1.append(str);

//刷新流

w1.flush();

//关闭流

w1.close();

w2.flush();

w2.close();

}

3)使用Writer直接向文件内添加内容时,后添加的会覆盖先添加的内容,那么在进行内容追加时就需要添加上一个true,表示允许追加内容到文件中。见上图

5.Reader字符输入流

1)Reader类是所有字符输入流的跟类

2)使用FileReader实现类进行文件的读取操作:

public static void main(String[] args) throws IOException {

Reader reader = new FileReader(“D:/AAAkejian/ReaderTest/test1.txt”);

int count = 0;

char[] cList = new char[10];

while( (count=reader.read(cList)) !=-1 ){

String str=new String(cList,0,count);

System.out.print(str);

}

}

6,结合输入输出流可以实现文件的复制功能

public void test1() throws IOException {

//创建字符输入流

FileReader fr = new FileReader(“D:/AAAkejian/Test/test1.txt”);

//创建字符输出流

FileWriter fw = new FileWriter(“D:/AAAkejian/Test/test2.txt”);

//记录读取到的个数

int count = 0;

//每次读取的内容放入这个数组中

char[] cList = new char[10];

while ((count = fr.read(cList))!=-1){

fw.write(cList,0,count);

fw.flush();

}

fw.close();

fr.close();

}

这里需要注意的是,字符流只适用于文本的输入输出,对于图片,视频等二进制文件则无法使用字符流进行操作。

7.字节流

1)字节输出流——OutputStream(所有字节输出流的父类)

字节输出流的使用,以 FileOutputStream 为例

public void test1() throws Exception {

//定义字节输出流对象

OutputStream osp = new FileOutputStream(“D:/AAAkejian/Test/test1.txt”);

String str = “Add abcd”;

//将 字符串 转化为字节存入字节数组中

byte [] bList = str.getBytes();

//写入文件

osp.write(bList);

//刷新流

osp.flush();

//关闭流

osp.close();

}

2)字节输入流——InputStream(所有字节输入流的父类)

字节输入流的使用,以 FileInputStream 为例,这里的读取规则,与字符输入流类似,利用循环进行循环读取文件内容。

public void test() throws Exception{

//创建字节输入流对象

InputStream ips = new FileInputStream(“D:/AAAkejian/Test/test1.txt”);

byte[] bList = new byte[3000];

int count = 0;

while( (count=ips.read(bList))!=-1 ){

//把byte数组转换为字符串

String str=new String(bList,0,count);

System.out.println(str);

}

ips.close();

}

3)利用字节输入输出流完成图片的复制功能

public void test()throws Exception{

//定义字节输入流对象

InputStream ips = new FileInputStream(“D:/AAAkejian/Test/test3.png”);

//定义字节输出流对象

OutputStream ops = new FileOutputStream(“D:/AAAkejian/Test/test1.png”);

//定义字节数组,用于存储所读取的内容

byte[] bList = new byte[100];

int count =0;

while ((count = ips.read(bList))!=-1){

ops.write(bList,0,count);

ops.flush();

}

ops.close();

ips.close();

}

8.缓存流

缓存流是在基础流(InputStream OutputStream Reader Writer)之上,添加了一个缓冲池的功能,用于提高IO效率,降低IO次数

缓冲流的使用:

public void test()throws Exception{

//定义字节输出流

OutputStream ops = new FileOutputStream(“D:/AAAkejian/Test/test1.txt”);

//定义缓存流对象

BufferedOutputStream bfops = new BufferedOutputStream(ops);

String str = “new content”;

byte[] bList = str.getBytes();

//此时的内容在缓冲池中,并未写入文件

bfops.write(bList);

//刷新缓冲池,将缓冲池中的内容写入文件中

//bfops.flush();

//h缓存流中的close方法,会先执行flush方法

bfops.close();

}

9.对象流

对象流的意义在于数据的持久化,例如游戏存到,就是一种对象流的使用。

在日常程序运行中,内容都是存储在内存中的,而将内存中的数据,存储到磁盘中,就实现了数据的持久化。

1)对象流输出的使用——ObjectOutputStream–序列化(存档):

public class Role implements Serializable {

private String name;

private int level;

private String power;

public Role(String name, int level, String power) {

this.name = name;

this.level = level;

this.power = power;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getLevel() {

return level;

}

public void setLevel(int level) {

this.level = level;

}

public String getPower() {

return power;

}

public void setPower(String power) {

this.power = power;

}

}

public void test()throws Exception{

OutputStream ops = new FileOutputStream(“d:/AAAkejian/Test/test3.txt”);

ObjectOutputStream oops = new ObjectOutputStream(ops);

//使用对象流调用输出流的输出方法,被输出的对象的类必须实现 Serializable接口

Role r1 = new Role(“gjx”,55,”撒泼打滚”);

oops.writeObject(r1);

oops.close();

}

2)对象输入流的使用——ObjectInputStream–反序列化(读档):

public void test()throws Exception{

InputStream ips = new FileInputStream(“D:/AAAkejian/Test/test3.txt”);

ObjectInputStream oips = new ObjectInputStream(ips);

Object o = oips.readObject();

System.out.println(o);

oips.close();

}

文章来源:智云一二三科技

文章标题:Java中的File类和IO流

文章地址:https://www.zhihuclub.com/177595.shtml

关于作者: 智云科技

热门文章

网站地图