0 IO流
IO技术主要的作用是解决设备与设备之间 的数据传输问题。比如: 硬盘--->内存 内存的数据---->硬盘上 把键盘的数据------->内存中
1 File类
File类描述一个文件或文件夹
1.1 File类的构造方法:
File(String pathname) 指定文件或者文件夹的路径创建一个File文件。
例:File file = new File("F:/a.txt")
File(File parent, String child) 根据parent抽象路径名和child路径名字符串创建一个新File实例。
例:
File(String parent, String child)
例:File file = new File("F:\\","a.txt")
P.S.目录分隔符:在windows系统上,目录分隔符是\;在linux系统上,目录分隔符是/。
(在windows系统中,写\\与/皆可)
1.2创建方法
createNewFile() 在指定位置创建一个指定的空文件,成功就返回true,如果已存在就不创建然后返回false
例: File file = new File("F:\\aa");
System.out.println("创建成功了吗?"+file.createNewFile());
mkdir() 在指定位置创建目录,这只会创建最后一级目录,如果上级目录不存在就抛异常。
例: File dir = new File("F:\\a.txt");
System.out.println("创建文件夹成功吗?"+dir.mkdir()); // mkdir 创建一个单级文件夹,
mkdirs() 在指定位置创建目录,这会创建路径中所有不存在的目录。
例: dir = new File("F:\\aa\\bb");
System.out.println("创建多级文件夹:"+ dir.mkdirs());
renameTo(File dest) 重命名文件或文件夹,也可以操作非空的文件夹。
如果目标文件与源文件是在同一个路径下,那么renameTo的作用是重命名,
如果目标文件与源文件不是在同一个路径下,那么renameTo的作用就是剪切,而且还不能操作文件夹。
移动/重命名成功则返回true,失败则返回false。
例: File destFile = new File("F:\\aaaaaaw");
System.out.println("重命名成功吗?"+file.renameTo(destFile)) ;
1.3 删除方法:
delete() 删除文件或一个空文件夹,如果是文件夹且不为空,则不能删除,成功返回true,失败返回false。
deleteOnExit() 在虚拟机终止时,请求删除此抽象路径名表示的文件或目录,保证程序异常时创建的临时文件也可以被删除。
1.4 判断方法:
exists() 文件或文件夹是否存在。
isFile() 是否是一个文件,如果不存在,则始终为false。
isDirectory() 是否是一个目录,如果不存在,则始终为false。
isHidden() 是否是一个隐藏的文件或是否是隐藏的目录。
isAbsolute() 测试此抽象路径名是否为绝对路径名。
1.5 获取方法:
getName() 获取文件或文件夹的名称,不包含上级路径。
getPath() 返回绝对路径,可以是相对路径,但是目录要指定
getAbsolutePath() 获取文件的绝对路径,与文件是否存在没关系
length() 获取文件的大小(字节数),如果文件不存在则返回0L,如果是文件夹也返回0L。
getParent() 返回此抽象路径名父目录的路径名字符串;如果此路径名没有指定父目录,则返回null。
lastModified() 获取最后一次被修改的时间。
1.6 文件夹相关:
staic File[] listRoots() 列出所有的根目录(Window中就是所有系统的盘符)
list() 返回目录下的文件或者目录名,包含隐藏文件。对于文件这样操作会返回null。
listFiles() 返回目录下的文件或者目录对象(File类实例),包含隐藏文件。对于文件这样操作会返回null。
list(FilenameFilter filter) 返回指定当前目录中符合过滤条件的子文件或子目录。对于文件这样操作会返回null。
listFiles(FilenameFilter filter)返回指定当前目录中符合过滤条件的子文件或子目录。对于文件这样操作会返回null。
2文件IO处理
IO流分类:
如果是按照数据的流向划分:
输入流
输出流
如果按照处理的单位划分:
字节流: 字节流读取的是文件中二进制数据,不做任何处理。
字符流: 字符流读取的是字符。其本质也是读取文件中二进制数据,不过会对其进行解码。
字符流 = 字节流 + 解码
2.1 字节流
2.1.1 输入字节流:
--------| InputStream 所有输入字节流的基类 抽象类
------------| FileInputStream 读取文件数据的输入字节流
使用FileInputStream读取文件数据的步骤:
1. 找到目标文件
2. 建立数据的输入通道。
3. 读取文件中的数据。
4. 关闭资源
//方式1: 使用循环读取文件的数据
public static void readTest1() throws IOException{
//找到目标文件
File file = new File("F:\\美女\\1.jpg");
//建立数据的输入通道
FileInputStream fileInputStream = new FileInputStream(file);
//读取文件的数据
int content = 0; //声明该变量用于存储读取到的数据
while((content = fileInputStream.read())!=-1){
System.out.print((char)content);
}
//关闭资源
fileInputStream.close();
}
//方式2:使用缓冲数组配合循环一起读取。
public static void readTest2() throws IOException{
//找到目标文件
File file = new File("F:\\美女\\1.jpg");
//建立数据的输入通道
FileInputStream fileInputStream = new FileInputStream(file);
//建立缓冲数组配合循环读取文件的数据。
int length = 0; //保存每次读取到的字节个数。
byte[] buf = new byte[1024]; //存储读取到的数据 缓冲数组的长度一般是1024的倍数,因为与计算机的处理单位。 理论上缓冲数组越大,效率越高
while((length = fileInputStream.read(buf))!=-1){ // read方法如果读取到了文件的末尾,那么会返回-1表示。
System.out.print(new String(buf,0,length));
}
//关闭资源
fileInputStream.close();
}
方式2效率高于方式1
2.1.2 输出字节流:
--------| OutputStream 是所有输出字节流的父类、抽象类。
-----------| FileOutStream 向文件输出数据的输出字节流。
使用FileOutputStream输出文件数据的步骤:
1. 找到目标文件
2. 建立数据的输出通道。
3. 把数据转换成字节数组写出。
4. 关闭资源
FileOutputStream要注意的细节:
1.使用FileOutputStream的时候,如果目标文件不存在,那么会自动创建目标文件对象。
2.使用FileOutputStream写数据的时候,如果目标文件已经存在,那么会先清空目标文件中的数据,然后再写入数据。
3.使用FileOutputStream写数据的时候, 如果目标文件已经存在,需要在原来数据基础上追加数据的时候应该使用new FileOutputStream(file,true)构造函数,第二参数为true。
4.使用FileOutputStream的write方法写数据的时候,虽然接收的是一个int类型的数据,但是真正写出的只是一个字节的数据,
只是把低八位的二进制数据写出,其他二十四位数据全部丢弃。
00000000-000000000-00000001-11111111 511
11111111---> -1
//方式一:每次只能写一个字节的数据出去。
public static void writeTest1() throws IOException{
//找到目标文件
File file = new File("F:\\b.txt");
//建立数据的输出通道
FileOutputStream fileOutputStream = new FileOutputStream(file);
//把数据写出
fileOutputStream.write('h');
fileOutputStream.write('e');
fileOutputStream.write('l');
fileOutputStream.write('l');
fileOutputStream.write('o');
//关闭资源
fileOutputStream.close();
}
//方式二:使用字节数组把数据写出。
public static void writeTest2() throws IOException{
//找到目标文件
File file = new File("F:\\b.txt");
//建立数据输出通道
FileOutputStream fileOutputStream = new FileOutputStream(file,true);
//把数据写出。
String data = "\r\nhello world";
fileOutputStream.write(data.getBytes());
//关闭资源
fileOutputStream.close();
}
//方式三:使用字节数组把数据写出。
public static void writeTest3() throws IOException{
//找到目标文件
File file = new File("F:\\b.txt");
//建立数据输出通道
FileOutputStream fileOutputStream = new FileOutputStream(file);
//把数据写出。
String data = "abc";
byte[] buf = data.getBytes();
fileOutputStream.write(buf, 0, 3); // 0 从字节数组的指定索引值开始写, 2:写出两个字节。
//关闭资源
fileOutputStream.close();
}
方式3效率远高于方式2
2.1.3 IO异常处理
例:
public static void readTest() {
FileInputStream fileInputStream = null;
try {
// 找到目标文件
File file = new File("F:\\aaaaa.txt");
// 建立数据输入通道
fileInputStream = new FileInputStream(file);
// 建立缓冲数组读取数据
byte[] buf = new byte[1024];
int length = 0;
while ((length = fileInputStream.read(buf)) != -1) {
System.out.print(new String(buf, 0, length));
}
} catch (IOException e) {
System.out.println("读取文件资源出错....");
throw new RuntimeException(e);
} finally {
try {
if (fileInputStream != null) {
fileInputStream.close();
System.out.println("关闭资源成功...");
}
} catch (IOException e) {
System.out.println("关闭资源失败...");
throw new RuntimeException(e);
}
}
}
2.1.4 缓冲输入字节流BufferedInputStream
输入字节流体系:
----| InputStream 输入字节流的基类。 抽象
----------| FileInputStream 读取文件数据的输入字节流
----------| BufferedInputStream 缓冲输入字节流 缓冲输入字节流内部维护了一个8kb的字节数组,能够提高读取文件数据的效率。
注意: 凡是缓冲流都不具备读写文件的能力。
使用BufferedInputStream的步骤 :
1. 找到目标文件。
2. 建立数据的输入通道
3. 建立缓冲输入字节流流
4. 关闭资源
public static void readTest2() throws IOException{
//找到目标文件
File file = new File("F:\\a.txt");
//建立数据的输入通道
FileInputStream fileInputStream= new FileInputStream(file);
//建立缓冲输入字节流流
BufferedInputStream bufferedInputStream= new BufferedInputStream(fileInputStream);
//读取文件数据
int content = 0 ;
while((content = fileInputStream.read())!=-1){
System.out.print((char)content);
}
//关闭资源
bufferedInputStream.close();//调用BufferedInputStream的close方法实际上关闭的是FileinputStream.
}
上述代码等价于自建8*1024缓冲字节数组:
public static void readTest() throws IOException{
File file = new File("F:\\a.txt");
//建立数组通道
FileInputStream fileInputStream = new FileInputStream(file);
//建立缓冲数组读取数据
byte[] buf = new byte[1024*8];
int length = 0;
while((length = fileInputStream.read(buf))!=-1){
System.out.print(new String(buf,0,length));
}
//关闭资源
fileInputStream.close();
}
2.1.5 缓冲输出字节流Bufferedoutputstream
--------| OutputStream 所有输出字节流的基类,抽象类
------------| FileOutputStream 向文件 输出数据的输出字节流
------------| Bufferedoutputstream 缓冲输出字节流 其内部维护了一个8kb的字节数组,提高了写数据的效率。
。
使用BufferedOutputStream的步骤:
1. 找到目标文件
2. 建立数据的输出通道
BufferedOutputStream 要注意的细节
1. 使用BufferedOutStream写数据的时候,它的write方法是是先把数据写到它内部维护的字节数组中。
2. 如果需要把数据真正的写到硬盘上面,需要调用flush方法或者是close方法,或者是内部维护的字节数组已经填满数据的时候。
public class Demo2 {
public static void main(String[] args) throws IOException {
//找到目标文件
File file = new File("F:\\a.txt");
//建立数据的输出通道
FileOutputStream fileOutputStream = new FileOutputStream(file);
//建立缓冲输出字节流对象
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
//把数据写出
bufferedOutputStream.write("hello world".getBytes());
//把缓冲数组中内部的数据写到硬盘上面。
//bufferedOutputStream.flush();
bufferedOutputStream.close();
}
}
2.2 字符流
(未完待续)