• 通过代码实现创建、删除、文件的读、写


    创建一个文件

    String s="d:\Test.txt";         //在D盘下创建一个Test文件 .txt为后缀
            File f=new File(s);         //构建一个文件对象。此时没有执行
            f.createNewFile();         //创建一个新文件
            

    创建文件夹

            String mulu="d:\Test";        //在d盘下创建名为Test 的文件夹
            File wj=new File(mulu);
            wj.mkdir();

    删除文件或文件夹

            String f="d:\Test";   //删除文件夹
            //String f="d:\Test.txt";     //删除文件
            File s=new File(f);
            s.delete();
            

    将文件夹下的文件夹或者子文件夹列出来

            String s="f:\英雄联盟";
            File f=new File(s);
            File[] list = f.listFiles();        //获取到的值放到数组中,用for循环,将其打印出来
            for (int i=0; i<list.length;i++){
                //String n=list[i].getName();        //getName()获取当前文件夹下所有的文件名
                String n=list[i].getPath();        //getPath()带有文件夹路径
                
                if(list[i].isDirectory()){          //isDirectory()判断是否为文件夹
                    System.out.print("【目录】");
                }
                else if(list[i].isFile()){             //isFile()判断是否为文件      exists()判断是否有该文件或路径是否存在
                    System.out.print("【文件】");
                }
                System.out.println(n);
            }

    重命名或者移动文件(将原文件删除,重新建一个文件出来)

    String a="d:\Test1.txt";
            String b="d:\Test10.txt";
            File m=new File(a);
            File n=new File(b);
            m.renameTo(n);

    读、写   字符流

            //写入  字符流
            String ad="d:\Test.txt";
            FileWriter w=new FileWriter(ad,true);//括号中如果只有一个参数,则覆盖,逗号后加true  即追加,在其后面追加,不覆盖
            w.write("Hello Word");
            w.close();
            
            //读取  字符流
            String a="d:\Test.txt";
         File n=new File(a); FileReader reader
    =new FileReader(n); char[] s=new char [(int) n.length()]; reader.read(s); String str =new String(s); System.out.println(str); reader.close();

    读取结果如下

     读、写    字节流

             //读取  字节流    (输入)
            String s="d:\Test.txt";
         File n=new File(s); FileInputStream stre
    =new FileInputStream(n); byte[] bb=new byte[(int)n.length]; stre.read(bb); stre.close(); //读取出的值为二进制字节,故而要转化成字符串 String str=new String(bb); System.out.println(str); //写入 字节流 (输出) String str = " 今天挺好。 春运来了。"; String s = "d:\Test.txt"; FileOutputStream stream = new FileOutputStream(s,true); //不加ture的话是覆盖,加ture是追加
    byte[] bbb = str.getBytes(); stream.write(bbb); stream.close();

     Scanner 读取文件

            String a="d:\Test.txt";
            File n=new File(a);
            Scanner sc=new Scanner(n);
            
            while (sc.hasNext()){
                System.out.println(sc.nextLine());
            }
            
            sc.close();
            
  • 相关阅读:
    有关Backgroundworker
    DataGridView风格设置
    Dev控件GridControl实现CheckBox列和ComBox列
    ALTER PROFILE DEFAULT LIMIT PASS_LIFE_TIME UNLIMITED
    PowerDesigner的Additional Checkes 中使用统配符
    PowerDesigner生成sql脚本时去掉双引号并把字段名设为大写
    PowerDesigner中的域(Domain)的概念及应用
    Oracle Sql中输入特殊字符 转义字符
    机械键盘 按一次字母有时候出来2个
    IE内嵌google chrome frame解决浏览器兼容问题
  • 原文地址:https://www.cnblogs.com/zhaotiancheng/p/6266501.html
Copyright © 2020-2023  润新知