文件创建:File file=new file(路径);
file.mkdir();创建目录 file.createNewFile();创建Txt文档等
mkdir()上层目录不存在,则不创建 mkdirs()上目录不存在,一并创建
为什么IO流或者序列化的定义(InputStream in=null;)需要写在外面?
1.方便关闭流,或者序列化流,2.方便统一进行调用。
IO流(可能抛出IO异常)
流的关闭顺序:先开的后关,后开的先关(close())
字节流:输入(InputStream接口):
int available():返回当前可读的输入字节数
InputStream is=new FileInputStream(路径); 读取字节文件:(b=is.read())!=-1(-1表示没有字节了)System.out.println((char)b);
is.available():读取文件长度
read():读一个字节
byte[] bytes=new byte[fileInputStream.available()];//文件长度
fileInputStream.read(bytes);//可以将字符全读入到bytes中去。
System.out.println(new String(bytes));//字符串形式输出
输出(OutputStream接口):OutputStream os = new FileOutputStream(路径); 写出字节文件:os.write(内容);
OutputStream os = new FileOutputStream(路径,true);true追加到文件,路径中文件不存在则自动创建。
byte[] bytes= {'a','b','c'};
也可以写入字节数组:os.write(bytes);
os.flush():将缓冲区的内容刷到文件去(现在的close()自带该功能)。
缓冲流(不常用)
提高读写速率当读取数据时,数据按块读入缓冲区,之后的读操作则直接访问缓冲区。
字节流:输入(InputStream接口):InputStream is=new
BufferedInputStream(FileInputStream(路径)); 读取字节文件:is.read(bytes))!=-1(-1表示没有字节了)
输出(OutputStream接口):OutputStream os = = new BufferedOutputStream(FileOutputStream(路径)); 写出字节文件:os.write(内容);
字符流:输入(Reader接口):Reader is=new FileReader(路径); 从文件读取字符:is.read(cs))!=-1(-1表示没有字符了)
char[] cs=new char[256];
int num;
while((num=is.read(cs))!=-1) {//读入cs中
System.out.println(new String(cs));
}
输出(Writer接口):Writer os = = new FileWriter(路径,true); 写出字符到文件:
os.write(内容);
true表示追加到文件后,路径中的文件没有则自动创建。
char[] c= {'a','b','c'};
也可以写入字节数组:os.write(c);
缓冲流(常用)
提高读写速率当读取数据时,数据按块读入缓冲区,之后的读操作则直接访问缓冲区。
字符流:输入(Reader接口):Reader is=new BufferedReader(FileReader(路径)); 从文件读取字符:is.readLine(); String s="";
(s=is.readLine())!=null(null表示没有字符了)
System.out.println(s);
public String readLine():读取一行。
输出(Writer接口):Writer os = = new BufferedWriter(FileWriter(路径)); 写出字符到文件:os.newLine():换行 只能覆盖不追加,想追加只能写在FileWriter中,就可以实现。
os.write(内容);
os.flush();将缓冲区的内容刷到文件去
InputStreamReader:字节流输入为字符流(文字):其他方式中文会乱码
int read():读取一个字符
String getEncoding:返回字符编码名
File file=new File("E:\\tmp\\abc.txt");
InputStreamReader iReader=new InputStreamReader(new FileInputStream(file), "utf-8"));
char[] cs=new char[256];
int num;
while((num=iReader.read(cs))!=-1) {
System.out.println(new String(cs));
}
OutputStreamWriter:字节转为字符输出(中文输出):其他方式会乱码,与上面类似
序列化与反序列化(类序列化必须实现Serializable)(可能抛出IO异常):不管是集合还是什么都可以用Object接收或写入
序列化(ObjectOutputStream):将对象写入到IO流中(保存),不能序列化static和 transient 修饰的成员变量,不想被序列化也可以添加transient。
反序列化(ObjectInputStream):从IO流中恢复对象(读取)
序列化实现:ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(路径));
oos.writeObject(对象));(保存对象到文件)
oos.close();(自带flush())
反序列化实现:ObjectInputStream ois =new ObjectInputStream(new FileInputStream(路径));
Object o = ois.readObject();(读取文件中的对象)
String s = (String)o;
System.out.println(s);
Java序列化算法
1.所有保存到磁盘的对象都有一个序列化编码号,自己生成的或者设置serialVersionUID
2.当程序试图序列化一个对象时,会先检查此对象是否已经序列化过,只有此对象从未(在此虚拟机)被序列化过,
才会将此对象序列化为字节序列输出
3.如果此对象已经序列化过,则直接输出编号即可
由于java序利化算法不会重复序列化同一个对象,只会记录已序列化对象的编号。如果序列化一个可变对象(对象内的内容可更改)后,
更改了对象内容,再次序列化,并不会再次将此对象转换为字节序列,而只是保存序列化编号。
IO流及序列化
最新推荐文章于 2023-09-18 22:51:45 发布