java 文件太长_Java读取资源文件时内容过长与换行的处理

Java读取Properties文件时碰到两问题

1. 资源文件中的key对应的value过长时,书写不方便,需要换行,若直接回车则回车后的内容被忽略

2.资源文件中的key对应的value需要换行显示时,若直接回车,则同样丢掉回车后的部分

针对上述问题找到如下解决办法:

1. 内容过长需要换行时拼接个/斜杠,这样/后的内容后正常显示

2.若内容本身需要换行时则用/n代替回车

import java.io.IOException;

import java.io.InputStream;

import java.util.Properties;

public class PropertiesTest2

{

public static void main(String[] args)

{

Properties properties = new Properties();

try

{

InputStream inputStream = PropertiesTest2.class.getClassLoader().getResourceAsStream("test.properties");

properties.load(inputStream);

inputStream.close(); //关闭流

}

catch (IOException e)

{

e.printStackTrace();

}

String key1 = properties.getProperty("key1");

String key2 = properties.getProperty("key2");

System.out.println(key1);

System.out.println(key2);

}

}

输出结果:

Where did you take the picture? It's so beautiful!

Spring

Hibernate

Ibatis

Velocity

Java

Struts

附:test.properties中的内容

key1=Where did you take the picture? / It's so beautiful!  key2=Spring/nHibernate/nIbatis/nVelocity/nJava/nStruts

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值