文件资源操作

1.访问文件资源

 

     假设有一个文件地位于 Web 应用的类路径下,您可以通过以下方式对这个文件资源进行访问:

  • 通过 FileSystemResource 以文件系统绝对路径的方式进行访问;
  • 通过 ClassPathResource 以类路径的方式进行访问;
  • 通过 ServletContextResource 以相对于Web应用根目录的方式进行访问。

说明:可以通过 getFileName() 获取文件名,通过 getFile() 获取资源对应的 File 对象,通过 getInputStream() 直接获取文件的输入流。此外,您还可以通过 createRelative(String relativePath) 在资源相对地址上创建新的资源。对于位于远程服务器(Web 服务器或 FTP 服务器)的文件资源,您则可以方便地通过 UrlResource 进行访问。

 

2.ResourceUtils 工具类

 

Spring 提供了一个 ResourceUtils 工具类,它支持“classpath:”和“file:”的地址前缀,它能够从指定的地址加载文件资源,ResourceUtils 的 getFile(String resourceLocation) 方法支持带特殊前缀的资源地址。

 

3.本地化文件资源

 

本地化文件资源是一组通过本地化标识名进行特殊命名的文件,Spring 提供的 LocalizedResourceHelper 允许通过文件资源基名和本地化实体获取匹配的本地化文件资源并以 Resource 对象返回。假设在类路径的 i18n 目录下,拥有一组基名为 message 的本地化文件资源,

LocalizedResourceHelper lrHalper = new LocalizedResourceHelper();
    // ① 获取对应美国的本地化文件资源
    Resource msg_us = lrHalper.findLocalizedResource("i18n/message", ".properties", Locale.US);
    // ② 获取对应中国大陆的本地化文件资源
    Resource msg_cn = lrHalper.findLocalizedResource("i18n/message", ".properties", Locale.CHINA);

 

4.FileCopyUtils

 

例子:Resource res = new ClassPathResource("conf/file1.txt");
        // ① 将文件内容拷贝到一个 byte[] 中
        byte[] fileData = FileCopyUtils.copyToByteArray(res.getFile());
        // ② 将文件内容拷贝到一个 String 中
        String fileStr = FileCopyUtils.copyToString(new FileReader(res.getFile()));
        // ③ 将文件内容拷贝到另一个目标文件
        FileCopyUtils.copy(res.getFile(),
        new File(res.getFile().getParent()+ "/file2.txt"));

        // ④ 将文件内容拷贝到一个输出流中
        OutputStream os = new ByteArrayOutputStream();
        FileCopyUtils.copy(res.getInputStream(), os);

 

5.属性文件操作

 

通过 java.util.Properties的load(InputStream inStream) 方法从一个输入流中加载属性资源。Spring 提供的 PropertiesLoaderUtils 允许您直接通过基于类路径的文件地址加载属性资源。

 

// ① jdbc.properties 是位于类路径下的文件
Properties props = PropertiesLoaderUtils.loadAllProperties("jdbc.properties");

static Properties loadProperties(Resource resource) -->从 Resource 中加载属性

 

static void fillProperties(Properties props, Resource resource)-->将 Resource 中的属性数据添加到一个已经存在的 Properties 对象中。

 

6.EncodedResource

 

当您使用 Resource 实现类加载文件资源时,它默认采用操作系统的编码格式。如果文件资源采用了特殊的编码格式(如 UTF-8),则在读取资源内容时必须事先通过 EncodedResource 指定编码格式,否则将会产生中文乱码的问题。

 

Resource res = new ClassPathResource("conf/file1.txt");
// ① 指定文件资源对应的编码格式(UTF-8)
EncodedResource encRes = new EncodedResource(res,"UTF-8");
String content  = FileCopyUtils.copyToString(encRes.getReader());

 

7.使用HTTPClient读取远程文件

 

	public static void read(String url, String diskPath) {

		HttpClient httpClient = new DefaultHttpClient();
		HttpGet httpGet = new HttpGet(url);
		HttpResponse response = null;

		try {
			response = httpClient.execute(httpGet);

			HttpEntity entity = response.getEntity();
			if (entity != null) {
				byte[] bytes = EntityUtils.toByteArray(entity);
				FileOutputStream out = new FileOutputStream(new File(diskPath));
				out.write(bytes);
				out.close();
			}

		} catch (Exception e) {
			System.err.println(e.getStackTrace());
		}finally{
			httpClient.getConnectionManager().shutdown();
		}

	}

 8、转码工具native2ascii.exe

      命令行格式:
      native2ascii.exe -[options] [inputfile [outputfile]]

      其中:
  -[options]表示命令开关,有两个选项可供选择:
  -reverse:用Latin-1或Unicode编码把文件转换成本地编码格式
  -encoding encoding_name:要把文件转换的目标编码
  inputfile:表示输入文件全名。
  outputfile:输出文件名。

     

      示例:

      D:\jdk1.5.0_06\bin>native2ascii.exe -encoding utf8 abc.txt bcd.txt 
      D:\jdk1.5.0_06\bin>native2ascii.exe -encoding GBK abc.txt bcd.txt

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值