在介绍IO流之前,需要首先给大家介绍一下File的使用,及一些常用的方法。
File类 :文件和目录路径名的抽象表示形式。
通俗一点说,实例化File类的一个对象,其传入的参数就是一个路径名(可以是绝对路径名,也可以是相对路径名)
例:File file=new File("D:\\搞笑图\\01.jpg");
File类中,常用的两个分隔符属性是 pathSeparator (;) 和 separator (\) 。
在代码中的具体使用
//分隔符常量
System.out.println(File.pathSeparator);
System.out.println(File.separator);
//路径表示形式
String path="D:\\搞笑图\\01.jpg";
path="D:"+File.separator+"搞笑图"+File.separator+"01.jpg";//可跨平台
path="D:/搞笑图/01.jpg";
下面给大家简单说一下绝对路径和相对路径,很简单,大家一看就会懂。
以不同的方式对路径进行构建
public static void main(String[] args) {
String partentPath = "D:\\搞笑图";
String name = "01.jpg";
//以相对路径构建
File src;
src = new File(new File(partentPath), name);
System.out.println(src.getName());
System.out.println(src.getPath());
//绝对路径
src = new File("D:\\搞笑图\\01.jpg");
System.out.println(src.getName());
System.out.println(src.getPath());
//没有盘符 以user.dir构建
src = new File("test01.txt");
System.out.println(src.getName());
System.out.println(src.getPath());
System.out.println(src.getAbsolutePath());
}
运行的结果:
01.jpg
D:\搞笑图\01.jpg
01.jpg
D:\搞笑图\01.jpg
test01.txt
test01.txt
C:\Users\kys-26\IdeaProjects\IODemo\test01.txt
由这段代码,可以很明显的发现,相对路径,就是给一个路径,再给一个文件,文件根据前面的路径进行创建,这就是相对路径。而绝对路径则更简单:D:\\搞笑图\\01.jpg 这就代表的是绝对路径。
下面给大家列举一些File类中的常用方法,由于比较简单,我就不每个方法 一 一 举例了
文件是否存在 | exists() |
文件是否可写 | canWrite() |
是否为绝对路径 | isAbsolute() |
读取文件长度(非文件夹) | length() |
创建新文件夹 | createNewFile() |
删除文件 | delete() |
是否为目录 | isDirectory() |
获取父目录字符串 | getParent() |
创建目录(父目录不存在) | mkdirs() |
返回文件目录名数组 | list() |
下面开始讲IO流
首先大家需要明白,IO流的读写都是以程序为中心,文件到程序:输入流(InputStream) 程序到文件:输出流(OutputStream ) 。
流的分类:字符流、字节流、节点流、处理流
字节流:以二进制的方式,对文件进行读写操作,其中包含了所有类型的文件,例如:视频、文本、音频等等。
字符流:只能对纯文本进行读写操作
节点流:与流的源头最近
处理流:增强功能,提高性能。
流的读写操作一般步骤为:1.建立联系(创建File对象) 2.选择流(输出流或输入流) 3.循环读取 4.关闭流 释放资源
下面为大家呈现一段代码,帮助大家更好的理解我所说的这几个步骤。
输入流的具体操作
public static void main(String[] args) {
//1、建立联系 File对象
File src=new File("D:\\Io流练习\\test1\\01.txt");
//选择流
InputStream is=null;
try {
is = new FileInputStream(src);
//不断的读取
byte[] car=new byte[10];//缓冲数组
int len=0;//实际接受的大小
//循环读取
while (-1!=(len=is.read(car))){
//字节数组转换成字符串
String info=new String(car,0,len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("文件不存在");
} catch (IOException e) {
e.printStackTrace();
System.out.println("读取文件失败");
}finally {//释放资源
if (null!=is){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
System.out.println("关闭资源失败");
}
}
}
}
输出流的具体操作
public static void main(String[] args) {
//File对象 目的地
File dest = new File("D:\\Io流练习\\test1\\02.txt");
//选择流
OutputStream os=null;
//以追加形式写文件
try {
os=new FileOutputStream(dest,true);
String str="输出流";
//字符串转换为字节数组
byte[] data=str.getBytes();
os.write(data,0,data.length);
os.flush();//强制刷新出去
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("文件未找到");
} catch (IOException e) {
e.printStackTrace();
System.out.println("文件写出失败");
}finally {
if (null!=os){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
System.out.println("关闭输出流失败");
}
}
}
}
编码与解码 : 1.输出流: OutputStreamWriter 编码 2.输入流: InputStreamReader 解码
大家首先要注意编码与解码的格式一定要一致,否则会出现乱码的情况
下面为大家插入一段代码,帮助大家更好的理解编码与解码
public static void main(String[] args) throws UnsupportedEncodingException {
//解码 byte --> char
String str = "中国";
//编码 char --> bytes
byte[] data=str.getBytes();
//编码与解码字符集同一
System.out.println(new String(data));
data = str.getBytes("gbk");
//编码与解码格式不统一会出现乱码
System.out.println(new String(data));
//字节数不完整
String str2 = "中国";
byte[] data2=str2.getBytes();
System.out.println(new String(data,0,3));
}
运行的结果
中国
�й�
�й