java ini类_java中IO类的各种操作

java的IO类操作主要包括如下几类

(1)、File类的使用。

(2)、字节操作流:OutputStream、InputStream

(3)、字符操作流:Reader、Writer

(4)、对象序列化:serializable

1)File类

Java代码

[java] view plaincopyprint?

public class File extends Object implements Serizliable Comparable

从定义看,File类是Object的直接子类,同时它继承了Comparable接口可以进行数组的排序。

File类的操作包括文件的创建、删除、重命名、得到路径、创建时间等,以下是文件操作常用的函数。

061013cf20189a19a79cc87763b3be08.png

File类的操作:

(1)创建文件,注意File.separator可以解决跨操作系统的问题。

下面的例子是一创建一个文件,如果该文件存在则删除,否则创建该文件。

Java代码

public class FileDemo1 {

public static void main(String[] args) {

File file = new File("D:" + File.separator + "test.txt");

if (file.exists()) {

file.delete();

} else {

try {

file.createNewFile();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

(2)文件的类型函数

file.isFile(); //判断是不是文件

file.isDirectory();//判断是不是目录

(3)列出目录的内容

pulbic String[] list();//列出所有文件名和目录名

public File[] listFiles();//列出所有文件和目录

2)、字节操作流(btyle)

(1)字节输出流OutputStream

30a2059660da3f2cb0cd4ae6fbfa4c76.png

Java代码

public class FileDemo1 {

public static void main(String[] args) {

File file = new File("D:" + File.separator + "test.txt");//指定要操作的文件

OutputStream out=null;//定义字节流输出对象

try {

//out= new FileOutputStream(file,true);//是否字节追加函数

out= new FileOutputStream(file);//获取实际的字节流输出对象,内容覆盖

} catch (FileNotFoundException e) {

e.printStackTrace();

}

String info="hello";//要输入的内容

byte[] b=info.getBytes();//将字符转化为字节数组

try {

out.write(b);

} catch (IOException e) {

e.printStackTrace();

}

try {

out.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

(2)字节输入流InputStream

c31714ef203172e437c2fb61a7699524.png

Java代码

public class FileDemo1 {

public static void main(String[] args) {

File file = new File("D:" + File.separator + "test.txt");//指定要操作的文件

InputStream In=null;//定义字节流输入对象

try {

//out= new FileOutputStream(file,true);//是否字节追加函数

In= new FileInputStream(file);//获取实际的字节流输入对象

} catch (FileNotFoundException e) {

e.printStackTrace();

}

int len=0;//输入数组长度

byte[] b=new byte[1024];//开辟空间,读取内容

//byte[] b=new byte[(int)file.length()];//根据文件大小开辟空间

try {

len=In.read(b);//读取

} catch (IOException e1) {

e1.printStackTrace();

}

try {

In.close();

} catch (IOException e) {

e.printStackTrace();

}

System.out.println(new String(b,0,len));

}

}

(3)字符输出流Write

cd2ce0a689448313fb6b13fad203278f.png

Java代码

public class FileDemo1 {

public static void main(String[] args) {

File file = new File("D:" + File.separator + "test.txt");// 指定要操作的文件

Writer write = null;// 定义字符输出流

try {

write = new FileWriter(file);

} catch (IOException e) {

e.printStackTrace();

}

String infor = "hello,heiehiehieh";

try {

write.write(infor);

} catch (IOException e) {

e.printStackTrace();

}

try {

write.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

(4)字符输入流Reader

a1e415b036d561b208d2f9d36dea1c5f.png

Java代码

public class FileDemo1 {

public static void main(String[] args) {

File file = new File("D:" + File.separator + "test.txt");// 指定要操作的文件

Reader read = null;// 定义字符输入流

try {

read = new FileReader(file);

} catch (IOException e) {

e.printStackTrace();

}

String infor = "hello,heiehiehieh";

char[] b=new char[1024];//设置字符的长度

try {

int len=read.read(b);

} catch (IOException e) {

e.printStackTrace();

}

try {

read.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

(5)字节流和字符流的区别(重点)

字节流没有缓冲区,是直接输出的,而字符流是输出到缓冲区的。因此在输出时,字节流不调用colse()方法时,信息已经输出了,而字符流只有在调用close()方法关闭缓冲区时,信息才输出。要想字符流在未关闭时输出信息,则需要手动调用flush()方法。

(6)转换流:在io中还存在一类是转换流,将字节流转换为字符流,同时可以将字符流转化为字节流。

OutputStreamWriter(OutStream out):j将字节流以字符流输出。

InputStreamReader(InputStream in):将字节流以字符流输入。

(7)打印流 PrintStream

在操作中要求输出信息时,可以采用PrintStream进行输出,它包括PrintWrite和PrintReader

3)对象序列化

对象序列化是指将一个对象可以转化为二进制的byte流,可以以文件的方式进行保存。

将对象保存在文件的操作叫做对象的序列化操作。

将对象从文件中恢复的操作叫做反序列化操作。

一个对象如果要能序列化,它必须继承Serizliable。在实现序列化是则需要ObjectOurputStream完成,而需要反序列化时则采用ObjectInputStream。

4727030f7b43c416ff0410288cd86ced.png

a23e2368e550873086dab2321d893510.png

transient关键字:变量声明为Transient后,该变量不可序列化。

4)内存流

在项目的开发过程中,有时希望只产生临时文件,将信息输出的内存中,此时会用到内存流,内存流基本方法如下:

3bfa37243945c0caa2ff6a63238fa2ce.png

Java代码

public class FileDemo1 {

public static void main(String[] args) {

String infor = "hello";

// 所有的内容向内存中输入

InputStream input = new ByteArrayInputStream(infor.getBytes());

// 所有内存的内容由outputStream输出

OutputStream out = new ByteArrayOutputStream();

int temp = 0;

try {

while ((temp = input.read()) != -1) {

char c = Character.toUpperCase((char) temp);

out.write(c);//从内存中输出,所有的内容都保存在ByteArrayOutputStream中

}

} catch (IOException e) {

e.printStackTrace();

}

try {

input.close();

} catch (IOException e) {

e.printStackTrace();

}

try {

out.close();

} catch (IOException e) {

e.printStackTrace();

}

System.out.println(out.toString());

}

}

5)System类对IO的支持

a93b507524bf7283a709a928aaf56f82.png

6)缓存读取

75f9d7a8cd232e260469c0780b54f191.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值