1 回顾
1.1 File
1.1.1文件的基本操作
- 创造文件
- 删除文件
- 修改文件名称
- 文件查询
- 递归思想
1.1.2 杨辉三角
/*递归*/
public class Yanghui {
public static int f(int row, int col) {
if (col == 0 || row == col) return 1;
return f(row - 1, col - 1) + f(row - 1, col);
}
public static void main(String[] args) {
for (int row = 0; row < 10; row++) {
for (int col = 0; col <=row; col++) {
System.out.print(f(row, col) + "\t");
}
System.out.println();
}
}
}
// public static void main(String[] args){
// int[][] a =new int[10][10];
//
// for(int row = 0;row < a.length; row++){
// for(int col = 0;col<= row;col++){
// if(col==0||col==row){
// a[row][col]=1;
// }else{
// a[row][col]= a[row-1][col-1] + a[row-1][col];
// }
// }
// }
// for(int row = 0;row < a.length; row++){
// for(int col = 0;col<= row;col++){
// System.out.println(a[row][col]+"\t");
// }
// System.out.println();
// }
1.2 流(Strem)
1.2.1 方向
以内存为参照
输入流
输出流
1.2.2 大小
字节流/字符流
中文:编码
2 字符流
字符流实际上是包装后的字节流
package stream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
public class MenuWrite {
private static PrintWriter pw;
private static int num = 0;
static {
try {
pw = new PrintWriter("D:\\idea-workspace\\hello\\hello\\FF0616\\resource\\cmnue.text");
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
}
public static void scan(File file){
if (file.isFile()){
String fileName = file.getName();
//写入文件
pw.println(++num + ":" + fileName);
pw.flush();
return;
}
File[]files = file.listFiles();
if(files != null){
for(File f : files){
scan(f);
}
}
}
}
字符流实际上是包装后的字节流
public class PrintWriter extends Writer{}
2.1.1 字符流的输入
3 对象流
存储一个对象
student
package stream;
import java.io.Serializable;
public class Student implements Serializable {
private String name;
private String gender;
private Integer age;
private String country;
private String job;
public Student(String name, String gender, Integer age, String country, String job) {
this.name = name;
this.gender = gender;
this.age = age;
this.country = country;
this.job = job;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
@Override
public String toString() {
return "Student{" +
"姓名='" + name + '\'' +
", 性别='" + gender + '\'' +
", 年龄=" + age +
", 国籍='" + country + '\'' +
", 工作='" + job + '\'' +
'}';
}
}
StudentObjectStream
package stream;
import java.io.*;
public class StudentObjectStream {
public static void main(String[] args) throws Exception{
String path = "D:\\idea-workspace\\hello\\hello\\FF0616\\resource\\stu.dat";
ObjectOutput out = new ObjectOutputStream(new FileOutputStream(path));
Student s = new Student("萧逸","男",18,"中国","助教");
out.writeObject(s);
out.close();
ObjectInputStream in = new ObjectInputStream(new FileInputStream(path));
Student stu = (Student) in.readObject();
System.out.println(stu.getName());
}
}
3.1 对象
如何存取多个数据