JAVA学习笔记(五)
2018-03-18 00:08
225 查看
十六、JAVA IO
JAVA IO 是一个预设的函数库,由 java.io 包提供,主要涉及文件,网络数据流,内存缓冲等的输入输出。
1、Java 流:字节流(字节是数据的最小单位,一般为二进制数据读写)、字符流(字符是数据的最小单位,一个字符占用两个字节)
2、字节流
(1)抽象类
(2)常见的字节流处理类表 //转载自:Java IO 字节流
//字节流每次都只操作一个单位数据单位(也就是字节),用 in.read() 读输入流,达到末尾时返回 -1 ,并且使用完成后调用close() 方法将其关闭import java.io.*;3、字符流
字符流的两个顶层抽象类是
// FileReader、FileWriter 的操作最小单位是一个字符 16 bits
// FileInputStream、FileOutputStream 的操作最小单位是一个字节 8 bits
4、(通用)标准流
Standard Input: 用以将数据输入给用户的程序,通常键盘作为标准输入流,表示为
Standard Output:用以输出用户程序产生的数据,通常控制台屏幕作为标准输出流,表示为
Standard Error: 这是用来输出用户产生的错误数据,通常控制台屏幕作为标准错误流,表示为
java.util.Scanner 是 Java5 的新特征,可以通过 Scanner 类来获取用户的输入。
Scanner s = new Scanner(System.in);(1)Scanner 包含的方法:next()、hasNext() (判断是否还有输入)、nextInt() (将输入的字符串转换为整数)、nextFloat()、nextBoolean()
(2)Scanner 输入值为 [空格]、回车、Tab等键时,会直接返回下一个输出,影响包含这些键值的字符串获取
6、文件输入输出
(1)FileInputStreamInputStream f = new FileInputStream("F:/test");
File 类在 java.io 包中,创建 File 对象:File file = new File("F:/test","input.txt");
十七、反射
反射是一种动态获取信息、动态调用对象方法的机制。通过反射我们可以在运行时:判断对象所属的类、构造任意一个类
4000
的对象、判断一个类具有的成员变量和方法、调用任意一个对象的方法 ,还可以通过反射生成动态处理
1、获取包名和类名
(1)getClass() 方法 public class test10{
public static void main(String arg[]) throws IOException{
//IOException IO流异常
test10 test=new test10();
System.out.println(test.getClass());结果输出为:class com.wyh.test10System.out.println(test.getClass().getName());结果输出为:com.wyh.test10 //getClass().getName()是用来返回Class对象所代表的具体对象的名称。
另一种 class.getName 的方法为 String i=test10.class.getName();
System.out.println(i);2、返回具有指定名的类的class 对象:class.forName()public class test14 {
public static void main(String[] args) throws Exception { //使用class.forName必须加 exception
Class t=Class.forName("com.wyh.test14");
System.out.println(t.getName());
}
}// class<?> 其中,?代表不确定的java类型;T代表java类型;K、V分别代表java键值中的 key、value;E代表 element(这个类用来构建xml中节点)
3、返回当前类的父类: getSupercClass()
4、获取所有的接口:getInterfaces() Class<?> intes[] = clazz.getInterfaces();5、newInstance() 方法,用于实例化对象,这会调用类的默认构造方法
6、从 Field 对象中获取类的属性类型和修饰符Class<?> clazz = Class.forName("com.tianmaying.ReflectionTest");
System.out.println("类本身定义的属性:");
// 取得本类的全部属性
Field[] field = clazz.getDeclaredFields();
for (int i = 0; i < field.length; i++) {
// 获取权限修饰符
int mo = field[i].getModifiers();
String priv = Modifier.toString(mo);
// 获取属性类型
Class<?> type = field[i].getType();
System.out.println(priv + " " + type.getName() + " " + field[i].getName() + ";");// 获取类的 public 属性用:getFields()
7、通过 field.set() 方法对属性赋值
JAVA中创建对象的5种方法
8、用 Method 对象获取方法的整个签名信息 // 获取类的所有方法
Method method[] = clazz.getMethods();
//可以通过给
十八、日期处理
// API :应用程序编程接口,预设函数(API接口)
时间日期API的引入:import java.util.Timer;
import java.util.Date;1、用 currentTimeMillis() 方法求时间import java.text.SimpleDateFormat;
import java.util.Date;
public class test14 {
public static void main(String[] args) throws Exception {
long Time= System.currentTimeMillis();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy年-MM月dd日-HH时mm分ss秒");
Date date=new Date(Time);
System.out.println(formatter.format(date)); //结果输出为:2018年-03月18日-15时51分09秒
}
}2、使用 Clock、instant、date 获取时间import java.time.Clock;
import java.time.Instant;
import java.util.Date;
输出的结果为:
Sun Mar 18 15:59:53 CST 2018
15213599939313、用 ZoneId.of("洲/城市") 来获取时区,用 t.getRules() 输出
4、本地时间用 LocalTime.now() 获取(可以自定义时区)
5、本地日期获取:LocalDate.now()
// 例子:给 LocalDate 对象加减时间(年/月/日)LocalDate today = LocalDate.now();
LocalDate tomorrow = today.plus(1, ChronoUnit.DAYS);
LocalDate yesterday = tomorrow.minusDays(2);
LocalDate independenceDay = LocalDate.of(2014, Month.JULY, 4);
DayOfWeek dayOfWeek = independenceDay.getDayOfWeek();6、LocalDateTime 包含本地日期与时间LocalDateTime time = LocalDateTime.of(2017, Month.MARCH,16, 15, 10, 1);输出结果为:2017-03-16T15:10:01(可以自定义访问修改时间)
基于 LocalDateTime 的时间可访问性,有些时候需要将Date与它进行相互转换
JAVA IO 是一个预设的函数库,由 java.io 包提供,主要涉及文件,网络数据流,内存缓冲等的输入输出。
1、Java 流:字节流(字节是数据的最小单位,一般为二进制数据读写)、字符流(字符是数据的最小单位,一个字符占用两个字节)
2、字节流
(1)抽象类
InputStream和
OutputStream中定义两个关键的抽象方法
read()和
write(),它们分别对数据的字节进行读写,其子类重载完成特定输入输出方式的对应实现。
(2)常见的字节流处理类表 //转载自:Java IO 字节流
//字节流每次都只操作一个单位数据单位(也就是字节),用 in.read() 读输入流,达到末尾时返回 -1 ,并且使用完成后调用close() 方法将其关闭import java.io.*;3、字符流
字符流的两个顶层抽象类是
Reader和
Writer,分别定义了关键方法
read()和
write(),表示对字符的读写。
// FileReader、FileWriter 的操作最小单位是一个字符 16 bits
// FileInputStream、FileOutputStream 的操作最小单位是一个字节 8 bits
4、(通用)标准流
Standard Input: 用以将数据输入给用户的程序,通常键盘作为标准输入流,表示为
System.in,其类型是
InputStream
Standard Output:用以输出用户程序产生的数据,通常控制台屏幕作为标准输出流,表示为
System.out,其类型是
PrintStream
Standard Error: 这是用来输出用户产生的错误数据,通常控制台屏幕作为标准错误流,表示为
System.err,类型和
System.out相同是
PrintStream
import java.io.*; public class test10{ public static void main(String arg[]) throws IOException{ //IOException IO流异常 InputStreamReader isrr=null; try { isrr =new InputStreamReader(System.in); char n; do{ n=(char)isrr.read();//一个字节一个字节地读,想要直接读字符串则用 readLine() System.out.println(n); }while(n!='#');//输入以# 结束,只读取包括#前的输入内容 }finally{ if(isrr !=null){ isrr.close(); } } } }5、Java Scanner 类
java.util.Scanner 是 Java5 的新特征,可以通过 Scanner 类来获取用户的输入。
Scanner s = new Scanner(System.in);(1)Scanner 包含的方法:next()、hasNext() (判断是否还有输入)、nextInt() (将输入的字符串转换为整数)、nextFloat()、nextBoolean()
(2)Scanner 输入值为 [空格]、回车、Tab等键时,会直接返回下一个输出,影响包含这些键值的字符串获取
6、文件输入输出
(1)FileInputStreamInputStream f = new FileInputStream("F:/test");
File f = new File("F:/test"); InputStream f = new FileInputStream(f);(2)FileOutputStreamOutputStream os = new FileOutputStream("test.txt");7、File类
File 类在 java.io 包中,创建 File 对象:File file = new File("F:/test","input.txt");
十七、反射
反射是一种动态获取信息、动态调用对象方法的机制。通过反射我们可以在运行时:判断对象所属的类、构造任意一个类
4000
的对象、判断一个类具有的成员变量和方法、调用任意一个对象的方法 ,还可以通过反射生成动态处理
1、获取包名和类名
(1)getClass() 方法 public class test10{
public static void main(String arg[]) throws IOException{
//IOException IO流异常
test10 test=new test10();
System.out.println(test.getClass());结果输出为:class com.wyh.test10System.out.println(test.getClass().getName());结果输出为:com.wyh.test10 //getClass().getName()是用来返回Class对象所代表的具体对象的名称。
另一种 class.getName 的方法为 String i=test10.class.getName();
System.out.println(i);2、返回具有指定名的类的class 对象:class.forName()public class test14 {
public static void main(String[] args) throws Exception { //使用class.forName必须加 exception
Class t=Class.forName("com.wyh.test14");
System.out.println(t.getName());
}
}// class<?> 其中,?代表不确定的java类型;T代表java类型;K、V分别代表java键值中的 key、value;E代表 element(这个类用来构建xml中节点)
3、返回当前类的父类: getSupercClass()
4、获取所有的接口:getInterfaces() Class<?> intes[] = clazz.getInterfaces();5、newInstance() 方法,用于实例化对象,这会调用类的默认构造方法
6、从 Field 对象中获取类的属性类型和修饰符Class<?> clazz = Class.forName("com.tianmaying.ReflectionTest");
System.out.println("类本身定义的属性:");
// 取得本类的全部属性
Field[] field = clazz.getDeclaredFields();
for (int i = 0; i < field.length; i++) {
// 获取权限修饰符
int mo = field[i].getModifiers();
String priv = Modifier.toString(mo);
// 获取属性类型
Class<?> type = field[i].getType();
System.out.println(priv + " " + type.getName() + " " + field[i].getName() + ";");// 获取类的 public 属性用:getFields()
7、通过 field.set() 方法对属性赋值
JAVA中创建对象的5种方法
8、用 Method 对象获取方法的整个签名信息 // 获取类的所有方法
Method method[] = clazz.getMethods();
// 获取方法参数列表 Class<?> para[] = method[i].getParameterTypes();7、可以用 invoke() 方法调用 Method 对象:method.invoke()
//可以通过给
getMethod()方法传入方法名和参数类型来获取特定的方法
十八、日期处理
// API :应用程序编程接口,预设函数(API接口)
时间日期API的引入:import java.util.Timer;
import java.util.Date;1、用 currentTimeMillis() 方法求时间import java.text.SimpleDateFormat;
import java.util.Date;
public class test14 {
public static void main(String[] args) throws Exception {
long Time= System.currentTimeMillis();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy年-MM月dd日-HH时mm分ss秒");
Date date=new Date(Time);
System.out.println(formatter.format(date)); //结果输出为:2018年-03月18日-15时51分09秒
}
}2、使用 Clock、instant、date 获取时间import java.time.Clock;
import java.time.Instant;
import java.util.Date;
Clock clock=Clock.systemDefaultZone(); //Clock 类提供当前日期与时间 long m=clock.millis(); //同System.currentTimeMillis()方法,获取的数据是:从1970年01月01日00时00分00秒000毫秒—到—>此刻的毫秒数,为long 类型 Instant in=clock.instant();// instant 表示某一个特点的时间点 //即从某个起点开始的偏移量,起点是格林威治时间(GMT)1970-01-01:00:00 Date leDate=Date.from(in); System.out.println(leDate); System.out.println(m);
输出的结果为:
Sun Mar 18 15:59:53 CST 2018
15213599939313、用 ZoneId.of("洲/城市") 来获取时区,用 t.getRules() 输出
4、本地时间用 LocalTime.now() 获取(可以自定义时区)
5、本地日期获取:LocalDate.now()
// 例子:给 LocalDate 对象加减时间(年/月/日)LocalDate today = LocalDate.now();
LocalDate tomorrow = today.plus(1, ChronoUnit.DAYS);
LocalDate yesterday = tomorrow.minusDays(2);
LocalDate independenceDay = LocalDate.of(2014, Month.JULY, 4);
DayOfWeek dayOfWeek = independenceDay.getDayOfWeek();6、LocalDateTime 包含本地日期与时间LocalDateTime time = LocalDateTime.of(2017, Month.MARCH,16, 15, 10, 1);输出结果为:2017-03-16T15:10:01(可以自定义访问修改时间)
基于 LocalDateTime 的时间可访问性,有些时候需要将Date与它进行相互转换
相关文章推荐
- JAVA与模式 学习笔记(一) 统一的建模语言UML介绍(2)
- 我的Thinking in Java学习笔记(三)
- 我的Thinking in Java学习笔记(四)
- 我的Thinking in Java学习笔记(六)
- 我的Thinking in Java学习笔记(五)
- JAVA与模式 学习笔记(一) 统一的建模语言UML介绍(1)
- 我的Thinking in Java学习笔记(四) (zt)
- 我的Thinking in Java学习笔记(八)
- 我的Thinking in Java学习笔记(七) (zt)
- 我的Thinking in Java学习笔记(十)
- Java中文处理学习笔记
- java 学习笔记
- java对象序列化学习笔记(z)
- Thinking in Java学习笔记(2)(zt)
- Java学习笔记001
- JAVA学习笔记之JIRA
- 我的Thinking in Java学习笔记(九)
- java对象序列化学习笔记
- 我的Thinking in Java学习笔记(六)(zt)
- JAVA与模式 学习笔记(一) 统一的建模语言UML介绍(2)