File类文件与输入输出流小结
?
???????????? File类文件与输入输出流小结
File(String?pathname)
??????????通过将给定路径名字符串转换为抽象路径名来创建一个新 File 实例。
File(String?parent, String?child)
??????????根据 parent 路径名字符串和 child 路径名字符串创建一个新 File 实例。
File(URI?uri)
??????????通过将给定的 file: URI 转换为一个抽象路径名来创建一个新的 File 实例。
File类中常用的方法:
?
booleanexixts()判断文件是否存在.??
StringgetName()获取文件的名字.??
booleancanRead()判断文件是否是可读的.??
booleancanWrite()判断文件是否可被写入.???
longlength()获取文件的长度(单位是字节).??
StringgetAbsolutePath()获取文件的绝对路径.??
StringgetParent()获取文件的父目录.??
booleanisFile()判断文件是否是一个正常文件,而不是目录.??
booleanisDirectroy()判断文件是否是一个目录.??
booleanisHidden()判断文件是否是隐藏文件.??
longlastModified()获取文件最后修改的时间时间是从1970年午夜至文件最后修改时刻的毫秒数.??
booleanmkdir()??创建一个目录,如果创建成功返回true,否则返回false(如果该目录已经存在将返回false).??
File方法的应用
用File类中的方法和递归调用来统计某一路径下的文件个数及文件夹个数
public class FileCount {
?
???????? public static void main(String[] args) {
?
?????????????????? String pat = "H:\\JAVA学习";
?????????????????? FileCount fs = new FileCount();
?????????????????? int result = fs.countFile(pat);
?????????????????? int result1 = fs.countDirectory(pat);
?????????????????? System.out.println("统计 结果:该文件夹中共有文件" + result);
?????????????????? System.out.println("统计结果:该文件夹中共有文件夹"+result1);
????????
???????? }
?
???????? public int countFile(String pat) {
?????????????????? int count = 0;
?????????????????? // 根据路径创建一个文件对象
?????????????????? java.io.File fil = new java.io.File(pat);
?????????????????? // 判断文件是否存在
?????????????????? if (!fil.exists()) {
??????????????????????????? System.out.println("给定的文件路径不存在!!");
??????????????????????????? return 0;
?????????????????? }
?
?????????????????? // 如果存在的话,就将这个文件夹下面的所有文件对象装入数组中
?????????????????? java.io.File[] fs = fil.listFiles();
?????????????????? if (fs == null) {
??????????????????????????? System.out.println("给定的文件路径不是文件夹!!");
??????????????????????????? return 0;
?????????????????? }
??????????????????????????? // 遍历文件数组
??????????????????????????? for (int i = 0; i < fs.length; i++) {
???????????????????????????????????? // 得到一个文件
???????????????????????????????????? java.io.File f = fs[i];
???????????????????????????????????? // 得到文件路径
???????????????????????????????????? String st = f.getAbsolutePath();
???????????????????????????????????? // 如果找到的是一个标准文件
???????????????????????????????????? if (f.isFile()) {
?????????????????????????????????????????????? count++;
?????????????????????????????????????????????? // 如果找到的是一个文件夹
???????????????????????????????????? } else if (f.isDirectory()) {
??????????????????????????????????????????????
?????????????????????????????????????????????? // 递归调用
?????????????????????????????????????????????? count +=countFile(st);???????????????????????????????? }
??????????????????????????? }
?????????????????? return count;
???????? }
???????? public int countDirectory(String pat) {
?????????????????? int count = 0;
?????????????????? // 根据路径创建一个文件对象
?????????????????? java.io.File file = new java.io.File(pat);
?
?????????????????? // 判断文件是否存在
?????????????????? if (!file.exists()) {
??????????????????????????? System.out.println("给定的文件路径不存在!!");
??????????????????????????? return 0;
?????????????????? }
?
?????????????????? // 如果存在的话,就将这个文件夹下面的所有文件对象装入数组中
?????????????????? java.io.File[] fs1 = file.listFiles();
?????????????????? if (fs1 == null) {
??????????????????????????? System.out.println("给定的文件路径不是文件夹2!!");
??????????????????????????? return 0;
?????????????????? }
??????????????????????????? ?//遍历文件数组
??????????????????????????? for (int i = 0; i < fs1.length; i++) {
???????????????????????????????????? // 得到一个文件
???????????????????????????????????? java.io.File f = fs1[i];
???????????????????????????????????? // 得到文件路径
???????????????????????????????????? String st = f.getAbsolutePath();
???????????????????????????????????? // 如果找到的是一个标准文件
???????????????????????????????????? if (f.isDirectory()) {
?????????????????????????????????????????????? count++;
?????????????????????????????????????????????? // 递归调用
?????????????????? ??????????????????????????? count+= countDirectory(st);
???????????????????????????????????? }
??????????????????????????? }
?????????????????? return count;
???????? }
}
?
?
?
二、输入输出流的基本应用
?
在程序中所有的数据都是以流的方式进行传输或保存的,程序需要数据时要使用输入流(InputStream)读取数据,当程序需要将一些数据保存起来时,就要使用输出流(OutputStream)。
InputStream
?
AudioInputStream, ByteArrayInputStream, FileInputStream, FilterInputStream, InputStream, ObjectInputStream, PipedInputStream, SequenceInputStream, StringBufferInputStream
基本方法
?
Intavailable()
??????????返回此输入流下一个方法调用可以不受阻塞地从此输入流读取(或跳过)的估计字节数
?void?close()
??????????关闭此输入流并释放与该流关联的所有系统资源。
?
?voidmark(int?readlimit)
??????????在此输入流中标记当前的位置。
?
?booleanmarkSupported()
??????????测试此输入流是否支持 mark 和 reset 方法。
?
?int read() 从输入流中读取数据的下一个字节。
?
?
?intread(byte[]?b)
从输入流中读取一定数量的字节,并将其存储在缓冲区数????????????????????????? 组 b 中。
?
?intread(byte[]?b, int?off,int?len)
??????????将输入流中最多 len 个数据字节读入 byte 数组。
longskip(long?n) 跳过和丢弃此输入流中数据的 n 个字节。
?
使用FileInputStream子类来实现超类inputstream,其方法与超类中的方法完全相同。
void close()
??????????关闭此输出流并释放与此流有关的所有系统资源。
?
void flush()
??????????刷新此输出流并强制写出所有缓冲的输出字节。
?
?void write(byte[]?b)
??????????将 b.length 个字节从指定的 byte 数组写入此输出流。
?
void write(byte[]?b,int?off, int?len)
??????????将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此输出流。
void write(int?b)
??????????将指定的字节写入此输出流。
?
同样outputstream也是一个抽象类可以用其直接子类Fileoutputstream
实现其方法:
两者应用举例:复制文件(输入输出流)
?
/**
?* i/o的应用
?*
?* @author Administrator
?*
?*/
public class IoTest {
???????? // 程序入口
???????? public static void main(String args[]) {
?????????????????? String srcpath = "(源文件路径)G:\\图片\\五子棋棋盘.PNG";
?????????????????? String despath = "(复制后文件路径)G:\\图1.png";
?????????????????? IoTest it = new IoTest();
???????? ???????? it.copyFile(srcpath,despath);
???????? }
?
???????? /**
???????? ?* 创建一个文件复制的方法
???????? ?*
???????? ?* @param srcpath
???????? ?*??????????? 要复制的文件的路径
???????? ?* @param destpath
???????? ?*??????????? 复制出的文件路径
???????? ?* @return
???????? ?*/
???????? public boolean copyFile(String srcpath, String destpath) {
?????????????????? try {
??????????????????????????? // 根据文件创建一个文件输入流
??????????????????????????? java.io.FileInputStream fis = newjava.io.FileInputStream(srcpath);
??????????????????????????? // 创建一个文件输出流
??????????????????????????? java.io.FileOutputStream fos = newjava.io.FileOutputStream(
?????????????????????????????????????????????? destpath);
??????????????????????????? // 读出一个字节
??????????????????????????? int t = fis.read();
?
??????????????????????????? while (t != -1) {// 则表明该文件没有到末尾
???????????????????????????????????? // 读出流
??????????????????????????? ???????? fos.write(t);
???????????????????????????????????? t = fis.read();
??????????????????????????? }
??????????????????????????? // 关闭输入流
??????????????????????????? fis.close();
??????????????????????????? // 强制输出
??????????????????????????? fos.flush();
??????????????????????????? // 关闭输出流
??????????????????????????? fos.close();
??????????????????????????? System.out.print("文件复制成功!!!");
?????????????????? } catch (Exception ef) {
??????????????????????????? System.out.println("你指定的文件不存在!!!");
?????????????????? }
?????????????????? return false;
???????? }
}