Java 学习(2022-07-12)
1. IO 流
1.1 文件
文件就是保存数据的地方,比如 word,txt,excel
1.1.1 创建文件相关方法
new File(String pathname)根据路径构建File对象new File(File parent, String child)根据父目录文件 + 子路径构建new File(String parent, String child)根据父目录 + 子路径构建
// 方式 1: new File(String pathname)
@Test
public void create01(){
String filepath = "e:\news1.txt";
File file = new File(filepath);
try {
file.createNewFile();
System.out.println("文件创建成功");
} catch (IOException e) {
e.printStackTrace();
}
}
// 方式 2:new File(File parent, String child) // 根据父目录文件 + 子路径构建
@Test
public void create02(){
File parentFile = new File("e:\\");
String filename = "news2.txt";
// 这里的 file 对象,在 java 程序中
// 只有执行了 createNewFile 方法,才会在磁盘中创建
File file = new File(parentFile, filename);
try {
file.createNewFile();
System.out.println("创建成功");
} catch (IOException e) {
e.printStackTrace();
}
}
// 方式 3 :new File(String parent, String child)` 根据父目录 + 子路径构建
@Test
public void create03(){
String parentFile = "e:\\";
String fileName = "news3.txt";
File file = new File(parentFile, fileName);
try {
file.createNewFile();
System.out.println("创建成功");
} catch (IOException e) {
e.printStackTrace();
}
}
1.1.2 常用文件操作
file.getName()获取文件名字file.getPath()获取文件路径file.getAbsolutePath()获取文件绝对路径file.length()获取文件大小(按照字节算)file.exists()文件是否存在file.isFile()是否是一个文件file.delete()删除文件,返回布尔值directory.mkdirs()创建目录
1.2 IO 流原理及流的分类
1.2.1 字节输入流(InputStream)
InputStream 抽象类是所有类字节流的超类
FileInpoutStream文件输入流
public void readFile02(){
// 单个字符读取,效率低,使用 read(byte[] b) 提交效率
String filePath = "e:\hello.txt";
int readData = 0;
byte[] buf = new byte[10]; // 一次读取 8 个字节
int readLen = 0;
FileInputStream fileInputStream = null;
try {
// 创建 FileInputStream 文件,用来读取
fileInputStream = new FileInputStream(filePath);
try {
// 如果返回 -1 ,读取完毕
// fileInputStream.read() 的返回值:实际读取字节数
while(( readLen = fileInputStream.read(buf)) != -1)
System.out.println(new String(buf, 0, readLen));
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
BufferedInputStream缓冲字节输入流ObjectInputStream对象字节输入流
1.3 节点流和处理流
- 节点流可以从一个特定的数据源读写数据,如
FileReader,FileWrite - 处理流(包装流)是“连接”在已存在的流之上,为程序提供更为强大的读写功能,如
BufferedReader,BufferedWriter
1.4 输入流
1.5 输出流
FileOutputStream
/**
* 演示使用 FileOutputStream 写入数据
* 如果该文件不存在,则创建
*/
@Test
public void writeFile(){
String filePath = "e:\a.txt";
FileOutputStream fileOutputStream = null;
// 1. new FileOutputStream(filepath):写入内容会覆盖之前内容
// 2. new FileOutputStream(filepath, true):写入内容不会覆盖之前内容
try {
fileOutputStream = new FileOutputStream(filePath);
fileOutputStream.write('a');
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
1.6 输入流 和 输出流综合案例(复制图片)
public static void main(String[] args) {
// 完成文件拷贝,将 e 盘下的图 -> c 盘
String srcfilePath = "e:\20220315_073237_ch55.jpg";
String destfilePath = "d:\20220315_073237_ch55.jpg";
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
try {
fileInputStream = new FileInputStream(srcfilePath);
fileOutputStream = new FileOutputStream(destfilePath);
byte[] buf = new byte[1024];
int len = 0;
while ((len = fileInputStream.read(buf)) != -1){
fileOutputStream.write(buf, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fileInputStream.close();
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
1.6 Properties 类
- 专门用于读写配置文件的集合类
配置文件的格式
键=值
键=值
- 注意:键值对不需要空格,值不需要用引号引起来。默认类型 String
1.6.1 常用方法
load:加载配置文件的键值对到 Properties 对象list:将数据显示到指定设备getProperty(key):根据键获取值setProperty(key,value):设置键值对到 Properties 对象store:将 Properties 中的键值对存储到配置文件,在 idea 中,保存信息到配置文件
1.6.2 获取数据库文件案例
public static void main(String[] args) throws IOException {
// 使用 Properties 类,来读取 mysql.properties 文件
// 1. 创建 Properties 对象
Properties properties = new Properties();
// 2. 加载指定配置文件
properties.load(new FileReader("src\mysql.properties"));
// 3. 把 k-v 控制台
properties.list(System.out);
// 4. 根据 key 获取对应值
String user = properties.getProperty("user");
System.out.println(user);
}