java 读取web-inf_java读取WEB-INF下properties配置文件

这段代码展示了如何使用Java读取WEB-INF目录下的jdbc.properties配置文件,并处理了读取过程中可能出现的中文乱码问题。此外,还提供了读取和写入XML、TXT格式配置文件的方法。
摘要由CSDN通过智能技术生成

Properties p = new Properties();

try{

String path = this.class.getClass().getResource("/").getPath();//得到工程名WEB-INF/classes/路径

path=path.substring(1, path.indexOf("classes"));//从路径字符串中取出工程路径

p.load(new FileInputStream(path+"jdbc.properties"));

}catch(Exception e){

e.printStackTrace();

}

01.package apistudy;

02.

03.import java.io.File;

04.import java.io.FileInputStream;

05.import java.io.FileOutputStream;

06.import java.io.IOException;

07.import java.io.InputStream;

08.import java.io.OutputStream;

09.import java.io.UnsupportedEncodingException;

10.import java.util.Properties;

11.

12.public class PropertiesTest

13.{

14.

15.    public static void main(String[] args)

16.    {

17.

18.        String readfile = "d:" + File.separator + "readfile.properties";

19.        String writefile = "d:" + File.separator + "writefile.properties";

20.        String readxmlfile = "d:" + File.separator + "readxmlfile.xml";

21.        String writexmlfile = "d:" + File.separator + "writexmlfile.xml";

22.        String readtxtfile = "d:" + File.separator + "readtxtfile.txt";

23.        String writetxtfile = "d:" + File.separator + "writetxtfile.txt";

24.

25.        readPropertiesFile(readfile); //读取properties文件

26.        writePropertiesFile(writefile); //写properties文件

27.        readPropertiesFileFromXML(readxmlfile); //读取XML文件

28.        writePropertiesFileToXML(writexmlfile); //写XML文件

29.        readPropertiesFile(readtxtfile); //读取txt文件

30.        writePropertiesFile(writetxtfile); //写txt文件

31.    }

32.

33.    //读取资源文件,并处理中文乱码

34.    public static void readPropertiesFile(String filename)

35.    {

36.        Properties properties = new Properties();

37.        try

38.        {

39.            InputStream inputStream = new FileInputStream(filename);

40.            properties.load(inputStream);

41.            inputStream.close(); //关闭流

42.        }

43.        catch (IOException e)

44.        {

45.            e.printStackTrace();

46.        }

47.        String username = properties.getProperty("username");

48.        String passsword = properties.getProperty("password");

49.        String chinese = properties.getProperty("chinese");

50.        try

51.        {

52.            chinese = new String(chinese.getBytes("ISO-8859-1"), "GBK"); // 处理中文乱码

53.        }

54.        catch (UnsupportedEncodingException e)

55.        {

56.            e.printStackTrace();

57.        }

58.        System.out.println(username);

59.        System.out.println(passsword);

60.        System.out.println(chinese);

61.    }

62.

63.    //读取XML文件,并处理中文乱码

64.    public static void readPropertiesFileFromXML(String filename)

65.    {

66.        Properties properties = new Properties();

67.        try

68.        {

69.            InputStream inputStream = new FileInputStream(filename);

70.            properties.loadFromXML(inputStream);

71.            inputStream.close();

72.        }

73.        catch (IOException e)

74.        {

75.            e.printStackTrace();

76.        }

77.        String username = properties.getProperty("username");

78.        String passsword = properties.getProperty("password");

79.        String chinese = properties.getProperty("chinese"); //XML中的中文不用处理乱码,正常显示

80.        System.out.println(username);

81.        System.out.println(passsword);

82.        System.out.println(chinese);

83.    }

84.

85.    //写资源文件,含中文

86.    public static void writePropertiesFile(String filename)

87.    {

88.        Properties properties = new Properties();

89.        try

90.        {

91.            OutputStream outputStream = new FileOutputStream(filename);

92.            properties.setProperty("username", "myname");

93.            properties.setProperty("password", "mypassword");

94.            properties.setProperty("chinese", "中文");

95.            properties.store(outputStream, "author: shixing_11@sina.com");

96.            outputStream.close();

97.        }

98.        catch (IOException e)

99.        {

100.            e.printStackTrace();

101.        }

102.    }

103.

104.    //写资源文件到XML文件,含中文

105.    public static void writePropertiesFileToXML(String filename)

106.    {

107.        Properties properties = new Properties();

108.        try

109.        {

110.            OutputStream outputStream = new FileOutputStream(filename);

111.            properties.setProperty("username", "myname");

112.            properties.setProperty("password", "mypassword");

113.            properties.setProperty("chinese", "中文");

114.            properties.storeToXML(outputStream, "author: shixing_11@sina.com");

115.            outputStream.close();

116.        }

117.        catch (IOException e)

118.        {

119.            e.printStackTrace();

120.        }

121.    }

122.

123.}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值