一、IO流
1.1 File类
- 概念:代表物理盘符中的一个文件或文件夹
- 常用方法:
- creatNewFile();//创建一个新文件
- Mkdir();//创建一个新目录
- Delete();//删除文件或空目录
- Exists();//判断File对象所代表的对象是否存在
- getAbsolutePath();//获取文件的绝对路径
- getPath();//获取文件的相对路径
- getName();//获取文件名
- getParent();//获取文件/目录所在的目录
- canRead();//是否可读
- canWrite();//是否可写
- isDirectory();//是否是目录
- isFile();//是否是文件
- length();//获取文件的长度
- listFiles();//列出目录中的所有内容
- renameTo();//修改文件名
1.2 流
- 概念:内存与存储设备之间传输数据的通道
- 流的分类:
- 按方向:输入流和输出流
- 按单位:字节流和字符流
- 按功能:结点流和过滤流
1.2.1 基本字节流
- 基本字节输入流:InputStream–>FileInputStream
public static void main(String[] args) throws IOException {
InputStream is=null;
try {
is=new FileInputStream("b.txt");
int content=is.read();
while (content!=-1) {
System.out.print((char)content);
content=is.read();
}
System.out.println("\n读取完毕");
} catch (Exception e) {
e.printStackTrace();
}finally {
if (is!=null) {
is.close();
}
}
}
- 基本字节输出流:OutputStream–>FileOutputStream
public static void main(String[] args) throws IOException{
OutputStream os=null;
try {
os=new FileOutputStream("a.txt", true);
os.write("Hello world".getBytes());
os.write("java".getBytes());
System.out.println("写入成功");
} catch (Exception e) {
e.printStackTrace();
}finally {
if (os!=null) {
os.close();
}
}
}
1.2.2 字节缓冲流
- 字节缓冲输入流:FileInputStream–>BufferedInputStream
public static void main(String[] args) throws IOException {
BufferedInputStream bis=null;
try {
bis=new BufferedInputStream(new FileInputStream("a.txt"));
byte[] b=new byte[10];
int len=bis.read(b);
while (len!=-1) {
String content=new String(b, 0, len);
System.out.println(content);
len=bis.read(b);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if (bis!=null) {
bis.close();
}
}
}
- 字节缓冲输出流:FileOutputStream–>BufferedOutputStream
public static void main(String[] args) throws IOException {
BufferedOutputStream bos=null;
try {
bos=new BufferedOutputStream(new FileOutputStream("a.txt",true));
bos.write("i like qianfeng,".getBytes());
bos.write("i like china".getBytes());
System.out.println("写入成功");
} catch (Exception e) {
e.printStackTrace();
}finally {
if (bos!=null) {
bos.close();
}
}
}
1.2.3 基本字符流
- 基本字符输入流:Reader–>InputStreamReader
- 基本字符输出流:Writer–>OutpitStreamWriter
1.2.4 字符缓冲流
- 字符缓冲输入流:BufferedReader
- 字符缓冲输出流:BufferedWriter
1.2.5 总结
1.3 独特流
1.3.1 打印流PrintStream
public static void main(String[] args) {
PrintStream ps1=null;
try {
ps1=new PrintStream("d1.txt");
ps1.append("我爱千锋");
ps1.printf("%.2f", 3.123234344343);
ps1.println("我爱北京");
ps1.print("我爱中国");
ps1.write("哈哈".getBytes());
System.out.println("写入成功");
System.out.append("1111");
System.out.write("dfefe".getBytes());
} catch (Exception e) {
e.printStackTrace();
}finally {
if (ps1!=null) {
ps1.close();
}
}
}
1.3.2 输出重定向
- 给流重新定义一个方向输出,将原本在控制太台输出的内容转出到文件中
public static void main(String[] args) throws FileNotFoundException {
System.setOut(new PrintStream("f1.txt"));
System.out.println("不忘初心");
System.out.println("方得始终");
}
1.3.3 输入重定向
- 给流重新定义一个方向接受数据,将原来控制台录入的数据转到文件中接收
public static void main(String[] args) throws FileNotFoundException {
System.setIn(new FileInputStream("f1.txt"));
Scanner input=new Scanner(System.in);
System.out.println("接收数据:");
String s1=input.next();
String s2=input.next();
System.out.println("接收的数据为:"+s1);
System.out.println("接收的数据为:"+s2);
}
1.3.4 对象流
- 序列化:将对象用输出流写入文件中的过程
- 反序列化:从文件中读取对象的过程
- 对象输入流:ObjectInputStream
- 对象输出流:ObjectOutputStream
- 实现了java.io.Serializable接口的对象才能被序列化,一般需要固定版本号
象输出流:ObjectOutputStream - 实现了java.io.Serializable接口的对象才能被序列化,一般需要固定版本号
- 如果序列化类中某个属性不想被序列化,可以设置属性为static或transient