Java文件操作小结

前言:此节通过三个例子总结了java文件的操作,有File类和Properties以及RandomAccessFile的基本用法介绍。
原文出处:http://blog.csdn.net/u014158743/article/details/52854387

File

列出指定目录下满足指定条件的文件的清单,包括子目录Demo

// 把temp下的所有的txt文件的绝对路径写入一个文本文件,包括子目录下的txt文件
//1:得到指定目录下的所有的txt文件,存到集合中
//2:把集合中的txt文件的信息写入到文件中
import java.io.*;
import java.util.*;
class  Demo {
    public static void main(String[] args) throws IOException {
        File dir = new File("e:\\java\\temp");

        List<File> list = new ArrayList<File>();

        fileToList(dir,list);

        File file = new File("txtlist.txt");
        if(!file.exists())
            file.createNewFile();

        listToFile(list,file);
    }

    //1:得到指定目录下的所有的txt文件,存到集合中
    public static void fileToList(File dir,List<File> list) {
        File[] files = dir.listFiles();
            for(File file:files) {
                if(file.isDirectory()) {
                    fileToList(file,list);
                } else {
                    if(file.getName().endsWith("txt"))
                        list.add(file);
            }
        }
    }
    //2:把集合中的java文件的信息写入到文件中
    public static void listToFile(List<File> list,File file)throws IOException {
          BufferedWriter bw = new BufferedWriter(new FileWriter(file));
          for(File files:list) {
               bw.write(files.getAbsolutePath());
               bw.newLine();
               bw.flush();
          }
          bw.close();
    }
}

Properties

基础用法Demo

import java.util.*;
import java.io.*;
class Demo {
    public static void main(String[] args) throws IOException {
        /*
           Properties:继承了HashTable,HashTable实现了 Map接口,所以Properties是一个集合类

                      是一个可以和流结合使用的集合类

                      存储的必须是属性类型的键值对,而且键值对都是字符串类型的,所以没有使用泛型
        */

        //fun1();
        //fun2();

        fun3();
    }

    public static void fun3() throws IOException {
        Properties pro = new Properties();

        // 把属性文件中的属性信息加载到内存
        FileReader fr = new FileReader("config.properties");
        //使用读取流对象读取文件,把读取出的键值对存到集合中
        pro.load(fr);

        pro.list(System.out);

        //修改颜色---从内存中的修改
        pro.setProperty("color","red");

        pro.list(System.out);
        //把内存中做的修改保存到文件中
        //把集合中的属性信息写入到文件
        FileWriter fw = new FileWriter("config.properties");
        pro.store(fw,"haha");

        fr.close();
        fw.close();
    }

    public static void fun2() {
        //得到所有的系统属性集
        Properties pro = System.getProperties();

        //sop(pro);

        /*Set<String> keys = pro.stringPropertyNames();
        for(String key:keys) {
            String value = pro.getProperty(key);
            sop(key+"="+value);
        }*/
        //从集合中修改---从内存中修改
        pro.setProperty("user.name","weixin");

        pro.list(System.out);


    }

    //Peroperties的基本使用
    public static void fun1() {
        //创建一个Peroperties集合类对象
        Properties pro = new Properties();

        //存储键值对
        pro.setProperty("name","lisi");//存储键值对
        pro.setProperty("age","23");
        pro.setProperty("sex","nv");

        Set<String> keys = pro.stringPropertyNames();//得到所有键的集合
        Iterator<String> ite = keys.iterator();
        while(ite.hasNext()) {
            String key = ite.next();
            String value = pro.getProperty(key);//根据键获取值
            sop(key+"="+value);
        }
        //在内存中做的修改
        pro.setProperty("name","zhangsan");

        Set<String> jian = pro.stringPropertyNames();
        for(String key:jian) {
            String v = pro.getProperty(key);
            sop(key+"="+v);
        }

    }
    public static void sop(Object obj) {
        System.out.println(obj);
    }
}

文件分割Demo

import java.io.*;
import java.util.*;
class Demo {
    public static void main(String[] args)throws IOException {
        //文件的分割
        File file = new File("ok.jpg");
        fenge(file);
    }

    public static void fenge(File file)throws IOException {
        if(file.isDirectory()) {
            System.out.println("不是文件,不能分割");
            return;
        }
        //把分割出的文件放到同一个目录下
        File dir = new File("fenge");
        if(!dir.exists())
            dir.mkdir();

        FileInputStream fis = new FileInputStream(file);
        FileOutputStream fos = null;

        byte[] arr = new byte[1024*1024*2];//分割出的每个文件的大小是 2m
        int len = 0;
        int num = 0;
        while((len = fis.read(arr))!=-1) {
            fos = new FileOutputStream(new File(dir,(++num)+".fenge")); //每次都是new出一个新文件
            fos.write(arr,0,len);
        }

        Properties pro = new Properties();
        pro.setProperty("fileType",file.getName());
        pro.setProperty("fileNum",String.valueOf(num));

        fos = new FileOutputStream(new File(dir,"readme.txt"));
        pro.store(fos,"xixi");


        fis.close();
        fos.close();

    }
}

RandomAccessFile

基本用法Demo

import java.io.*;
class Demo {
    public static void main(String[] args)throws IOException {
        /*
         RandomAccessFile:
                        只能访问文件
                        不属于IO体系
                        内部既有字节输入流,也有字节输出流
                        内部有一个字节数组,可以通过指针操作该数组,所以可以随机
        */

        //writeData();

        // 执行writeData后random.txt内容为:“刘能___8________赵四___:”
        readData();

    }

    public static void readData()throws IOException {
        RandomAccessFile  random = new RandomAccessFile("random.txt","r");
           byte[] arr = new byte[4];
        int len = random.read(arr);
        int age = random.readInt();

        System.out.println(new String(arr,0,len)+","+age);

        long index = random.getFilePointer();
        System.out.println("index="+index);//8

           random.seek(16);//让指针指向 赵四的起始位置

        len = random.read(arr);
        age = random.readInt();
        System.out.println(new String(arr,0,len)+","+age);
    }

    public static void writeData()throws IOException {
        //在"rw"模式下,文件不存在会自动创建 
        RandomAccessFile  random = new RandomAccessFile("random.txt","rw");

        //默认从文件开头写入
        random.write("刘能".getBytes());
        random.writeInt(56);

        //获取指针指向的位置
        long index = random.getFilePointer();
        System.out.println("index="+index);//8

        //设置指针的位置
        random.seek(16);

        random.write("赵四".getBytes());
        random.writeInt(58);

        index = random.getFilePointer();
        System.out.println("index="+index);//
    }
}

结束,晚安。☻☻☻

猜你喜欢

转载自blog.csdn.net/u014158743/article/details/52854387