一、同步
1、同步代码块
把要实现线程安全的代码放在同步代码块中
java中任何对象内部都有一个开关,你可以理解为标志位,1就是表示关,0就表示开
2、同步方法
二、io
input输入
output输出
计算机中的文件以及文件中的内容
File控制计算机中的文件和文件夹
三、file常用的方法
![](https://img-blog.csdnimg.cn/80e73efd8cda48daa59b470d9781980d.png)
四、编码格式
String和byte转换
![](https://img-blog.csdnimg.cn/f6fbb5a406fa423aa4ede2dea1bea725.png)
五、流
管道、java语言操作文件中得内容就需要流
根据流向去划分:
输入流(向内存,走就是输入)
输出流(从内存往外走,叫输出)
根据传输内容划分
字节流
字符流
字节型文件、字符型文件
txt打开乱码就是字节型文件,不乱码就是字符型文件
根据依赖其他流的程度划分
低级流 不依赖于任何流
高级流 依赖于低级流
字节输入流 InputStream
字节输出流 OutputStream
字符输入流Reader
字符输出流Writer
六、读取文本内容
File file = new File("d:/a/haha.txt");
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
int num = -1;
StringBuilder sb = new StringBuilder();
while((num = fis.read()) != -1) {
sb.append((char)num);
}
System.out.println(sb.toString());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if(fis != null) {
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
七、向文本中写入内容
File file = new File("d:/a/hehe.txt");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
fos.write('a');
fos.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if(fos != null) {
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
八、缓冲
File file = new File("d:/a/haha.txt");
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
byte[] buff = new byte[1024];
int length = -1;
StringBuilder sb = new StringBuilder();
while((length = fis.read(buff)) != -1) {
String str = new String(buff,0,length);
sb.append(str);
}
System.out.println(sb.toString());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if(fis != null) {
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
九、向文本写入很多内容
File file = new File("d:/a/hehe.txt");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file,true);
fos.write("gdasgdystdsgdhwgdyyuw".getBytes());
fos.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if(fos != null) {
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
十、自带缓存
File file = new File("d:/a/haha.txt");
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedInputStream bis = new BufferedInputStream(fis);
十一、对象序列化
存到文件中
Human human = new Human(1, "能能");
File file = new File("d:/a/haha.txt");
FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(human);
oos.flush();
从文件取
File file = new File("d:/a/haha.txt");
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
Human human1 = (Human)ois.readObject();
System.out.println(human1.getId());
System.out.println(human1.getUsername());