今日内容
一、File[熟悉]
1.1 介绍
1.2 构造方法
// 通过路径创建文件对象
File f1 = new File("a.txt");// 相对路径,相对于当前项目
System.out.println(f1 );
File f2 = new File("E:\\a.txt");// 绝对路径
File f3 = new File("E:/b.txt");// 绝对路径
1.3方法
public static void main(String[] args) throws IOException {
// 通过路径创建文件对象
File f1 = new File("a.txt");// 相对路径,相对于当前项目
System.out.println(f1 );
File f2 = new File("E:\\a.txt");// 绝对路径
File f3 = new File("E:/b.txt");// 绝对路径
// 判断文件对象是否存在
// boolean exists = f2.exists( );
// System.out.println(exists );
if (!f3.exists( )){
// 如果文件不存在,就创建文件
boolean newFile = f3.createNewFile( );
System.out.println("创建文件"+newFile );
}
File f4 = new File("a");
// 创建单层文件夹
boolean mkdir = f4.mkdir( );// make directory
System.out.println("创建文件夹:" + mkdir );
// 创建多层文件夹
File f5 = new File("b/c/d");
boolean mkdirs = f5.mkdirs( );
System.out.println("创建多层文件夹:" + mkdirs );
// 删除文件
File f6 = new File("a.txt");
boolean delete = f6.delete( );
System.out.println("删除是否成功:" + delete );
// 只能删除空文件夹
File f7 = new File("a");
boolean delete1 = f7.delete( );
System.out.println("删除单层文件夹:" + delete1 );
// 只能删除空文件夹
File f8 = new File("b");
boolean delete2 = f8.delete( );
System.out.println("删除多层文件夹:" + delete2 );
// 获得文件绝对路径
File f9 = new File("a.txt");
String path = f9.getAbsolutePath( );
System.out.println(path );
}
二、IO流
2.1 介绍
2.2 字节流
2.2.1FileInputStream
public static void main(String[] args) {
// 使用try-catch完成 读取a.txt中的数据
FileInputStream f = null;
try{
f = new FileInputStream("a.txt");
int i = -1;
while((i = f.read()) != -1){
System.out.println(i );
}
} catch (Exception e){
System.out.println("出错了" );
e.printStackTrace();
} finally {
try {
f.close( );
} catch (IOException e) {
e.printStackTrace( );
}
}
}
2.2.2 FileOutputStream
public static void main(String[] args) throws IOException {
/**
* 跟读取数据不一样,读取数据时没有该文件会报错
* 但是现在写出数据,没有该文件,它会自动创建出来
* -----------------------------------------
* 如果该文件以前有内容,这样直接写,是覆盖直接的内容
* -----------------------------------------
* 如果想要对该文件实现追加内容,就需要构造方法中设置第二个参数为true
*/
// 创建对象
//FileOutputStream fos = new FileOutputStream("b.txt");
FileOutputStream fos = new FileOutputStream("b.txt",true);
// 写入一个字节
fos.write(65);
fos.write(67);
// 关流
fos.close();
}
2.3 练习: 拷贝图片
将D:\justdoit.jpg图片拷贝一份到E:\jiushigan.jpg
思路: 从d盘读取,写到e盘
// 边读边写
public static void copy() throws IOException {
FileInputStream fis = new FileInputStream("E:\\justdoit.jpg");
FileOutputStream fos = new FileOututStream("D:\\jiushigan.jpg");
int a = -1;
while((a = fis.read()) != -1) {
fos.write(a);
}
fis.close();
fos.close();
}
2.4 缓冲字节流
public static void main(String[] args) throws Exception {
long begin = System.currentTimeMillis( );
// 创建字节输入流
FileInputStream fis = new FileInputStream("D:\\taotie2.png");
// 将其包装成缓冲流
BufferedInputStream bis = new BufferedInputStream(fis);
// 创建字节输出流
FileOutputStream fos = new FileOutputStream("E:\\taotie.png");
// 将其包装成缓冲流
BufferedOutputStream bos = new BufferedOutputStream(fos);
// 需要缓冲流在读写
int i = -1;
while((i = bis.read()) != -1){
bos.write(i);
}
bis.close();
bos.close();
long end = System.currentTimeMillis( );
System.out.println(" 耗时: "+(end - begin) );
}
2.5 字符流
2.5.1 FileReader
public static void main(String[] args) throws Exception{
FileReader fr = new FileReader("a.txt");
int a = -1;
while ((a = fr.read()) != -1) {
System.out.println((char)a );
}
fr.close();
}
2.5.2 FileWriter
public static void main(String[] args) throws Exception{
FileWriter fw = new FileWriter("d.txt");
// 写出一个字符
fw.write(97);
// 写出一个字符数组
fw.write(new char[]{'我','爱','学'});
// 写出一个字符串
fw.write("我爱java");
// 写出字符串中的部分数据
fw.write("javabigdata",4,3);
fw.close();
}
2.6 练习 复制小说
踹凯吃完成
public static void copyText() {
FileReader fr = null;
FileWriter fw = null;
try {
fr = new FileReader("E:\\三体.txt");
fw = new FileWriter("E:\\三体copy.txt");
int a = -1;
while((a = fr.read()) != -1) {
fw.write(a);
}
} catch(Exception e) {
System.out.println("出错了" + e.getMessage());
} finally {
try{
fr.close();
fw.close();
}catch(Exception e){
System.out.println("出错了" + e.getMessage());
}
}
}
2.7 缓冲字符流
2.8 总结
三、Hutool
public static void main(String[] args) {
// 删除文件/文件夹
// boolean del = FileUtil.del("E:\\2311\\第一组");
// System.out.println(del );
// 改名
// File rename = FileUtil.rename(new File("E:\\2333.txt"), "2311.txt", true);
// System.out.println(rename );
// 加水印
ImgUtil.pressText(//
FileUtil.file("d:/taotie2.png"), //
FileUtil.file("e:/taotie4.png"), //
"版权所有", Color.WHITE, //文字
new Font("黑体", Font.BOLD, 100), //字体
0, //x坐标修正值。 默认在中间,偏移量相对于中间偏移
0, //y坐标修正值。 默认在中间,偏移量相对于中间偏移
0.8f//透明度:alpha 必须是范围 [0.0, 1.0] 之内(包含边界值)的一个浮点数字
);
BufferedInputStream in = FileUtil.getInputStream("E:\\Angel.mp3");
BufferedOutputStream out = FileUtil.getOutputStream("E:\\ge.mp3");
long copySize = IoUtil.copy(in, out, IoUtil.DEFAULT_BUFFER_SIZE);
System.out.println("-- over --" );
}
四、Idea导入jar包
答: 就是java代码打包
答: 别人写好的一些工具代码,我们项目中需要使用,所以自己的项目就需要加入这些jar包