1.在不同操作系统下的文件表达形式:
Windows: E:\javase\每日小结\test.txt(绝对路径文件名,全限定名)
Linux : /javase/每日小结/test.txt
相对路径:是相对某个目录来描述当前文件的路径。
绝对路径:是某个文件或目录的具体路径,可以根据路径找到文件的所在。
2.java.io.File类:真实文件或目录的抽象标识。在Java程序,操作File类的实例,就相当于操作与这个实例对应的 真实文件。
1)常量:separator,代表与操作系统相关的路径分隔符。 File.separator
2)访问文件属性的方法:
boolean exists();
int length();
long lastModified();
String getName(); //获取文件名(带扩展名)
String getAbsoultePath(); //绝对路径名
String getCanonicalPath() throws IOException; //规范的绝对路径名
3)文件操作相关的方法::
boolean createNewFile(); //创建为文件
boolean delete(); //删除
boolean mkdir(); //创建为目录,如果父目录不存在,则创建失败。
boolean mkdirs(); //创建为目录,并会创建必须不存在的父目录。
4)列举它的子文件和子目录:
String[] list(); //列举出当前目录下的子文件和子目录的绝对路径名,作为字符串数组返回。
File[] listFiles(); //列举出当前目录下的子文件和子目录,作为File类型数组返回。
3.流(Stream):数据通信的通道。程序跟外部设备进行数据交换的通道。
在Java程序中,要操作某个文件的内容,必须使用IO流。
4.IO流的分类:
1)按流向分:输入流(源文件-->程序)和输出流(程序-->目标文件)
2)按传输单位分:字符流(以字节为单位传输数据)和字符流(以Unicode字符为单位传输数据)
3)按功能分:节点流(直接操作目标设备)和过滤流(对节点流的包装,提供更强大的数据处理功能,也叫处理 流)
5.四个抽象流类:
1)InputStream和OutputStream
a)InputStream:字节输入流。常用的方法:
int read() throws IOException; //一次读取一个字节并返回,如果遇到文件尾,返回-1。
int read(byte[] b)throws IOException; //一次读取指定长度(b.length)的字节数据,存储到指定 的数组中,返回读取到的字节数。如果遇到文件尾,返 回-1。
void close() throws IOException; //流操作完之后,一定要关闭,以释放相关的系统资源。
b)OutputStream:字符输出流。常用方法:
void write(int b) throws IOException; //一次写出一个指定的字节。
void write(byte[] b) throws IOException; //一次写出指定数组中的字节数据。
void write(byte[] b,int off,int len) throws IOException; //一次写出b个字节数组中从off位置开始的len个字节
void flush() throws IOException; //写操作完毕后,刷新缓冲区。强制把缓冲区中的数据写到目标文件中。
void close() throws IOException; //流操作完毕后,一定要关闭,以释放相关的系统资源。
2)Reader和Writer
a) Reader:字符输入流。常用方法有:
int read() throws IOException; //一次读取一个字符并返回,如果遇到文件尾,返回-1;
int read(char[] cbuf) throws IOException; //一次读取指定长度(b.length)的字符数据,存 储到指定的数组中,返回读取到的字符数。如 果遇到文件尾,返回-1
int read(char[] b, int off, int len) throws IOException; //一次读取指定长度 (b.length)的字符数据,存 储到指定的数组中,返回读取 到的字符数。如果遇到文件 尾,返回-1
void close() throws IOException; //流操作完之后,一定要关闭,以释放相关的系统资源。
b) Writer:字符输出流。
void write(int b) throws IOException; //一次写出一个指定的字符。
void write(char[] b) throws IOException; //一次写出指定数组中的字符数据。
void write(char[] b, int off, int len) throws IOException; //一次写出b字符数组中从off位置开始的len个字符。
void flush() throws IOException; //写操作完毕后,刷新缓冲区。强制把缓冲区中的数据写到目标文件中。
void close() throws IOException; //流操作完之后,一定要关闭,以释放相关的系统资源。
void write(String str) throws IOException; //写入字符串。
void write(String str, int off, int len) throws IOException //写入字符串的某一部分。
6. FileInputStream和FileOutputStream:专门用来以字节的方式操作文件的流类。适用操作二进制文件。如:图片、声音、视频等。
1) FileInputStream:文件字节输入流
用构造方法:
public FileInputStream(String name) throws FileNotFoundException
public FileInputStream(File file) throws FileNotFoundException
编程基本格式:
InputStream is = null;
try{
is = new FileInputStream(new File("文件的绝对路径"));
byte[] buffer = new byte[8]; //缓存区,一般定义成8的倍数 (8192)
int count = -1; //计数器
while((count = is.read(buffer)) != -1){
//对读取到的字节数据进行相应操作
}
//for(int count = -1; (count = is.read(buffer)) != -1;){
//对读取到的字节数据进行相应操作
//}
}catch(IOException e){
e.printStackTrace();
}finally{
if(null != is){ //关闭流
try{ is.close(); }catch(IOException e){ e.printStackTrace();}
}
}
2) FileOutputStream: 文件字节输出流
常用构造方法:
public FileOutputStream(String name) throws FileNotFoundException //默认用覆盖模式写文件
public FileOutputStream(String name, boolean append) throws FileNotFoundException //append为true时,用追加模式写文件
public FileOutputStream(File file) throws FileNotFoundException
public FileOutputStream(File file, boolean append) throws FileNotFoundException
编程基本格式:
OutputStream os = null;
try{
os = new FileOutputStream(new File("文件的绝对路径"));
byte[] b = ...;
os.write(b, off, len);
os.flush();
}catch(IOException e){
e.printStackTrace();
}finally{
if(null != os){
try{ os.close(); }catch(IOException e){ e.printStackTrace();}
}
}
注意:
1.字节流只适合操作二进制的文件,例如:音频,视频,图片等。
2.字节输出流写中文不会乱码
7. FileReader和FileWriter:专门以字符方式操作文件的流类。适用于操作文本文件。
1) FileReader:字符输入流。没有比Reader新增方法。
2) FileWriter:字符输出流。没有比Writer新增方法。