Java读写配置文件——Properties类的简要使用笔记

参考文章:Java读写配置文件——Properties类的简要使用笔记

一、简介

Java Properties功能大致如下:

  1. 读写Properties文件
  2. 读写XML文件
  3. 不仅可以读写上述两类文件,还可以读写其它格式文件如txt等,只要符合key=value格式即可.

注意:资源文件中含有中文时的处理方法 

  1. 将中文字符通过工作转成utf8编码,可以通过Java自带的nativetoascii或Eclipse中的属性编辑器。
  2. 直接调用 new String(youChineseString.getBytes("ISO-8859-1"), "GBK");

附:WEB程序中加载资源文件的方法

Properties prop = null; 
1. prop = Thread.currentThread().getContextClassLoader().getResourceAsStream("filename");
2. prop = this.getClass().getClassLoader().getResourceAsStream("filename");

Properties类继承自Hashtable,大致API如下:

Demo目录结构:

二、读取配置文件

1.读取配置文件类:PropertiesReader.java

关于Properties读取文件这里提供六种方法:《JAVA读取Properties的六种方法》,下面取最常用的一种:

关于路径的写法:(可以相对路径也可以是绝对路径)Class.getResourceAsStream(String path) 
path 不以’/'开头时默认是从此类所在的包下取资源,以’/'开头则是从ClassPath(src文件)根下获取。

package com.lcw.properties.test;

import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;

/**
 * properties文件读取类
 */
public class PropertiesReader {
    public void getPropertiesReader(){
        Properties properties=new Properties();//获取Properties实例
        InputStream inStream=getClass().getResourceAsStream("config.properties");//获取配置文件输入流
        try {
            properties.load(inStream);//载入输入流
            Enumeration enumeration=properties.propertyNames();//取得配置文件里所有的key值
            while(enumeration.hasMoreElements()){
                String key=(String) enumeration.nextElement();
                System.out.println("配置文件里的key值:"+key+"=====>配置文件里的value值:"+properties.getProperty(key));//输出key值
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

2.测试类:PropertiesTest.java

package com.lcw.properties.test;

public class PropertiesTest {
    /**
     * 测试类
     */
    public static void main(String[] args) {
        PropertiesReader propertiesReader=new PropertiesReader();
        propertiesReader.getPropertiesReader();
    }
}

3.配置文件信息:config.properties

color=black
animal=rabbit
food=hamburger
chinese=\u6211\u662F\u4E2D\u6587

4.运行效果:

三、写入配置文件

1. writeProperties方法

    // 写入资源文件信息
    public void writeProperties(){
        Properties properties=new Properties();
        try {
            OutputStream outputStream=new FileOutputStream("config.properties");
            properties.setProperty("number", "2015");
            properties.setProperty("song", "手写的从前");
            properties.store(outputStream, "rabbit");
            outputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

2.生成文件:

#rabbit
#Wed Jan 07 17:16:56 CST 2015
number=2015
song=\u6211\u7231\u4F60
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
HDFS是Hadoop分布式文件系统,它提供了Java API来进行文件读写操作。在HDFS中,文件被分成多个块并存储在不同的节点上,因此需要使用分布式文件系统的API来进行文件读写操作。 HDFS Java API提供了以下几个来进行文件读写操作: 1. FileSystem:表示一个文件系统对象,可以通过它来获取文件系统的配置信息、创建文件、删除文件等操作。 2. Path:表示一个文件或目录的路径。 3. FSDataInputStream:表示一个输入流,可以用来读取HDFS中的文件。 4. FSDataOutputStream:表示一个输出流,可以用来向HDFS中写入数据。 下面是一个简单的示例代码,演示如何使用HDFS Java API进行文件读写操作: ```java import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.apache.hadoop.fs.FSDataInputStream; import org.apache.hadoop.fs.FSDataOutputStream; public class HdfsExample { public static void main(String[] args) throws Exception { // 创建一个Configuration对象,用于获取Hadoop配置信息 Configuration conf = new Configuration(); // 获取HDFS文件系统对象 FileSystem fs = FileSystem.get(conf); // 创建一个Path对象,表示要读取的文件路径 Path inputPath = new Path("/input/test.txt"); // 创建一个FSDataInputStream对象,用于读取文件 FSDataInputStream in = fs.open(inputPath); // 读取文件内容 byte[] buffer = new byte[1024]; int len = in.read(buffer); while (len > 0) { System.out.write(buffer, 0, len); len = in.read(buffer); } // 关闭输入流 in.close(); // 创建一个Path对象,表示要写入的文件路径 Path outputPath = new Path("/output/test.txt"); // 创建一个FSDataOutputStream对象,用于写入文件 FSDataOutputStream out = fs.create(outputPath); // 写入文件内容 String content = "Hello, HDFS!"; out.write(content.getBytes()); // 关闭输出流 out.close(); } } ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值