/*
*作者:呆萌老师
*☑csdn认证讲师
*☑51cto高级讲师
*☑腾讯课堂认证讲师
*☑网易云课堂认证讲师
*☑华为开发者学堂认证讲师
*☑爱奇艺千人名师计划成员
*在这里给大家分享技术、知识和生活
*各种干货,记得关注哦!
*vx:it_daimeng
*/
打印流分为PrintStream 和PrintWriter
PrintStream :针对字节 调用println 内容中有换行符等 自动调用flush方法(自动发送缓冲区的内容到文件中)
PrintWriter:针对字符 只有在调用println 才会 自动调用flush方法(自动发送缓冲区的内容到文件中)
//System.out.println() 打印到屏幕(标准输出设备) 建立在 PrintStream基础上
File file=new File("d:/aa/1.txt");
try {
PrintWriter printWriter=new PrintWriter(file);
printWriter.println("hello");
printWriter.println("world");
printWriter.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String filename=FilenameUtils.getName("d:/aa/1.txt"); //返回文件名
System.out.println(filename);
String extname=FilenameUtils.getExtension("d:/aa/1.txt"); //返回文件的扩展名(后缀名)
System.out.println(extname);
System.out.println(FilenameUtils.isExtension("d:/aa/0.jpg", "jpg"));//判断文件后缀名
long begin=System.currentTimeMillis();
try {
FileUtils.copyFile(new File("d:/aa/0.jpg"), new File("d:/aa/1.jpg"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long end=System.currentTimeMillis();
System.out.println("用工具包复制文件用时:"+(end-begin)+"毫秒");
properties
Properties是一个属性类,也许它可能在一些初级项目中很少运用,但是Properties文件,在许多项目中会经常运用,它使得我们的配置属性的独立化,方便项目后期的配置属性的更改。
//属性类 是一个集合 实现的是map接口
Properties properties=new Properties();
//存数据
properties.put("type", "mysql");
properties.setProperty("dbname", "taobao");
properties.setProperty("uid", "mayun");
//获取属性信息
//System.out.println(properties.getProperty("uid"));
//将集合中的信息保存到文件
File file=new File("db.properties");
FileWriter fileWriter;
try {
fileWriter = new FileWriter(file);
//将集合中的数据保存到流中
properties.store(fileWriter, "info");
fileWriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Properties properties=new Properties();
try {
//默认的路径是当前项目根目录
FileReader fileReader=new FileReader("db.properties");
properties.load(fileReader);
System.out.println(properties.getProperty("uid"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}