欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java 读取本jar包内的资源文件

程序员文章站 2022-03-02 17:09:31
...
package com.test.io;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStreamReader;

/**
* 需要在d盘建立临时文件。
* D:\\test\\test2.txt
*
* @author DZIT
*
*/
public class IoTest {

/**
* @param args
*/
public static void main(String[] args) {
// 字符读取写入
BufferedReader in1 = null;
BufferedReader in2 = null;
BufferedWriter ow1 = null;
BufferedWriter ow2 = null;

// 字节读取写入
BufferedInputStream is1 = null;
BufferedOutputStream os1 = null;
try {
// 从class同级文件读取字符数据
in1 = new BufferedReader(new InputStreamReader(IoTest.class
.getClassLoader().getResourceAsStream(
"com/test/io/test.txt"), "utf-8"));
// 写入到文件系统
ow1 = new BufferedWriter(new FileWriter(new File(
"D:\\test\\out1.txt")));

// 从文件系统读取数据
in2 = new BufferedReader(new FileReader("D:\\test\\test2.txt"));
// 写入到文件系统
ow2 = new BufferedWriter(new FileWriter(new File(
"D:\\test\\out2.txt")));

String str = null;
while ((str = in1.readLine()) != null) {
System.out.println(str);
ow1.write(str);
// 换行
ow1.newLine();
// 刷新,否侧可能不出数据
ow1.flush();
}

str = null;
while ((str = in2.readLine()) != null) {
System.out.println(str);
ow2.write(str);
ow2.newLine();
ow2.flush();
}

// 从class同级文件读取字节数据。同理可参照上边从文件系统读取数据。
is1 = new BufferedInputStream(IoTest.class.getClassLoader()
.getResourceAsStream("com/test/io/asd.jpg"));

os1 = new BufferedOutputStream(new FileOutputStream(new File(
"D:\\test\\outImage.jpg")));
byte[] b = new byte[1024];
while (is1.read(b) != -1) {
os1.write(b);
}
os1.flush();

in1.close();
in2.close();
ow1.close();
ow2.close();
is1.close();
os1.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}



//for resource test in jar
try{
[color=red]//读取资源文件,但是打成jar包后会出错。[/color]
ResourceBundleTest test = new ResourceBundleTest();
URL url = test.getClass().getResource("/config/REG_IP1.xsd");
System.out.println("url is ====:" + url.toURI());
FileReader fdata = new FileReader(new File(url.toURI()));

[color=red]//读取资源文件,打成jar包后会也没有问题。[/color]
InputStream in=test.getClass().getResourceAsStream("/config/REG_IP1.xsd");
Reader inreader=new InputStreamReader(in);


//[color=red]读取项目根目录下的文件[/color]
Properties props = new Properties();
props.load(new FileInputStream("db.properties"));
System.out.println("读取项目根目录下的文件");

//[color=red]读取bin目录下的文件。如下示例为 文件放在 src/conf 下的test。property文件。编译后会在bin/conf下[/color]
ResourceBundle bund = ResourceBundle.getBundle("conf.test", Locale.getDefault());
System.out.println("读取bin目录下的文件");

//[color=red]yy.properties应放在/WEB-INF/classes目录[/color]
//ResourceBundle res = ResourceBundle.getBundle("yy.properties");


//[color=red]读取跟当前class相同目录下的文件[/color]
Properties props2=new Properties();
props2.load(ResourceBundleTest.class.getResourceAsStream("ResourceBundleTest.properties"));


}catch(Exception e){
System.out.println("E getResource !!");
}
}

上一篇: CSS常用效果

下一篇: css常用效果