mark
UTF - 8 它是一种变长的字符集
表示:单字节来表示字母,双字节来表示一些希腊字母,三字节来表示汉字,当然也有四字节的 这么做当然会增加表示和识别的难度,不过,可以节省空间。这也是为什么utf-8在网络编码中流行的原因。
File
定义:表示了磁盘上的文件或者目录
File f = new File("/Users/liyang/Documents");
f.isDirectory();//是否是个目录
f.isFile();//是否是个文件
f.mkdir();//创建目录,只能创建一级目录
f.mkdirs();//创建多级目录
f.createNewFile();//创建文件
f.list();//当前目录下的文件和目录名字
f.listFiles();//返回当前目录的File对象
f.delete();//删除File
File[] files = f.listFiles(**new** FilenameFilter(){//策略模式,这里用的匿名内部类
@Override
public boolean accept(File dir, String name) {
if (name.startsWith("git")){
return true;
}
return false;
}
});
for (File ff : files){
System.out.println(ff.getName());
}```
###warning
- windows平台的路径分隔符号“\”
- others平台的路径分隔符为"/"
File.separator;//解决上面的问题
#流
- 分类
1.功能上分类:输入流和输出流
2.处理数据单位不同:字节流和字符流
3.按功能的不同:节点流和处理流
| | 字节流 | 字符流|
| :----: | :----: | :-----: |
|输入流 | InputStream | Reader|
|输出流 | OutputStream | Writer|
---
#字节流
#FileInputStream
File f = new File("/Users/liyang/Documents/test.txt");
InputStream in = new FileInputStream(f);
byte[] buffer = new byte[200];
int length = 0;
while(-1 != (length = in.read(buffer))){//将文件读到buffer里面,长度每次为200,直到最后一次小于200
String str = new String(buffer,0,length);
System.out.println(str);
}
in.close();
#FileOutputStream
File f = new File("/Users/liyang/Documents/page2.jpg");
File cf = new File("/Users/liyang/Documents/copypage2.jpg");
cf.createNewFile();
InputStream in = new FileInputStream(f);
OutputStream out = new FileOutputStream(cf);
byte[] buffer = new byte[200];
int length = 0;
while(-1 != (length = in.read(buffer))){//将文件读到buffer里面,长度每次为200,直到最后一次小于200
out.write(buffer,0,length);//这里一定,最好是用这种方式,这个方法是将整个字节数组指定长度的内容都输入进去
String str = new String(buffer,0,length);
System.out.println(str);
}
out.flush();
in.close();
out.close();
#BufferedOutputStream
File f = new File("1.txt");
OutputStream out = new FileOutputStream(f,true);//true表示追加
out = new BufferedOutputStream(out);//缓冲流
out.write("http://www.baidu.com".getBytes());
out.flush();
out.close();
#DataOutputStream And DataInputStream
//DataOutputStream 以二进制的方式写入基本数据类型,生成的也是二进制文件
String filePath = "/Users/liyang/Documents/test.txt";
DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(filePath)));
byte b = 4;
int i =12;
char ch = 'a';
float f = 4.4f;
dos.write(b);
dos.writeInt(i);
dos.writeChar(ch);
dos.writeFloat(f);
dos.flush();
dos.close();
DataInputStream ios = new DataInputStream(new BufferedInputStream(new FileInputStream(filePath)));
System.out.println(ios.readByte());
System.out.println(ios.readInt());
System.out.println(ios.readChar());
System.out.println(ios.readFloat());
ios.close();
---
#字符流
#OutputStreamWriter And InputStreamReader 字节流和字符流的转化
String filePath= "/Users/liyang/Documents/test1.txt";
FileOutputStream f = new FileOutputStream(filePath,true);
OutputStreamWriter or= new OutputStreamWriter(f);//字符流和字节流的转换桥梁
BufferedWriter bw = new BufferedWriter(or);
bw.write("ss来了");
bw.flush();
bw.close();
InputStream in =new FileInputStream(filePath);
Reader r = new InputStreamReader(in);
BufferedReader br = new BufferedReader(r);
String rStr = br.readLine();
System.out.println(rStr);
InputStreamReader ins = new InputStreamReader(System.in);//键盘输入标准输入流
BufferedReader br = new BufferedReader(ins);
String tmpStr;
while (null != (tmpStr = br.readLine())){
System.out.println(tmpStr);
}
br.close();
#FileWriter And FileReader
String filePath= "/Users/liyang/Documents/test1.txt";
Writer w = new FileWriter(filePath,true);
BufferedWriter bw = new BufferedWriter(w);
bw.write("我颠覆流整个世界,只为摆正你的倒影");
bw.write("\n");
bw.flush();
bw.close();
Reader r = new FileReader(filePath);
BufferedReader br = new BufferedReader(r);
String str;
while (null != (str = br.readLine()) ){
System.out.println(str);
}
br.close();
#ObjectOutputStream And ObjectInputStream
###对象序列化,对象要序列化需要实现java.io.Serializable接口
定义:将对象转换为字节流保存起来,并在以后还原这个对象,这种机制叫做对象序列化
mark:将一个对象永久存储到设备上称为持久化。
warning:
1.只能序列化非静态变量,不能序列化任何静态变量
2.序列化成员变量,如果成员变量是一个对象,对象也必须可序列化,否则出错
3.当成员变量用transient修饰的时候,序列化的时候就会跳过该属性的序列化
public class test {
private static ObjectInputStream oi;
public static void main(String[] args) throws Exception {
Person p1 = new Person(20,"lilei",169);
Person p2 = new Person(21,"lilei1",180);
Person p3 = new Person(22,"lilei2",179);
//序列化
String filePath = "/Users/liyang/Documents/testSerializable.txt";
OutputStream out = new FileOutputStream(filePath);
ObjectOutputStream os = new ObjectOutputStream(out);
os.writeObject(p1);
os.writeObject(p2);
os.writeObject(p3);
os.close();
//反序列化
InputStream in = new FileInputStream(filePath);
oi = new ObjectInputStream(in);
p1 = (Person)oi.readObject();
p2 = (Person)oi.readObject();
p3 = (Person)oi.readObject();
System.out.println(p1);
System.out.println(p2);
System.out.println(p3);
}
}
//MARK:实现Serializable这个标记接口就可以将对象序列化了
class Person implements Serializable{
int age;
String name;
transient float height; // 如果transient修饰后的属性,反序列化的时候就会是默认值
Person(int age,String name,float height){
this.age = age;
this.name = name;
this.height = height;
}
public String toString(){
return "age:" + this.age + "name:" + this.name + "height:"+this.height;
}
}
网友评论