• java 读取resource下的文件


    一、普通main代码里使用

    1.假设有如下结构的代码

    在这里插入图片描述

    (1)、main方法里复制resource下的文件

    import org.apache.commons.io.FileUtils;
    import java.io.*;
    
    public class App {
        public static void main(String[] args) throws Exception {
    	        //从resources下读取diff2html.min.css为输入流
                InputStream inputStream = App.class.getClassLoader().getResourceAsStream("diff2html.min.css");
                //利用Apache Commons IO库把输入流写入到D:\myExcel\jsAndCss\diff2html.min.css ,如果文件不存在会自动创建
                FileUtils.copyInputStreamToFile(inputStream, new File("D:\\myExcel\\jsAndCss\\diff2html.min.css"));
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    可以把代码和resource目录打成一个jar包也同样适用。运行jar包的时候就会把jar包里resource下的diff2html.min.css文件复制到D:\myExcel\jsAndCss\diff2html.min.css

    (2)、main方法里读取resource下的文件

    import java.io.*;
    import java.nio.charset.StandardCharsets;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Map;
    
    public class App {
        public static void main(String[] args) throws Exception {
    	    List<String> list = new ArrayList<>();
    		//从resources下读取diff2html.min.css为输入流
            InputStream inputStream = App.class.getClassLoader().getResourceAsStream("diff2html.min.css");
            BufferedInputStream buf = new BufferedInputStream(inputStream); 
            int len = 0;
            byte[] bys = new byte[1024];
            while ((len = buf.read(bys)) != -1) {
                //diff2html.min.css里每一行的内容添加到list里
    			String row = new String(bys, 0, len, StandardCharsets.UTF_8);
    			System.out.println(row);
                list.add(row);
            }
            inputStream.close(); 
            buf.close();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    2.假设有如下结构的代码

    在这里插入图片描述

    import com.haitangxt.utils.ReadSource;
    
    public class App {
        public static void main(String[] args) throws Exception {
                ReadSource readSource = new ReadSource();
            	readSource.copyfile("D:\\myExcel\\jsAndCss");
          }
     }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    import org.apache.commons.io.FileUtils;
    
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStream;
    
    public class ReadSource {
        public  void copyfile(String jsCssPath) {
            try {
            	//从resources下的test/diff2html.min.css读取为输入流
                InputStream inputStream = ReadSource.class.getClassLoader().getResourceAsStream("test/diff2html.min.css");
                //利用Apache Commons IO库把输入流写入到D:\myExcel\jsAndCss\diff2html.min.css ,如果文件不存在会自动创建
                FileUtils.copyInputStreamToFile(inputStream, new File(jsCssPath + "\\diff2html.min.css"));
            } catch (IOException e) {
            	e.printStackTrace();
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    二、对于springboot项目读取resource下的资源文件

    在这里插入图片描述

    
    @Component
    public class WorldAddressUtils {
    
     public JSONObject getAllCountrys() {
            try (
                    //读取文件为字节流
                    InputStream file = this.getClass().getResourceAsStream("/resourcesfile/worldAddress.txt");
                    //字节流转化为字符流,以UTF-8读取防止中文乱码
                    InputStreamReader in = new InputStreamReader(file, "UTF-8");
                    //加入到缓存
                    BufferedReader buf = new BufferedReader(in);
            ) {
                String str = "";
                while ((str = buf.readLine()) != null) {
    				System.out.println(str);
    			}
    		}catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
  • 相关阅读:
    YoloV7改进策略:独家原创,全网首发,复现Drone-Yolo,以及改进方法
    正则表达式使用文档
    设计模式:模板模式和策略模式混合使用
    tiup uninstall
    【前缀“选区-” bat脚本】
    C++ 异形窗口
    Vue与React更应该学哪一个
    整理C++继承、多继承、菱形继承、虚继承
    小程序首页如何进行装修设置
    linux安装mysql 8.0.20
  • 原文地址:https://blog.csdn.net/qq_33697094/article/details/127899112