Java的IO类操作主要包括如下几类
1、File类的使用。
2、字节操作流:OutputStream、InputStream
3、字符操作流:Reader、Writer
4、对象序列化:serializable
(1)File类
从定义看,File类是Object的直接子类,同时它继承了Comparable接口可以进行数组的排序。
File类的操作包括文件的创建、删除、重命名、得到路径、创建时间等,以下是文件操作常用的函数。
File类的操作:
(1)创建文件,注意File.separator可以解决跨操作系统的问题。
下面的例子是一创建一个文件,如果该文件存在则删除,否则创建该文件。
- 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) {
-
- e.printStackTrace();
- }
- }
- }
- }
(2)文件的类型函数
file.isFile(); //判断是不是文件
file.isDirectory();//判断是不是目录
(3)列出目录的内容
pulbic String[] list();//列出所有文件名和目录名
public File[] listFiles();//列出所有文件和目录
(2)字节操作流(btyle)
(1)字节输出流OutputStream
- 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);
- } 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
- public class FileDemo1 {
- public static void main(String[] args) {
- File file = new File("D:" + File.separator + "test.txt");
- InputStream In=null;
- try {
-
- In= new FileInputStream(file);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- int len=0;
- byte[] b=new byte[1024];
-
- 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
- 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
- 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。
transient关键字:变量声明为Transient后,该变量不可序列化。
(4)内存流
在项目的开发过程中,有时希望只产生临时文件,将信息输出的内存中,此时会用到内存流,内存流基本方法如下:
- public class FileDemo1 {
- public static void main(String[] args) {
- String infor = "hello";
-
- InputStream input = new ByteArrayInputStream(infor.getBytes());
-
- OutputStream out = new ByteArrayOutputStream();
- int temp = 0;
- try {
- while ((temp = input.read()) != -1) {
- char c = Character.toUpperCase((char) temp);
- out.write(c);
- }
- } 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的支持
(6)缓存读取