今日目标:
|-- FIleUtil工具类的使用(学习第三方jar包的使用)
|-- Apache的commons项目介绍和使用
|-- try with resource语句块
|-- Properties文件的使用
|-- 新的IDE -- intellij IDEA
|-- 安装教程
|-- 配置和使用教程
|-- 系统编程(多任务)
|-- 什么是多任务
|-- 操作系统如何实现多任务
|-- 时间片切换
|-- 优先级别调度
|-- java是如何实现多任务 -- 多线程
|-- java如何实现多线程
|-- 继承Thread
|-- 实现Runable接口
|-- 实现Callabe和Future接口(1.5提供的)
|-- jdk提供了线程池用来获取线程(1.5)
|-- 线程的生命周期
|-- 线程安全问题
|-- 加锁 synchronized关键字的使用
|-- Lock锁
|-- 同步问题(生产者和消费者问题)
|-- 唤醒机制
FileUtils工具类的使用
1、Apache基金会,是java目前最大的开源基金会
commons项目,提供大量的好用的各种java工具的使用
common-io、common-fileupload
common-io中存在了FileUtils提供大量好用方便的IO流操作。
2、第三方jar包的使用!!
我们使用第三方(其他程序员、公司、组织、国家、基金会)
字节码打成一个jar
上代码:
package com.openlab.day22.io;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.io.FileUtils;
public class TestFileUtils {
public static void main(String[] args) {
// FileUtils.copyFile(new File("G:\\pictures\\b.jpg"), new File("c:\\a.jpg"));
// InputStream is = null;
// try {
// is = new FileInputStream("G:\\\\pictures\\\\b.jpg");
//
//
// is.close();
//
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// } catch (IOException e) {
// e.printStackTrace();
// }
// 将需要关闭的IO流对象的创建,放在try上面,这样,如果我们没有手动关闭该流
// 等try运行结束,程序会自动关闭该流(避免了我们忘了关闭流,或者写的不规范,导致IO没有被关闭)
try(FileInputStream is = new FileInputStream("G:\\pictures\\b.jpg")) {
} catch (Exception e) {
System.out.println("异常正常处理");
}
}
}
try-with-resource语句块和try-with-finally语句块:
try {
} catch(Exception e) {
} finally {
// IO流关闭
}
JDK7 提供新特性:
try-resource
try(创建需要关闭对象的代码放在这儿){
} catch(Exception e) {
}
Properties文件:
开发项目时,或者写程序,经常需要定义常量
如果在项目不同阶段,或者有些项目中数据,需要经常性修改和变化,如果我们
将这些值,定义成变量或者常量,在项目部署时,就非常不方便(字节码文件没有办法修改)
如果这种情况下,一般建议将这种值配置出来(使用一个独立的文件,保存这些值)
这种独立文件,java喜欢使用如下的三种格式:
|-- properties
|-- xml
|-- json
创建properties文件:
在src下(类路径下),创建xxxx.properties文件,该文件是一种Map格式
也就是说,是以键值对存在的!!
注意:在eclipse中,该文件默认使用的编码是ISO-8859-1编码,也就是它无法中文(一定要看到中文,需要安装插件)
在IDEA 中,可以设置为utf-8
上代码:
@Test
void test01() throws Exception {
Properties properties = new Properties();
try (InputStream is = new FileInputStream("commons.properties")){//先使用文件输入流打开properties文件
properties.load(is);//在使用load方法接收输入流
System.out.println(properties.getProperty("name"));//使用key得到值
System.out.println(properties.getProperty("size"));
System.out.println(properties.getProperty("path"));
System.out.println(properties.getProperty("username"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
如何在代码中获取properties配置文件中的值呢?
java.util.Properties类可以获取。
首先使用类加载器,获取类路径下的资源文件(以流的形式返回)
InputStream is = new FileInputStream("commons.properties")
之后在调用load方法加载这个文件。
properties.load(is);
获取配置文件的两种方法:
TestProperties.class.getClassLoader().getResourceAsStream("commons.properties")
this.getClass().getClassLoader().getResourceAsStream("commons.properties")
上代码:
@Test
void test02() throws Exception {
Properties properties = new Properties();
// 使用API
// this.getClass().getName()
// System.out.println(this.getClass().getName());
// TestProperties.class.getClassLoader().getResourceAsStream("commons.properties")
try (InputStream is = this.getClass().getClassLoader().getResourceAsStream("commons.properties")){
properties.load(is);
System.out.println(properties.getProperty("name"));
System.out.println(properties.getProperty("size"));
System.out.println(properties.getProperty("path"));
System.out.println(properties.getProperty("username"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
void test03() {
String age = PropertiesUtils.getValueByKey("commons.properties", "age");
String username = PropertiesUtils.getValueByKey("commons.properties", "username");
System.out.println(age);
System.out.println(username);
}
注意:静态方法中,是无法使用this关键字的!!!
经典面试题
byte a = 10;
a = a + 10; // 第一行代码 这行代码会报错,因为10是int类型,a是字节类型,类型会自动转换为int类型,与原来的类型不相同
a += 10; // 第二行代码 这行代码不会报错
自定义工具类:
package com.openlab.day22.io;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class PropertiesUtils {
public static String getValueByKey(String name, String key) {
Properties prop = load(name);
if (prop != null) {
return prop.getProperty(key);
}
throw new RuntimeException("配置的路径或者名称有误");
}
private static Properties load(String name) {
Properties properties = new Properties();
try (InputStream is = PropertiesUtils.class.getClassLoader().getResourceAsStream(name)){
properties.load(is);
return properties;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}