• java创建本地文件并读取文件


    java创建本地文件并读取文件

    java功能强大,可以直接或间接与操作系统打“交道”。例如在本地创建文件,并读取文件,其实与本地写log日志是一样的道理。

    接下来就是使用java来实现创建本地文件,并读取本地文件,废话就不多说了,直接上代码。

    package localfile;
    
    import java.io.*;
    import java.util.Properties;
    
    public class MichaelMakeNativeFile {
        private static Properties base;
        private static File pFile = null;
        private static long iniModifyTime = 0;
        private static String currentUrl;
    
        //static生命周期与类实例化一致
        static {
            currentUrl = System.getProperty( "user.dir" ) + "\\";
            System.out.println("当前项目的完整路径(只到项目文件夹名,不包括包名):"+currentUrl);
            //1)创建文件
            pFile = new File( currentUrl + "config\\michael.txt" );
            iniModifyTime = pFile.lastModified();
            //操作键值对
            base = new Properties();
            //++++++++++++++++++++++++++++++++++++文件夹和文件++++++++++++++++++++++++++++++++++++
            try {
                //如果不存在该文件,则创建新的文件,并写入新的默认值
                if(!pFile.exists()){
                    createFile("config","michael.txt");
                    //2)写入文件
                    fileWrite("name","michael.pan");
                    fileWrite("age","20");
                }
                //+++++++++++++++++++++++++++++++++++++++++
                //Properties需要加载FileReader才能读取File文件的值!!!
                FileReader reader = new FileReader( pFile );
                base.load( reader );
                //+++++++++++++++++++++++++++++++++++++++++
                //3)读取文件
                // readFile(pFile);
            } catch (Exception e) {
                e.printStackTrace();
            }
            //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        }
        //创建文件夹和文件
        private static void createFile(String fileDir,String fileName){
            try {
                //1)生成文件file
                String property = System.getProperty("user.dir");//当前项目路径
                String pathName = property + "\\"+fileDir+"\\"+fileName;
                //**************************************************************************************
                pFile = new File(pathName);
                //记录最后修改的时间
                iniModifyTime = pFile.lastModified();
                pFile.getParentFile().mkdir();//根据java内容创建的File文件,调用mkdir()在本地文件上创建对应的文件夹
                pFile.createNewFile();//根据java内容创建的File文件,调用createNewFile在本地文件上创建对应的文件,如pcq.ini
                //**************************************************************************************
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //使用FileOutputStream的写入本地文件
        private static void writeFile(File file,String writeContent){
            FileOutputStream fileOutputStream = null;
            try {
                //2)写入文件,两种文件输出形式FileOutputStream和FileWriter。
                fileOutputStream = new FileOutputStream(file);
                fileOutputStream.write(writeContent.getBytes());
                //在关闭流之前,将遗留在缓存区的流冲刷出来,避免数据丢失
                fileOutputStream.flush();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    //关闭流
                    assert fileOutputStream != null;
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        //使用FileWriter的写入方式
        private static void fileWrite(String key,String value){
            FileWriter fileWriter = null;
            try {
                fileWriter = new FileWriter(pFile);
                //设置key:value键值对
                base.setProperty(key,value);
                //保存键值对
                base.store(fileWriter,"");
                // fileWriter.write(writeContent);
                fileWriter.flush();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    assert fileWriter != null;
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        //从本地读文件
        private static String readFile(File file){
            StringBuilder stringBuilder = null;
            FileReader fileReader = null;
            try {
                fileReader = new FileReader(file);
                //3)读取本地文件的内容 //read(ch)一次最多读5个字符,返回值最大为5。不足5时,返回对应的字符数
                char[] chars = new char[5];
                //+++++++++++++++++++++++++++读取方式一
                //数据对的读入
                // int data;
                // while ((data = fileReader.read()) != -1){read()返回值为-1时,表示读文件到末尾;其他值都是相应的字符对应的数字
                //     System.out.println((char)data);
                // }
                //***************************方式一结束
                int len;
                stringBuilder = new StringBuilder();
                String str = null;
                //返回值为-1时,表示读文件到末尾
                while ((len = fileReader.read(chars)) != -1){
                    //转换的数据是从ch数组中的[0,len)获得
                    str = new String(chars, 0, len);
                    stringBuilder.append(str);
                }
                System.out.println("FileReader读取的内容:"+stringBuilder.toString());
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    assert fileReader != null;
                    fileReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return stringBuilder.toString();
        }
        //读取properties文件
        private static String readProperties( String key){
            if(iniModifyTime != pFile.lastModified()){
                reloadFile();
                iniModifyTime = pFile.lastModified();
            }
            return base.getProperty(key);
        }
        //重新加载文件,则就需要重新在Properties里面加载FileReader,每次刷新都要重新创建新的对象
        private static void reloadFile(){
            FileReader fileReader = null;
            try {
                fileReader = new FileReader(pFile);
                base = new Properties();
                base.load(fileReader);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    assert fileReader != null;
                    fileReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    
        public static void main(String[] args) throws UnsupportedEncodingException {
            new MichaelMakeNativeFile();
            fileWrite("name2","中文");
            fileWrite("name","michael.pan");
            fileWrite("age","20");
            System.out.println(readProperties( "name"));
            System.out.println(readProperties( "age"));
            System.out.println(readProperties( "sex"));
            System.out.println(readProperties("classroom"));
            System.out.println(readProperties( "name2"));
            System.out.println(readProperties( "score"));
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179

    提示:直接将代码粘贴到开发环境中即可运行使用,希望对您有帮助,谢谢!

  • 相关阅读:
    2023ES SHOW深圳电子元器件及物料采购展览会!|深圳比创达电子EMC
    Linux 软件包工具rpmbuild
    Skywalking9.2.0监控浏览器
    二分查找一个数首次与最后出现的位置
    window 常用基础命令
    Alluxio 2.9新版发布 | 重塑架构,支持大规模多租户环境
    JVM线上监控环境搭建Grafana+Prometheus+Micrometer
    XPD738协议系列-USB Type-C PD 和 Type-A 双口控制器
    Nginx实现tcp代理并支持TLS加密实验
    谷粒商城(一)
  • 原文地址:https://blog.csdn.net/Self_discipline8/article/details/126024977