构造方法
主要用来创建对象
this关键
- 当本类的成员变量与局部变量同名时,可以用this变量名的方式指定成员变量
- this还可以实现构造方法的调用,但方法之间不可以相互调用,否则回死循环,本调用语句必须写在构造方法的第一行
- this();代表的是调用本类的无参构造
- this(参数);代表的是调用本类对应参数的构造方法
构造代码块
类里方法外,用于提取所以构造方法的共性功能,每一次创建对象时才会触发,并且优先于构造方法执行
io流
文件流
文件在程序中是以流的形式来操作的
输入流:数据从数据源(文件)到程序(内存)的路径
输出流:数据从程序(内存)到数据源的(文件)路径
常用的文件操作
创建文件对象相关构造器和方法
new File(String pathname)//根据路劲构建一个File对象
new File(File parent,String child)//根据父目录文件+子路劲构建
new File(String parent,String child)//根据父目录+子路径构建
createNewFile 创建新文件
##字节输出流
FileOutputStream的创建方式
String filePath = “文件路径”;
FileOutputStream fileOutputStream = null;
fileOutputStream = new FileOutputStream(filePath,true);
fileOutputStream .write(“想要输出的字节”)//写入单个字节
//写入字符串
String str = “hello,word”;
//str.getBytes() 可以把 字符串 -> 字节数组
FileOutputStream.write(str.getBytes)
FileOutputStream.write(str.getBytes,开始数(0),结束数(2))
// 第二种定义数组提高效率
byte[] buf = new byte[8];
int Len =0;
while ((Len = fileOutputStream.read(buf)) != -1){
fileOutputString.write(buf,0,Len)
}
3. 注意new FileOutputStream(filePath) 创建方式,这样会覆盖原来的内容
new FileOutputStream(filePath,true) 创建方式,当写入的内容是,会追加到文件后面