(观看网易云课堂---java300集大型视频做的笔记,都是里面的内容,整合了一下)
一、File类
1.文件与目录的路径的抽象表达形式,不代表文件本身;
2.路径分隔符(pathSeparator):“;”;
3.文件分隔符(separator):“\”windows;“/”非windows;
4.相对路径与绝对路径;
5.部分函数介绍:
String path = "C:\\Users\\Administrator\\Desktop\\图片\\java操作";
String name = "1.jpg";
File file = new File(path,name);
//获取文件名:
System.out.println(file.getName());//返回文件名
System.out.println(file.getPath());//以绝对路径构建:返回路径;以相对路径构建,返回文件名
System.out.println(file.getAbsolutePath());//返回绝对路径
System.out.println(file.getParent());//返回上一级路径,如果是相对路径则返回null
//判断信息:主要是属性
System.out.println(file.exists());//文件是否存在
System.out.println(file.canWrite());//文件是否可写
System.out.println(file.canRead());//文件是否可读
System.out.println(file.isDirectory());//是否是目录(文件夹):不存在的路径默认为文件夹
System.err.println(file.isFile());//是否是文件
//长度,字节数,只有 文件能读
System.out.println(file.length());//文件长度
/*
* 创建文件
* createNewFile();返回boolean类型值
* 创建临时文件
* createTempFile()返回File对象
* 删除临时文件
* deleteOnExit();退出即删除
*
* 目录操作
* 创建目录:mkdir()确保父目录存在
* 创建目录:mkdirs()父目录不存在的话,创建父目录链
* 得到子目录中文件或目录(名字,字符串):String a[] = file.list();
* 得到子目录中文件或目录(File对象):File files[] = file.listFiles();
* */
二、 IO流的分类
1.流向:输入流,输出流
2.数据:
字节流:二进制,可以处理一切文件,包括纯文本、doc、音频、视频。。。
字符流:文本文件,只能处理纯文本。
3.功能:节点流,处理流
三、字节流与字符流
1.字节流:
抽象类:
InputStream:read(byte[] b)、read(byte[] b, int off, int len)、close()
OutputStream:write(byte[] b)、write(byte[] b, int off, int len)、flush()、close()
实现类:FileInputStream、FileOutputStream:
2.字符流:
抽象类:
Reader:read(char[] b)、read(char[] b, int off, int len)、close()
Writer:write(char[] b)、write(byte[] b, int off, int len)、write(String,int off,int len)、flush()、close()
实现类:FileReader、FileWriter
3.字节流实例
String path = "C:\\Users\\Administrator\\Desktop\\测试.txt";//文件路径
//读文件
File file = new File(path);
InputStream in = null;
try {
in = new FileInputStream(file);
byte[] b = new byte[1024];//缓冲区
int len;//一次读取长度
while((len = in.read(b)) != -1) {
String result = new String(b , 0 , len);
System.out.println(result);
}
} catch (FileNotFoundException e) {
// TODO 自动生成的 catch 块
System.out.println("文件打开失败!");
e.printStackTrace();
} catch (IOException e) {
System.out.println("文件读取失败!");
e.printStackTrace();
} finally {
try {
if(in != null) {
in.close();
}
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
String path = "C:\\\\Users\\\\Administrator\\\\Desktop\\\\测试.txt";//路径
//写文件
File file = new File(path);//连接文件
OutputStream out = null;
try {
out = new FileOutputStream(file,true);//初始化,字节流读取文件,追加读
String content = "go head! Donnot waste time!";//要写入文件的内容
byte[] b = content.getBytes();//字符串转换为字节数组
out.write(b, 0, b.length);//写入文件
out.flush();//刷新缓冲区
} catch (FileNotFoundException e) {
// TODO 自动生成的 catch 块
System.out.println("文件打开失败");
e.printStackTrace();
} catch(IOException e) {
System.out.println("数据写入文件失败");
e.printStackTrace();
} finally {
if(out != null) {
try {
out.close();//关闭输出流
} catch (IOException e) {
// TODO 自动生成的 catch 块
System.out.println("输出流关闭失败");
e.printStackTrace();
}
}
}
4.字符流示例
public class FileReaderDemo {
//只能用于纯文本
public static void main(String[] args) {
String path = "C:\\Users\\Administrator\\Desktop\\图片\\java操作\\测试.txt";//文件路径
File file = new File(path);
try {
Reader in = new FileReader(file);//字符流
char[] a = new char[1024];
int len ;//一次读取长度
while((len = in.read(a)) != -1) {
String result = String.copyValueOf(a);//字符数组转为字符串输出
System.out.println(result);
}
in.close();
} catch (FileNotFoundException e) {
System.out.println("文件 打开失败");
e.printStackTrace();
} catch (IOException e) {
System.out.println("文件读取失败");
e.printStackTrace();
}
}
}
四、处理流
用于增强功能、提供性能,放在节点流上
1.缓冲流
BufferedInputStream
BufferedOutputStream
BufferedReader:新增方法:readLine()
BufferedWriter:新增方法:write(),newLine()
2.转换流
字节流转换为字符流 用于处理乱码(编码集,解码集)
编码:字符--->二进制
解码:二进制--->字符
要保证编码集、解码集统一
字节流能解码,字符流不能解码(设置字符集)
eg:BufferedReader out = new BufferedReader(new getInputStream(new FileInputStream(new File(path))));
五、其他流
对象处理流:
序列化:输入流:ObjectInputStream readObject()
反序列化:输出流:ObjectOutputStream writeObject()
注意:先序列化,再反序列化:反序列化必须与序列化保持一致。
不是所有的对象都可以序列化,必须实现java.io.Serializable