输入输出类
File类
创建文件对象==>操作文件或目录的属性
File file = new File(String pathname); c:\\test.txt 或 c:/test.txt
构造方法:
File(File parent, String child) 根据 parent 抽象路径名和 child 路径名字符串创建一个新 File 实例。
File(String pathname) 通过将给定路径名字符串转换为抽象路径名来创建一个新 File 实例
File(String parent, String child) 根据 parent 路径名字符串和 child 路径名字符串创建一个新 File 实例
常用方法
boolean exists() 判断文件或目录是否存在
boolean isFile() 判断是否是文件
boolean isDirectory() 判断是否是目录
String getPath() 返回此对象表示的文件的相对路径名
String getAbsolutePath() 返回此对象表示的文件的绝对路径名
String getName() 返回此对象表示的文件或目录的名称
boolean delete() 删除此对象指定的文件或目录 //删除文件夹必须先删除文件夹内所有文件(文件夹)
boolean createNewFile() 创建名称的空文件,不创建文件夹
long length() 返回文件的长度,单位为字节,如果文件不存在,则返回0L
String[] list() 返回 文件或目录的字符串数组
boolean mkdir() 创建此抽象路径名指定的目录。路径创建
boolean mkdirs() 创建此抽象路径名指定的目录,包括所有必需但不存在的父目录
File getParentFile() 返回此抽象路径名父目录的抽象路径名;如果此路径名没有指定父目录,则返回 null。
===>如:File file = new File("C:/temp/text.txt"); file.getParentFile() -->返回C:/temp
通过流来读写文件
流是一组有序的数据序列
以先进先出方式发送信息的通道
按流向区分
程序 读==>输入流 InputStream 和 Reader 作为基类
程序 写==>输出流 OutputStream 和 Writer 作为基类
输入输出流是相对于计算机内存来说的
字节输入流InputStream
字节流
按照处理数据单元划分 字节输出流OutputStream
字符输入流Reader
字符流
字符输出流Writer
字节流是8位通用字节流,字符流是16位Unicode字符流
InputStream常用方法
int read()
从输入流中读取数据的下一个字节。返回 0 到 255 范围内的 int 字节值。如果因为已经到达流末尾而没有可用的字节,则返回值 -1。
在输入数据可用、检测到流末尾或者抛出异常前,此方法一直阻塞。
int read(byte[] b)
从输入流中读取一定数量的字节,并将其存储在缓冲区数组 b 中。以整数形式返回实际读取的字节数。
在输入数据可用、检测到文件末尾或者抛出异常前,此方法一直阻塞。
如果 b 的长度为 0,则不读取任何字节并返回 0;否则,尝试读取至少一个字节。
如果因为流位于文件末尾而没有可用的字节,则返回值 -1;否则,至少读取一个字节并将其存储在 b 中。
将读取的第一个字节存储在元素 b[0] 中,下一个存储在 b[1] 中,依次类推。读取的字节数最多等于 b 的长度。
设 k 为实际读取的字节数;这些字节将存储在 b[0] 到 b[k-1] 的元素中,不影响 b[k] 到 b[b.length-1] 的元素。
int read(byte[] b, int off, int len)
将输入流中最多 len 个数据字节读入 byte 数组。尝试读取 len 个字节,但读取的字节也可能小于该值。
以整数形式返回实际读取的字节数。
在输入数据可用、检测到流末尾或者抛出异常前,此方法一直阻塞。
如果 len 为 0,则不读取任何字节并返回 0;否则,尝试读取至少一个字节。如果因为流位于文件末尾而没有可用的字节,则返回值 -1;
否则,至少读取一个字节并将其存储在 b 中。
将读取的第一个字节存储在元素 b[off] 中,下一个存储在 b[off+1] 中,依次类推。读取的字节数最多等于 len。
设 k 为实际读取的字节数;这些字节将存储在 b[off] 到 b[off+k-1] 的元素中,不影响 b[off+k] 到 b[off+len-1] 的元素。
在任何情况下,b[0] 到 b[off] 的元素以及 b[off+len] 到 b[b.length-1] 的元素都不会受到影响。
void close()
关闭此输入流并释放与该流关联的所有系统资源
int available()
返回此输入流下一个方法调用可以不受阻塞地从此输入流读取(或跳过)的估计字节数
可以从输入流中读取的字节数目
子类FileInputStream
引入相关的类-->构造文件输入流FileInputStream对象-->读取文本文件的数据-->关闭文件流对象
java.io.IOException ---> FileInputStream fis = new FileInputStream("d:\\text.txt");-->fis.available() / fis.read(); -->fis.close();
java.io.FileInputStream
OutputStream
void write(int c)
void write(byte[] buf)
void write(byte[] b, int off, int len)
void close()
void flush():强制把缓冲区的数据写到输出流中
int count = 0; byte[] arr = new byte[1024]; while ((count=fis.read(arr))!=-1){ fos.write(arr,0,count); } //使用数组读取速度更快??
子类FileOutputStream
FileOutputStream(File file)
FileOutputStream(String name)
FileOutputStream(String name, boolean append)
注意:1,前两种构造方法在向文件写数据时将覆盖文件中原有的内容
2,创建FileOutputStream实例时,如果相应的文件并不存在,则会自动创建一个空的文件
==> FileOutputStream fos = new FileOutputStream("test.txt",true); //后加true即是添加
FileOutputStream fos = new FileOutputStream("test.txt");
String str = "好好学习java";
byte[] strs = str.getBytes();
fos.write(strs,0,strs.length);
字符流
Reader类
int read()
int read(char[] c)
read(char[] c, int off, int len)
void close()
子类InputStreamReader常用的构造方法
InputStreamReader(InputStream in) -->给一个字节流,包装成字符流
InputStreamReader(InputStream in, String charsetName)
FileReader是InputStreamReader的子类
FileReader(File file)
FileReader(String pathName)
只能按照本地平台的字符编码来读,不能通过用户特定的字符编码来读
本地平台字符编码方法
String str = System.getProperty("file.encoding");
System.out.println(str);
使用FileReader读取文件
//引入相关类
//创建FileReader对象
Reader fr = new FileReader("F:\\test\\java.txt");
//读取文本文件
fr.read();
//关闭相关的流对象
fr.close();
Reader reader = null; try { reader = new FileReader("F:\\test\\java.txt"); char[] arr = new char[1024]; StringBuffer stringBuffer = new StringBuffer(); while((reader.read(arr))!=-1){ stringBuffer.append(arr); } System.out.println(stringBuffer); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ try { reader.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
BufferedReader : 带有缓冲区德字符输入流
构造方法:
//BufferedReader(Reader in) Reader re = null; BufferedReader bReader = null; try { re = new FileReader("F:\\test\\java.txt"); bReader = new BufferedReader(re); String line = null; while((line=bReader.readLine())!=null){ System.out.println(line); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ try { bReader.close(); re.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
/*
* 解决中文乱码
* 修改读取文件的格式
* InputStream inputStream = new FileInputStream("F:\\test\\pet.template");
* Reader reader = new InputStreamReader(inputStream,"UTF-8");
* BufferedReader bReader = new BufferedReader(reader);
* */
Writer()类
方法:
write(String)
write(String,int,int)
write(char[],int off,int len)
子类OutputStreamReader
OutputStreamWriter(OutputStream out)
OutputStreamWriter(OutputStream out,String charsetName)
OutputStreamReader子类FileWriter
FileWriter 构造方法
FileWriter(File file)
FileWriter(String string)
Writer writer = null; try { writer = new FileWriter("F:\\test\\java.txt",true); //写入文本文件 writer.write("hellword"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ try { writer.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
BufferWriter 是Writer类子类
BufferWriter(Witer out)
Writer fw = null;
BufferedWriter bWriter = null; try { fw = new FileWriter("F:\\test\\java.txt",true); bWriter = new BufferedWriter(fw); //写入文本文件 bWriter.write("我爱北京"); bWriter.newLine(); bWriter.write("三大框架龙鞥"); bWriter.flush(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ try { bWriter.close(); fw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
字节流和字符流
读写二进制文件(图片\音频等)
DataInputStream类
FileInputStream的子类
DataOutputStream类
FileOutputStream的子类
序列化 将对象的状态写入到特定的流中的过程
1.实现Serializable接口
2.创建对象输出流 ObjectOutputStream
3.调用writeObject()方法将对象写入文件
4.关闭对象输出流
反序列化 从特定的流中获取数据重写构建对象的过程
1.实现Serializable接口
2.创建对象输入流 ObjectInputStream
3.调用readObject()方法将对象写入文件
4.关闭对象输出流