1、File类
文件和目录路径名的抽象表示
一个File类的对象,表示计算机硬盘上的一个文件和目录
File类的构造方法
File f1 = new file("文件路径");//在F盘目录下创建一个与文件名相关联的文件对象
File类的常用方法
public class FileDemo1 {
public static void main(String[] args) {
File f1 = new File("F:/demo.txt");
System.out.println(f1.canRead());//true
System.out.println(f1.canWrite());//true
System.out.println(f1.getPath());//F:\demo.txt
System.out.println(f1.getName());//demo.txt 获得文件的名称
System.out.println(f1.getParent());//F:\
System.out.println(f1.length());//9 获得文件的长度 文件内容长度,以字节为单位
System.out.println(f1.isFile());//true 判断是否为文件
System.out.println(f1.isDirectory());//false 判断是否为目录
System.out.println(f1.isAbsolute());//true
System.out.println(f1.isHidden());//false
System.out.println(f1.exists());//true 判断文件是否存在
System.out.println(f1.lastModified());//1646029426570
System.out.println(new Date(1639485687368L));//Tue Dec 14 20:41:27 CST 2021
File f1 = new File("F:/demo");
//f1.mkdir();//只能创建单级文件夹
//f1.mkdirs();//创建多级文件夹
f1.delete();//一次只能删除一级文件夹,文件中必须为空的.
}
}
2、IO流分类
I——>Input 输入 把电脑硬盘上的文件(数据)读入到程序中
O——>Output 输出 把程序中的数据写出到目标地址中
字节流:每次读取数据时是以字节为单位书写
字节输入流: FileInputStream 字节输出流:FileOutputStream
字符流:每次以字符为单位进行读写操作,字符流只能读取文本文件(txt,java,html)
字符输入流:FileReader 字符输出流:FileWriter
public class Demo1 {
public static void main(String[] args) {
/*
将F盘的demo.txt文件复制E盘中demo.txt
*/
FileInputStream in = null;
FileOutputStream out = null;
try {
//创建字节输入流对象,接入到指定文件上,如果找不到文件,会报异常
in = new FileInputStream("F:/demo.txt");
//创建一个字节输出流对象,同时创建一个空文件,如果路径不对,会报异常
// FileOutputStream out = new FileOutputStream("E:/demo.txt");
out = new FileOutputStream("F:/demo1.txt");
int b =0;
while (( b = in.read())!=-1){
out.write(b);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("文件找不到");
} catch (IOException e) {
e.printStackTrace();
System.out.println("读或写异常");
}finally {
try {
if (in!=null){
in.close();
}
if (out!=null){
out.close();
}
}catch (IOException ex){
}
}
}
}
in.read() 返回int类,表示独到的那一个字节,如果文件内容读完了,返回-1
int b = read() 返回一个int值,表示每次读到的一个字节单位,读完返回-1
int c = in.read() 返回的也是int值,表示实际向数值中装入的字节个数,读完返回-1
节点流:流对象中直接包含的就是数据,对数据进行操作
处理流:处理流中包含的是其他流对象(节点流),可以缓冲功能
创建处理流(包装流),底层有一个缓冲数组,每次将读到的数据先存入到缓冲数组中,等缓冲数组满了之后在实际操作,以自己定义的数组长度大于缓冲数组,则缓冲区失效
public class Demo3 {
public static void main(String[] args) throws IOException {
//节点流:直接包含数据,对数据进行操作
InputStream in = new FileInputStream("F:/demo.txt");
OutputStream out = new FileOutputStream("F:/demo1.txt");
//创建处理流(包装流)底层有一个缓冲数组,每次将读到的数据先存入到缓冲数组中,等缓冲数组满了之后在实际操作,
//一旦自己定义的数组长度大于缓冲区数组大小,缓冲区数组就失效了
BufferedInputStream bin = new BufferedInputStream(in);
BufferedOutputStream bout = new BufferedOutputStream(out);
byte [] bytes = new byte[10224];
int size = 0;
while ((size=bin.read(bytes))!=-1) {
bout.write(bytes, 0, size);
}
bin.close();
bout.flush();//刷新缓冲区
bout.close();
}
}
3、字符流
每次读取单位为字符,字符流只能读取文本文件(txt,java,html...)
计算机上的文件存储都是字节,字符流每次之所以能读到一个字符,通过转换流,将读到的字节结合指定的字符编码转换为字符。
reader——>inputStreamReader(转换流)——>FileReader
Writer——>OutputStreamWriter(转换流)——>FileWriter
read() 每次直接可以读到一个字符的编码,一个中文由3个字节组成
readLine()每次读一行数据,读完返回null
public class CharDemo1 {
public static void main(String[] args) throws IOException {
//字符流 读取字符
/*FileReader reader = new FileReader("F:/demo.txt");
FileWriter writer = new FileWriter("F:/demo1.txt");
int c = 0;
while ((c = reader.read())!=-1){
// System.out.println(c);//20320 22909
writer.write(c);
}
reader.close();
writer.close();*/
FileReader reader = new FileReader("F:/demo.txt");
FileWriter writer = new FileWriter("F:/demo1.txt");
int size = 0;
char[] chars = new char[100];
while ((size = reader.read(chars))!=-1){
// System.out.println(c);//20320 22909
writer.write(chars,0,size);
System.out.println(size);
}
reader.close();
writer.close();
}
}
4、print(打印)流
print 打印流:只做输出没有输入,单向从程序向外输出
打印流分为字节打印流和字符打印流
printWriter:字符打印流 ,print流可以打印各种类型数据
public class PrintWriterDemo {
public static void main(String[] args) throws FileNotFoundException {
PrintWriter out = new PrintWriter("F:/demo.html");
out.print("<h1>ssss</h1>");
out.print("<h1>hhhhhhhhhh</h1>");//print 打印不换行 底层都用的是write()
out.println("<h1>aaaa</h1>");//println 会换行
out.println("<h1>wwww</h1>");
out.write("<h1>sdcf</h1>");
out.close();
}
}
5、对象输入输出流
对象输入输出流,将对象信息,写出到文件中,做到持久保存(序列化)
对象:程序运行时,创建的对象
public class ObjectStreamDemo {
public static void main(String[] args) throws IOException, ClassNotFoundException {
/*
对象输出
*/
/* Date date = new Date();
String s = "abcd";
FileOutputStream fout = new FileOutputStream("F:/obj.txt");
ObjectOutputStream out = new ObjectOutputStream(fout);
out.writeObject(date);
out.writeObject(s);//对象序列化,将程序运行时,创建的对象输出到指定的文件,持久保存
out.close();*/
/*
对象输入
*/
FileInputStream in = new FileInputStream("F:/obj.txt");
ObjectInputStream oin = new ObjectInputStream(in);
Date date = (Date)oin.readObject();//对象反序列化,这也是java创建对象的方式之一
String s = (String)oin.readObject();
System.out.println(date);
System.out.println(s);
}
}