new Thread(new Runnable() {
@Override
public void run() {
// 要执行的代码才是重要的
}
}).start(); new Thread(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() +"新线程创建了");
}
}).start();
// 函数式编程的代码
new Thread(()-> {
System.out.println(Thread.currentThread().getName() +"新线程创建了");
}
).start(); public interface Runnable {
public abstract void run();
}
// Comparator接口中, 有且仅有一个抽象方法
public interface Comparator<T> {
// 这是该接口中有且仅有的一个抽象方法
int compare(T o1, T o2);
// 该方法与Object类中equals定义相同, 所以不算抽象方法
boolean equals(Object obj);
// 一些default方法, 有方法体, 不算抽象方法
// 一些静态方法, 有方法体, 不算抽象方法
}
// 我们今天自己定义的接口, 也满足函数式接口的要求
public interface Cook {
void makeFood();
} public class Test {
public static void main(String[] args) {
// 匿名内部类方式
new Thread(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() +"新线程创建了");
}
}).start();
// Lambda表达式方式
new Thread(() -> {
System.out.println(Thread.currentThread().getName() +"新线程创建了");
}
).start();
}
} public class Person {
private String name;
private int age;
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
public class Test {
public static void main(String[] args) {
// 创建Person数组, 静态初始化
Person[] persons = {
new Person("柳岩", 38),
new Person("迪丽热巴", 18),
new Person("古力娜扎", 19)
};
// 匿名内部类
// Arrays.sort(persons, new Comparator<Person>() {
// @Override
// public int compare(Person o1, Person o2) {
// return o1.getAge() - o2.getAge();
// }
// });
// Lambda表达式
Arrays.sort(persons, (Person o1, Person o2) -> {
return o1.getAge() - o2.getAge();
});
for (Person person : persons) {
System.out.println(person);
}
}
}public interface Calculator { // 计算器
int calc(int a, int b); // 计算a和b的结果返回
}
在下面的代码中,请使用匿名内部类和Lambda的标准格式调用 invokeCalc 方法,完成120和130的相加计算:
public class Demo08InvokeCalc {
public static void main(String[] args) {
// TODO 请在此使用匿名内部类方式调用invokeCalc方法来计算120+130的结果
// TODO 请在此使用Lambda【标准格式】调用invokeCalc方法来计算120+130的结果
}
private static void invokeCalc(int a, int b, Calculatorcalculator) {
int result = calculator.calc(a, b);
System.out.println("结果是:" + result);
}
} public interface Calculator {
// 计算两个数的结果
int calc(int a, int b);
}
public class Test {
public static void main(String[] args) {
// 匿名内部类方式
invokeCalc(120, 130, new Calculator() {
@Override
public int calc(int a, int b) {
return a + b;
}
});
// Lambda表达式方式
invokeCalc(120, 130, (int a, int b) -> {
return a + b;
});
}
// 调用计算器
public static void invokeCalc(int a, int b, Calculatorcalculator) {
int result = calculator.calc(a, b);
System.out.println(result);
}
}public class Test {
public static void main(String[] args) {
int jc = jc(5);
System.out.println(jc); // 120
}
// 计算n的阶乘
public static int jc(int n) {
// 出口
if (n == 1) {
return 1; // 能解决问题, 直接写代码
}
// 如果不能解决, 则拆解问题
return n * jc(n-1);
}
}public class Test {
public static void main(String[] args) {
File dir = new File("day08");
print(dir);
}
// 定义方法: 打印某个目录中所有路径
public static void print(File dir) { // 传入要打印的目录的File对象
// 先打印当前目录的路径
System.out.println(dir);
// 获取当前目录中的所有子文件和子目录
File[] files = dir.listFiles();
// 遍历数组
for (File file : files) {
// file代表子文件或者子目录, 具体是什么还需要判断
if (file.isFile()) {
// 如果是文件, 则打印文件
System.out.println(file);
} else {
// 如果是目录, 则递归调用当前方法, 打印"该子目录"中的所有路径
print(file); // 注意传入的是子目录对象!!!!!
}
}
}
}public class Test {
public static void main(String[] args) {
print(new File("day08"));
}
// 定义方法: 打印某个目录中的.java文件
public static void print(File dir) {
// 先获取当前目录中的所有文件
File[] files = dir.listFiles();
// 遍历
for (File file : files) {
// 判断是文件还是目录
if (file.isFile()) {
// 是文件, 判断后缀
if(file.getName().toLowerCase().endsWith(".java")) {
// 是java文件, 打印
System.out.println(file);
}
} else if (file.isDirectory()) {
// 是目录, 递归调用方法
print(file);
}
}
}
} public class Test {
public static void main(String[] args) {
print(new File("day08"));
}
// 打印指定目录中的.java文件
public static void print(File dir) {
// 获取当前目录中的文件, 使用过滤器
/*File[] files = dir.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return new File(dir, name).isDirectory() ||
name.toLowerCase().endsWith(".java");
}
});*/
// 使用Lambda表达式简化
File[] files = dir.listFiles((d, name) -> new File(d,name).isDirectory() ||
name.toLowerCase().endsWith(".java"));
// 遍历
for (File file : files) {
if (file.isFile()) {
// 是文件, 则打印
System.out.println(file);
} else if (file.isDirectory()) {
// 是目录, 则递归
print(file);
}
}
}
}
void close() :释放资源
void flush() :刷新缓冲区(对于字节流来说没有作用)
abstract void write(int b): 一次写一个字节 (参数是int是为了代码中写表示byte的整数方便不用强转)
void write(byte[] b): 一次写一个字节数组
void write(byte[] b, int off, int len): 一次写一个字节数组的一部分
java.io.FileOutputStream类: 文件字节输出流 (向文件写数据)
FileOutputStream(String name): 通过文件路径创建文件字节输出流
FileOutputStream(File file): 通过File对象创建文件字节输出流
java.io.FileOutputStream类: 文件字节输出流 (向文件写数据)
void write(byte[] b): 一次写一个字节数组
void write(byte[] b, int off, int len): 一次写一个字节数组的一部分
java.lang.String类:
byte[] getBytes(): 使用平台的默认字符集将此String编码为byte数组
Java中,byte的范围是 -128 ~ 127 共256个数值
编码表中字符范围是 0 ~ 255 共256个数值
写多个字节时:
如果第一个字节是正数中的 0~127, 则记事本会查询 ASCII码表 显示字符
如果第一个字节是负数中的:-128~-1, 则记事本会查询 GBK码表 显示字符.(将两个连续的byte组合为一个中文)
java.io.FileOutputStream类: 文件字节输出流
// 带有 续写 功能的构造方法, 不会清空文件
FileOutputStream(String name, boolean append): 通过文件路径创建文件字节输出流, true可以续写
FileOutputStream(File file, boolean append): 通过File对象创建文件字节输出流, true可以续写
换行符:
Windows系统: "\r\n"
Linux系统: "\n"
MacOS系统: "\r"
FileInputStream fis = new FileInputStream("模块名\\文件名");
// 一次读一个字节:
int by; // int变量用来存储每次读到的数据
while ((by = fis.read()) != -1) {
System.out.print((char)by); // 不要换行, 文件中自带换行的byte
}
fis.close();
java.io.InputStream抽象类: 字节输入流 (顶层类)
int read(byte[] b): 一次读一个字节数组
String(byte[] bytes): 使用平台默认字符集解码将byte数组转换为String
String(byte[] bytes, int offset, int length): 使用平台默认字符集将byte数组的一部分转换为String
FileInputStream fis = new FileInputStream("模块名\\文件名");
byte[] bytes = new byte[1024]; // 定义字节数组, 用于保存每次读取到的数据
int len; // 定义int变量, 用于保存每次读取到的长度
while ((len = fis.read(bytes)) != -1) {
String s = new String(bytes, 0, len); // 将读取到的字节转换为字符串, 读到多少个就转换多少个
System.out.print(s);
}
fis.close(); // 释放资源
FileInputStream fis = newFileInputStream("c:\\1.jpg"); // 输入流指向要读的数据源文件
FileOutputStream fos = newFileOutputStream("d:\\1.jpg"); // 输出流指向要写的目的地文件
// 一次读写一个字节数组
byte[] bytes = new byte[1024];
int len = 0;
while((len = fis.read(bytes))!=-1){
fos.write(bytes,0,len);
}
// 释放资源
fos.close();
fis.close();
String(char[] value): 将char数组转换为String
String(char[] value, int offset, int count): 将char数组的一部分转换为String
FileReader fr = new FileReader("a.txt");
// 一次读一个字符
int ch; // 定义变量保存每次读到的字符
while ((ch = fr.read()) != -1) {
System.out.print((char)ch);
}
// 一次读一个字符数组
char[] cs = new char[1024];
int len;
while ((len = fr.read(cs)) != -1) {
String s = new String(cs, 0, len);
System.out.print(s);
}
// 释放资源
fr.close();
abstract void close(): 刷新缓冲区并释放资源
abstract void flush() :刷新缓冲区
void write(int c): 写一个字符 (int类型为了代码编写方便)
void write(char[] cbuf): 写一个字符数组
abstract void write(char[] b, int off, int len): 写一个字符数组的一部分
void write(String str): 写一个字符串
void write(String str, int off, int len): 写一个字符串的一部分
注意: write()方法只是将数据写到内存缓冲区, 最后必须调用flush()或close()才能将数据真正写入磁盘
java.io.FileWriter类: 文件字符输出流
FileWriter(File file): 通过File对象创建文件字符输出流
FileWriter(String fileName): 通过文件路径创建文件字符输出流
void write(int c): 写一个字符 (int类型为了代码编写方便)
FileWriter使用步骤:
1.创建FileWriter对象, 构造方法中绑定要写入数据的目的地
2.使用FileWriter中的方法 write(), 把数据写入到"内存缓冲区"中(字符转换为字节的过程)
3.使用FileWriter中的方法 flush(), 把内存缓冲区中的数据,"刷新到文件中"
4.释放资源 close() (会先把内存缓冲区中的数据刷新到文件中)
FileWriter fw = newFileWriter("09_IOAndProperties\\d.txt");
fw.write(97); // int ch 用int值代表char
//fw.flush();
fw.close();
flush(): 刷新缓冲区 (将数据从内存中写入到磁盘)
close(): 刷新缓冲区, 并释放资源. 关闭流后不能再用同一个流对象操作
flush()可以省略, 只用close()来刷新并释放资源
void write(char[] cbuf): 写一个字符数组
abstract void write(char[] b, int off, int len): 写一个字符数组的一部分
void write(String str): 写一个字符串
void write(String str, int off, int len): 写一个字符串的一部分
FileWriter中带有续写功能的构造:
FileWriter(File file, boolean append): 通过File对象创建文件字符输出流. 第二个参数为true可以续写
FileWriter(String fileName, boolean append): 通过文件路径创建文件字符输出流. 第二个参数为true可
以续写
换行:
windows: "\r\n"直接写入
try {
//IO流对象的创建, 操作等代码
fw = newFileWriter("d:\\09_IOAndProperties\\g.txt", true);
for (int i = 0; i <10 ; i++) {
fw.write("HelloWorld"+i+"\r\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// 释放资源
if(fw != null){
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}try (创建流对象语句,如果多个,使用';'隔开) {
// 读写数据
} catch (IOException e) {
e.printStackTrace();
}
// 示例
try (FileWriter fw = newFileWriter("fw.txt");FileReader fr = newFileReader("fr.txt")) {
// IO操作
int ch = fr.read();
fw.write(ch);
} catch (IOException e) {
e.printStackTrace();
}FileWriter fw = new FileWriter("fw.txt");
FileReader fr = new FileReader("fr.txt");
try (fw; fr) {
// IO操作
int ch = fr.read();
fw.write(ch);
} catch (IOException e) {
e.printStackTrace();
}java.io.OutputStream抽象类: 字节输出流 (顶层类)
// 成员方法
void close() :释放资源
void flush() :刷新缓冲区(对于字节流来说没有作用)
// 写字节的成员方法
abstract void write(int b): 一次写一个字节 (参数是int是为了代码中写表示byte的整数方便不用强转)
void write(byte[] b): 一次写一个字节数组
void write(byte[] b, int off, int len): 一次写一个字节数组的一部分
java.io.FileOutputStream类: 文件字节输出流 (向文件写数据)
// 构造方法
FileOutputStream(String name): 通过文件路径创建文件字节输出流
FileOutputStream(File file): 通过File对象创建文件字节输出流
FileOutputStream(String name, boolean append): 通过文件路径创建文件字节输出流, true可以续写
FileOutputStream(File file, boolean append): 通过File对象创建文件字节输出流, true可以续写
java.io.InputStream抽象类: 字节输入流 (顶层类)
// 常用成员方法
void close(): 释放资源
// 读数据的方法
int read(): 一次读一个字节. 读到文件末尾返回-1 (返回int也是为了代码编写方便)
int read(byte[] b): 一次读一个字节数组. 读到的字节存放在参数中的字节数组中, 返回int值是本次读到的
字节的个数. 读到文件末尾返回-1
java.io.FileInputStream类: 文件字节输入流
// 构造方法
FileInputStream(String name): 使用文件路径创建文件字节输入流
FileInputStream(File file): 使用File对象创建文件字节输入流
java.io.Reader抽象类: 字符输入流 (顶层)
// 常用成员方法
void close() :关闭此流并释放与此流相关联的任何系统资源
// 读数据的方法
int read(): 一次读一个字符char, 返回读到的字符. 读到文件末尾返回-1 (返回int为了代码编写方便)
int read(char[] cbuf): 一次读取一个字符数组char[]. 返回读取的字符个数. 读到文件末尾返回-1
java.io.FileReader类: 文件字符输入流
// 构造方法
FileReader(File file): 根据File对象创建文件字符输入流
FileReader(String fileName): 根据File对象创建文件字符输入流
java.io.Writer抽象类: 字符输出流 (顶层类)
// 常用成员方法
abstract void close(): 刷新缓冲区并释放资源
abstract void flush() :刷新缓冲区
// 写
void write(int c): 写一个字符 (int类型为了代码编写方便)
void write(char[] cbuf): 写一个字符数组
abstract void write(char[] b, int off, int len): 写一个字符数组的一部分
void write(String str): 写一个字符串
void write(String str, int off, int len): 写一个字符串的一部分
java.io.FileWriter类: 文件字符输出流
// 构造方法
FileWriter(File file): 通过File对象创建文件字符输出流
FileWriter(String fileName): 通过文件路径创建文件字符输出流
FileWriter(File file, boolean append): 通过File对象创建文件字符输出流. 第二个参数为true可以续写
FileWriter(String fileName, boolean append): 通过文件路径创建文件字符输出流. 第二个参数为true可
以续写
集合:
java.util.Properties类: 属性集, 属于Map的双列集合, 继承自Hashtable
// 构造方法
Properties(): 创建一个Properties集合
// 可以使用Map接口中的方法
// 特有成员方法
Object setProperty(String key, String value): 保存/替换键值对
String getProperty(String key): 通过键获取值. 键不存在返回null
Set<String> stringPropertyNames(): 返回键的集合
void store(OutputStream out, String comments): 将集合数据用字节流写入文件(不能中文), 并写入注释
void store(Writer writer, String comments): 将集合数据用字符流写入文件(可以中文), 并写入注释
void load(InputStream inStream): 从配置文件中通过字节流加载数据到Properties集合(不能读中文)
void load(Reader reader): 从配置文件中通过字符流加载数据到Properties集合(可以读中文)
1.3.1.jpg (54.87 KB, 下载次数: 22)
| 欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |