zoukankan      html  css  js  c++  java
  • 指定一个文件夹自动计算出其总容量 并且进行目录下文件的添加 与指定文件的访问

    代码实现:

    //编写一个程序,指定一个文件夹,能自动计算出其总容量
    import java.io.*;
    public class Denglu
    {
    public static void main(String[] args) throws IOException
    {
    try
    {
    InputStreamReader isr=new InputStreamReader(System.in);
    BufferedReader inp=new BufferedReader(isr);//进行字节字符转换 用于键盘输入
    String sdir="C:\Users\888888\Desktop\05.StreamAndFileSourceCode\File";//文件
    int count=0;//计算文件数目
    File fdir1=new File(sdir);//创建文件夹
    if(fdir1.exists()&&fdir1.isDirectory())
    {
    System.out.println(sdir+"存在");
    String[] f1=fdir1.list();//列表 下面区分list()和listFile()
    /*File f=new File("c:\");
    String[] f1=f.list();
    File[] f2=f.listFiles();
    ① list() 返回一个字符串数组,这些字符串指定此抽象路径名表示的目录中的文件和目录。
    以C盘为例,返回的是c盘下文件夹名字的字符串数组,如[TEMP, Windows]
    ②listFiles() 返回一个抽象路径名数组,这些路径名表示此抽象路径名表示的目录中的文件。
    以C盘为例返回的是C盘下文件夹目录地址,如[c:TEMP, c:Windows]*/

    for(int i=0;i<f1.length;i++)
    {
    System.out.println(f1[i]);//输出文件夹下的文件
    count++;
    }
    System.out.println("一共有"+count+"文件");

    //在目录下添加文件 名字jiahui
    File fdir2=new File("C:\\Users\\888888\\Desktop\\05.StreamAndFileSourceCode\\File\jiahui");
    if(!fdir2.exists())
    {
    fdir2.mkdir();//如果不存在就创建 肯定不存在你指定的
    }
    count=0;
    System.out.println("建立新的文件夹后: ");
    for(int i=0;i<f1.length;i++)
    {
    System.out.println(f1[i]);//再一次输出目录下的问价及总数
    count++;
    }
    System.out.println("一共有"+count+"文件");
    }
    System.out.println("请输入一个文件名字:");//对其中一个文件属性访问
    String sfile=inp.readLine();
    File ffile=new File(fdir1,sfile);//用一个已经存在代表某磁盘文件夹的fdir1对象作为文件夹 以sfile作为文件名字
    if (ffile.isFile())
    {
    System.out.println(ffile.getName());
    System.out.println(ffile.getPath());
    System.out.println(ffile.length());
    }

    inp.close();
    }
    catch(IOException e)
    {
    System.out.println(e.toString());
    }


    }
    }

    运行结果:(区分list()与listFile()   ,此结果为list())

  • 相关阅读:
    Solution: Win 10 和 Ubuntu 16.04 LTS双系统, Win 10 不能从grub启动
    在Ubuntu上如何往fcitx里添加输入法
    LaTeX 笔记---Q&A
    Hong Kong Regional Online Preliminary 2016 C. Classrooms
    Codeforces 711E ZS and The Birthday Paradox
    poj 2342 anniversary party
    poj 1088 滑雪
    poj 2479 maximum sum
    poj 2481 cows
    poj 2352 stars
  • 原文地址:https://www.cnblogs.com/mm20/p/7779273.html
Copyright © 2011-2022 走看看