学习内容:视频P351-P400
- File文件的遍历搜索
public static void method05(File file) {
File[] files = file.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
File pathname = new File(dir,name);
return pathname.isDirectory()||pathname.getName().toLowerCase().endsWith("txt");
}
});
for (File file1 : files) {
if (file1.isDirectory()){
method05(file1);
}else
System.out.println(file1);
}
}
- 递归的概念和使用
- 练习1-n之间的和
public static int method01(int n) {
if (n != 1) {
return n + method01(n - 1);
} else {
return 1;
}
}
- 递归阶层计算
public static int method02(int n) {
if (n == 1) {
return 1;
} else {
return n * method02(n - 1);
}
}
IO流视频P359-P400
- 字符输出流、字符输入流、字节输出流、字节输入流、flush和close的区别、try、catch、finally处理异常、Properties集合的store和load方法、缓冲流的概念、字节缓冲输入流和输出流、字符缓冲输出流和输入流、字符编码集、转换流的使用、对象的序列化、对象的反序列化、transient瞬间关键字
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
public class IODemo {
public static void main(String[] args) throws IOException {
method03();
}
public static void method01() throws IOException {
FileOutputStream fos = new FileOutputStream("D:\\ideaProjects\\Demo_java\\javaTest\\src\\April\\Apr19th\\a.txt");
byte[] bytes = "张三".getBytes();
fos.write(bytes);
System.out.println(Arrays.toString(bytes));
fos.close();
}
public static void method2() throws IOException {
FileOutputStream fos = new FileOutputStream("D:\\ideaProjects\\Demo_java\\javaTest\\src\\April\\Apr19th\\c.txt",true);
fos.write("追加内容".getBytes());
for (int i = 0; i < 10; i++) {
fos.write("你好".getBytes());
fos.write("\r\n".getBytes());
}
fos.close();
}
public static void method03() throws IOException {
FileInputStream fis = new FileInputStream("D:\\ideaProjects\\Demo_java\\javaTest\\src\\April\\Apr19th\\c.txt");
int len = 0;
while ((len = fis.read() )!= -1){
System.out.println((char)len);
}
fis.close();
}
}
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
public class Demo001 {
public static void main(String[] args) throws IOException {
method02();
}
public static void method01() throws IOException {
FileInputStream fis = new FileInputStream("D:\\ideaProjects\\Demo_java\\javaTest\\src\\April\\Apr20th\\a.txt");
FileOutputStream fos = new FileOutputStream("D:\\ideaProjects\\Demo_java\\javaTest\\src\\April\\Apr20th\\b.txt");
int temp = 0;
while ((temp = fis.read()) != -1) {
fos.write(temp);
}
}
public static void method02() throws IOException {
FileReader fr = new FileReader("D:\\ideaProjects\\Demo_java\\javaTest\\src\\April\\Apr20th\\a.txt");
char[] cs = new char[1024];
int len = 0;
while ((len = fr.read(cs))!= -1){
System.out.println(new String(cs,0,len));
}
fr.close();
}
}
import javax.imageio.IIOException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;
public class Demo02 {
public static void main(String[] args) {
method01();
}
public static void method01(){
Properties prop = new Properties();
prop.setProperty("张三","18岁");
prop.setProperty("李四","20岁");
prop.setProperty("王五","45岁");
Set<String> set = prop.stringPropertyNames();
Iterator<String> iterator = set.iterator();
while (iterator.hasNext()){
String key = iterator.next();
String value = prop.getProperty(key);
System.out.println(key+" "+value);
}
}
public static void method03(){
FileWriter fw = null;
try {
fw = new FileWriter("D:\\ideaProjects\\Demo_java\\javaTest\\src\\April\\Apr20th\\a.txt",true);
char[] cs = {'z','b','x'};
int len = 0;
for (int i = 0; i < 10; i++) {
fw.write(cs,0,2);
fw.write("\r\n");
}
}catch (IOException e){
System.out.println(e);
}finally {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
import java.io.Serializable;
public class Person implements Serializable {
private String name;
private transient 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;
}
}
import java.io.*;
import java.util.HashMap;
import java.util.Properties;
import java.util.Set;
public class Demo01 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
method11();
}
public static void methodLoad01() throws IOException {
Properties prop = new Properties();
prop.load(new FileReader("javaTest\\src\\April\\Apr21th\\a.txt"));
Set<String> set = prop.stringPropertyNames();
for (String key : set) {
String value = prop.getProperty(key);
System.out.println(key + "" + value);
}
}
public static void method02() throws IOException {
FileOutputStream fos = new FileOutputStream("javaTest\\src\\April\\Apr21th\\a.txt", true);
BufferedOutputStream bos = new BufferedOutputStream(fos);
byte[] b = {12, 32, 65, 97, 48};
bos.write(b);
bos.write("张三".getBytes());
bos.flush();
bos.close();
}
public static void method03() throws IOException {
FileInputStream fis = new FileInputStream("javaTest\\src\\April\\Apr21th\\a.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
byte[] by = new byte[1024];
long s = System.currentTimeMillis();
int len = 0;
while ((len = bis.read(by)) != -1) {
System.out.println(new String(by, 0, len));
}
long e = System.currentTimeMillis();
bis.close();
System.out.println(e - s + "毫秒");
}
public static void method04() throws IOException {
FileWriter w = new FileWriter("javaTest\\src\\April\\Apr21th\\a.txt", true);
BufferedWriter bw = new BufferedWriter(w, 1024);
bw.write("abcde", 0, 5);
bw.newLine();
bw.flush();
bw.close();
}
public static void method05() throws IOException {
FileReader fr = new FileReader("javaTest\\src\\April\\Apr21th\\a.txt");
BufferedReader br = new BufferedReader(fr, 1024);
while (br.readLine() != null) {
System.out.println(br.readLine());
}
br.close();
}
public static void method06() throws IOException {
HashMap<String,String> map =new HashMap<>();
FileReader fr = new FileReader("javaTest\\src\\April\\Apr21th\\read.txt");
FileWriter fw = new FileWriter("javaTest\\src\\April\\Apr21th\\a.txt",true);
BufferedReader br =new BufferedReader(fr);
BufferedWriter bw =new BufferedWriter(fw);
String line ;
while ((line = br.readLine())!=null){
String[] arr = line.split("\\.");
map.put(arr[0],arr[1]);
}
String line2;
for (String key:map.keySet()){
String value = map.get(key);
System.out.println(key+"."+value);
line2 = key+"."+value;
bw.write(line2);
bw.newLine();
}
bw.close();
br.close();
}
public static void method07() throws IOException {
FileOutputStream fw = new FileOutputStream("javaTest\\src\\April\\Apr21th\\a.txt",true);
OutputStreamWriter osw = new OutputStreamWriter(fw,"GBK");
osw.write("张三");
osw.flush();
osw.close();
}
public static void method08() throws IOException {
FileInputStream fis = new FileInputStream("javaTest\\src\\April\\Apr21th\\read.txt");
InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
int len =0;
while ((len = isr.read())!= -1){
System.out.println((char) len);
}
isr.close();
}
public static void method09() throws IOException {
FileInputStream fis = new FileInputStream("javaTest\\src\\April\\Apr21th\\a1.txt");
InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
FileOutputStream fos = new FileOutputStream("javaTest\\src\\April\\Apr21th\\b1.txt");
OutputStreamWriter osw = new OutputStreamWriter(fos,"UTF-8");
int len =0;
while ((len = isr.read())!= -1){
System.out.println((char) len);
osw.write(len);
}
osw.close();
fos.close();
}
public static void method10() throws IOException {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("javaTest\\src\\April\\Apr21th\\a1.txt"));
oos.writeObject(new Person("张三",18));
oos.close();
}
public static void method11() throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("javaTest\\src\\April\\Apr21th\\a1.txt"));
Object o = ois.readObject();
ois.close();
System.out.println(o);
Person p = (Person) o;
System.out.println(p.getName()+" "+p.getAge());
}
}