以内存为参照
package com.io.input;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class FileInputStreamTest {
public static void main(String[] args) {
FileInputStream fs = null;
try {
//创建文件输入流对象
//文件路径 D:java_leaningabc1.text (IDEA会自动变成D:\java_leaning\abc1.txt,因为java中表示转义)
//路径写成 D:/java_leaning/abc1.txt也可以
fs = new FileInputStream("D:\java_leaning\abc1.txt");
//开始读
int data = fs.read(); //这个方法的返回值是,读取到”字节“的本身
while(data != -1) {
System.out.println(data);
data = fs.read();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {//确保流关闭
if (fs != null){//避免空指针异常
try {
fs.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
read(byte[] b)
package com.io.input;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class FileInputStreamTest2 {
public static void main(String[] args) {
FileInputStream fs = null;
try {
//使用相对路径
//IDEA默认当前路径:工程根目录
fs = new FileInputStream("basic/src/com/io/input/tmpfile");
//开始读
byte[] b = new byte[4];//准备一个4个字节的数组,一次最多读取4个字节
int dataCount = fs.read(b); //这个方法的返回值是,读取到”字节“的数量
while(dataCount != -1){
System.out.println("读取到的字节数为:" + dataCount);
System.out.println(new String(b,0,dataCount)); //将byte数组转成字符串
dataCount = fs.read(b);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {//确保流关闭
if (fs != null){//避免空指针异常
try {
fs.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
available()方法:获取剩余未读字节数量
package com.io.input;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileOutputStreamTest {
public static void main(String[] args) {
FileOutputStream fps = null;
try {
// myfile文件不存在会新建
// 这种方式是原文件清空后写入
// fps = new FileOutputStream("myfile");
//以追加的方式写入
fps = new FileOutputStream("myfile",true);
// 开始写
byte[] b = {97,98,99,100};
// 将byte数组全部写出
String a = "hello world";
byte[] bytes = a.getBytes();
fps.write(b);
fps.write(b,0,1);
fps.write(bytes);
fps.flush();
// 写完之后要刷新
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fps != null){
try {
fps.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
文件复制
package com.io;
import java.io.File;
public class Demo01 {
public static void main(String[] args) {
File file = new File("D:\java_leaning\abc.txt"); //获取文件对象,注意路径转义
// File file1 = new File("D:\java_leaning","D:abc.txt"); //另一种构造,使用较少
System.out.println(file.getName());//获取文件名/目录名
System.out.println(file.getPath());//获取文件或文件夹的路径,就是new file时写的路径(绝对/相对)
System.out.println(file.getAbsolutePath());//获取绝对路径
System.out.println(file.getAbsoluteFile());//返回一个用当前文件绝对路径构建的file对象
System.out.println(file.getParent());//返回当前文件/文件夹的父级路径
file.renameTo(new File("D:\java_leaning\abc1.txt"));//给文件/目录重命名
System.out.println(file.exists());//判断是否存在
System.out.println(file.canRead());
System.out.println(file.canWrite());
System.out.println(file.isFile());
System.out.println(file.isDirectory());
file.lastModified();
file.length();//文件长度,单位字节数
package com.io;
import java.io.File;
import java.io.IOException;
public class Demo02 {
public static void main(String[] args) {
File file = new File("D:\java_leaning\xxx.txt");
System.out.println(file.exists());
if (!file.exists()){
try {
file.createNewFile(); //创建文件
} catch (IOException e) {
throw new RuntimeException(e);
}
}
else{
file.delete();//删除
}
File f2 = new File("D:\java_leaning\cc");
f2.mkdir();//创建单层目录
File f3 = new File("D:\java_leaning\dd\ee\ff");
f3.mkdirs();//创建多层目录
File f4 = new File("D:\java_leaning");
for (String s : f4.list()) { //ls
System.out.println(s);
}
File[] files = f4.listFiles();//获取当前目录下所有子文件
}
}
十、序列化与反序列化
序列化
反序列化
transient关键字
序列化版本号



