栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

FileSystemResource类解读

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

FileSystemResource类解读

FileSystemResource解读 FileSystemResource介绍

spring的一个用于读取文件系统下的资源的一个实现类,使用起来非常简单。

FileSystemResource部分源码

获取文件的输入流

	
	@Override
	public InputStream getInputStream() throws IOException {
		try {
			return Files.newInputStream(this.filePath);
		}
		catch (NoSuchFileException ex) {
			throw new FileNotFoundException(ex.getMessage());
		}
	}

判断资源是否可写(如果是目录那就肯定不可写啦)

	@Override
	public boolean isWritable() {
		return (this.file != null ? this.file.canWrite() && !this.file.isDirectory() :
				Files.isWritable(this.filePath) && !Files.isDirectory(this.filePath));
	}
FileSystemResource使用例子

读取一个文件,写(覆盖)些数据进去

 @Test
    public void testFileSystemResourc() throws IOException {
        String path = "F://test.txt";
        FileSystemResource fileSystemResource = new FileSystemResource(path);

        System.out.println(fileSystemResource.isFile());
        System.out.println("长度:"+fileSystemResource.contentLength());
        System.out.println("文件名"+fileSystemResource.getFilename());

        //FileSystemResource实现了WritableResource接口
        OutputStream outputStream = fileSystemResource.getOutputStream();
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
        outputStreamWriter.write("123456789");
        outputStreamWriter.flush();
        outputStreamWriter.close();

    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/995604.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号