1 java.io.File
---->1.File类是一个对象,代表一个文件或者一个文件目录
1.如何创建File的实例
newFile(StringfilePath)
newFile(StringfilePath,StringchildPath)
newFile(FileparentFile,StringchildPath)//在parentFile后面的路径
Filefile1=newFile("test.txt");
Filefile2=newFile("C:\\Users\\17520\\Desktop\\Java\\test2.txt");
//注意:在main方法中!!!!!!!!!!!!!!!
publicstaticvoidmain(String[]args){
Filefile=newFile("test.txt");//这里返回的是工程下的路径
System.out.println(file.getAbsolutePath());
//要返回module下的路径,需要下面这种
Filefile2=newFile("InputOutput\\test.txt");
System.out.println(file2.getAbsolutePath());
}
//常用方法
//1.StringgetAbsolutePath():返回绝对路径
Stringpath11=file1.getAbsolutePath();
System.out.println(path11);//C:\Users\17520\Desktop\Java\IDEAProject\InputOutput\test.txt
//2.StringgetPath():返回相对路径
Stringpath21=file1.getPath();
Stringpath22=file2.getPath();
System.out.println(path21);//test.txt
System.out.println(path22);//C:\Users\17520\Desktop\Java\test2.txt
//3.StringgetName():获取文件名(带后缀)
System.out.println(file1.getName());
//4.String getParnet():获取上层文件的目录路径,若无则返回null
System.out.println(file1.getParent());
System.out.println(file2.getParent());//C:\Users\17520\Desktop\Java
//5.longlength():获取文件长度(字节数)
System.out.println(file1.length());
//6.longlastModified():获取最后一次的修改时间,返回毫秒数
System.out.println(file1.lastModified());
//7.String[]list():获取指定路径下的所有文件或者文件目录的名称数组
//8.File[]listFiles():获取指定目录下的所有文件或者文件目录的File数组
//
//9.boolean isDirectory():判断是否是文件目录
//10.boolean isFile():判断是否是文件
//11.boolean exists():判断是否存在
//12.boolean canRead():判断是否可读
//13.boolean canWrite():判断是否可写
//14.boolean isHidden():判断是否隐藏
//15.public boolean createNewFile():创建文件,若文件还在,则不创建,返回false
//16. public boolean mkdir():创建文件目录,
//17.public boolean mkdirs():
2 IO
---->字节流(8bit) / 字符流(16bit)
---->节点流 / 处理流
----> InputStream / OutputStream:抽象类
----> Reader / Writer:抽象类
3 FileReader
//把test.txt的内容输出控制台
//1.实例化File对象,指明遥操作的文件
Filefile=newFile("test.txt");//相较于当前工程
//2.提供具体的流
FileReaderfr=null;
try{
fr = new FileReader(file);
//3.数据的读入
//返回读入的一个字符,如果达到文件末尾,则返回-1
//方式一
int data = fr.read();
while(data!=-1){
System.out.print((char)data);
data=fr.read();
}
}catch(FileNotFoundExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}finally{
//4.流的关闭
try{
if(fr != null){
fr.close();
}
}catch(IOExceptione){
e.printStackTrace();
}
}
---->读入的文件一定要存在,否则抛异常
//对read()方法的操作升级,使用read的重载方法
//对上述代码的读入部分进行修改
char[] cbuf = char[5];
int len;
if((len = fr.read(cbuf)) != -1){
for(int i = 0; i < len; i++){
cbuf[i].sout
}
}
4 FileWriter
//1.提供File类的对象,指明写出到的文件
File file = new File("test.txt);
//2.声明流
FileWriter fw = null;
//3.写数据
try{
fw = new FileWrite(file);
fw.write("helloworld");
fw.write("nihao");
}catch( ) {
}finally{
//关闭流
if(fw != null){
fw.close();
}
}
---->说明:1.输出操作,文件可以不存在,会创建
2.对上述程序再次运行,会覆盖文件
3.如果想在文件后面进行添加,需要使用构造器fw = new FileWrite(file, true);
---->利用上述的操作实现文本的复制
//1.创建File类的对象
File file1 = new File("test1.txt");
File file2 = new File("test2.txt");
//2.创建流
FileReader fr = null;
FileWriter fw = null;
//具体操作
try{
fr = new FileReader(file1);
fw = new FileWriter(file2);
char[] cbuf = new char[5];
int len = 0; //每次读入到cbuf中的数据
//读入缓冲区的总字节数,如果没有更多的数据,则为-1,因为已经到达文件的末尾。
while((len = fr.read(cbuf) != -1){
String str = new String(cbuf, 0 , len);
fw.write(str);
}
}catch(IOException e){
e.printStackTrace();
}finally{
if(fw != null){
fw.close();
}
if(fr != null){
fr.close();
}
}
5 字节流复制图片
注意:
1.对于文本文件(.txt .java .c .cpp)使用字符流,对于非文本文件使用字节流、使用字节流处理文本文件 可能会出现乱码
2.对于文本文件 只进行复制操作可以使用字节流
6 缓冲流
---->BufferedInputStream
---->可以提高流的读取速度
---->是一种处理流,必须作用在节点流上面
---->关闭时,先关外面的流
---->关闭外层时,内层流自动关闭了
---->内部提供了缓冲区,能够提高复制的速度
---->BufferedOutputStream和BufferedWriter内部有一个flush()方法,目的是刷新缓冲区,当显示的调用时,缓冲区不到8192就会被刷新
---->使用bufferedReader / BufferedWriter时的注意点
进行流操作的两种方式:
>方式一:
char[] cbuf = new char[10];
int len;
while((len = bufferedReader.read(cbuf)) != 0){
bufferedWriter.write(cbuf, 0, len);
}
>方式二:
String str;
while((str = bufferedReader.readline()) != null){
bufferedWriter.write(str + "\n");//注意这里不包含换行符,需要手动的添加
}
7 转换流
---->数据字符流
---->提供字节流与字符流的转换
---->InputStreamReader将输入的字节流转换成字符流
---->OutputStreamWriter将输出的字符流转换成字节流
---->字节、字节数组 ---> 字符、字符数组 :解码
---->字符、字符数组 ---> 字节、字节数组 :编码
---->关于字符集:
8 对象流
---->序列化:对象必须是可以序列化的
---->序列化:将内存中的java对象保存到磁盘中,或通过网络传输出去ObjectOutputStream
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("test.txt"));
oos.writeObject(new String("我爱中国");
oos.flush(); //显示的调用
---->反序列化
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test3.txt"));
Object obj=ois.readObject();
String str=(String)obj;
System.out.println(str);
ois.close();
9 自定义序列化
---->一个对象可序列化:1.实现两个接口之一 --->Serializable / Externalizable
2.public static final long serialVersionUID = 453156886134L;
3.除了当前类(Person)实现了接口之外,还必须保证其内部的所有属性也必须是可序列化的(基本数据类型是可序列化的)
4 序列化不能操作,static / transient的数据