java学习之FileUtils工具类的使用、try-with-resource语句块、Properties文件、经典面试题

5 篇文章 0 订阅
今日目标:
	|-- 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;
	}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值