文件
File类访问文件属性
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200803192346295.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FpYW5jaHVuMjI=,size_16,color_FFFFFF,t_70)
File类的常用方法
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200803192403213.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FpYW5jaHVuMjI=,size_16,color_FFFFFF,t_70)
补充:
String getParent(); //返回此File对象的路径名的上一级,如果路径没有上一级,则返回null
boolean mkdir(); //创建一个目录,它的路径名由当前File对象指定
boolean mkdirs(); //创建包括父目录的目录
file.lastModified(); //获取文件或目录的最后修改日期
注:
绝对路径货物相对路径:
绝对路径一般从根目录开始,写全路径
相对路径一般从当前目录开始
流
Java流的分类:
1、输入输出流是相对于计算机内存来说的
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200803192448401.png)
2、字节流是 8 位通用字节流,字符流是 16 位 Unicode 字符流
![在这里插入图片描述](https://img-blog.csdnimg.cn/2020080319250929.png)
FileInputStream(输入流)
(一)InputStream类常用方法
int read( ) :从输入流中读取下一个字节数据
int read(byte[] b) :从输入流中读取数据,并将数据存储在缓冲区数组b中,返回实际读取的字节流
int read(byte[] b,int off,int len) :从输入流中读取最多len长度的字节,保存到字节数组b中,保存的位置从off开始
void close( ) :关闭输入流
int available():可以从输入流中读取的字节数目
(二)子类FileInputStream常用的构造方法
FileInputStream(File file)
FileInputStream(String name)
FileOutputStream(输出流)
(一)OutputStream类常用方法
void write(int c) :将指定的字节数据写入此输出流中
void write(byte[] buf): 将数组buf中的所有字节写入次输出流中
void write(byte[] b,int off,int len) :将字节数组中从偏移量off开始的长度为len的字节数据输出到输出流中
void close() :关闭输出流
void flush():强制把缓冲区的数据写到输出流中
(二)子类FileOutputStream常用的构造方法
FileOutputStream (File file)
FileOutputStream(String name)
FileOutputStream(String name,boolean append)
注:
1.前两种构造方法在向文件写数据时将覆盖文件中原有的内容
2.创建FileOutputStream实例时,如果相应的文件并不存在,则会自动创建一个空的文件
FileInputStream读文件的流程:
1、FileInputStream对象和String结果对象声明
2、创建FileInputStream对象(文件路径或File对象)
3、读单字节或整个读到byte数组中
4、转成字符串
5、关闭FileInputStream流
6、返回结果字符串
读文件方法(两种):
//数组的形式
public static String readFile(String path){
FileInputStream fis=null;
String str=null;
try {
fis=new FileInputStream(path);
byte[] b=new byte[fis.available()];
fis.read(b);
str=new String(b);
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return str;
}
//单字节的形式
public static String readByOne(String path){
FileInputStream fis=null;
String str=null;
try {
fis=new FileInputStream(path);
StringBuffer sb=new StringBuffer();
int temp;
while((temp=fis.read())>0){
char c=(char) temp;
sb.append(c);
}
str=sb.toString();
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}finally {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return str;
}
FileOutputStream写文件的流程:
1、File对象装载文件路径
2、判断文件父级目录是否存在,不存在则创建
3、声明FileOutputStream对象
4、创建FileOutputStream对象(File对象,是否追加)
5、把要写的字符串转成byte数组,并写入输出流
6、关闭FileOutputStream流
写文件的方法:
public static void writeFile(String str,String path,boolean isAppend){
File f=new File(path);
if (!f.getParentFile().exists()) {
f.getParentFile().mkdirs();
}
FileOutputStream fos=null;
try {
fos=new FileOutputStream(f,isAppend);
byte[] b=str.getBytes();
fos.write(b);
}catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}