0
点赞
收藏
分享

微信扫一扫

spring boot 读取项目根目录文件,写入到jar 包发布启动地址的同级目录下,如果没有文件生成文件

唯米天空 2022-08-02 阅读 23


介绍:

获取系统根目录

File path = null;
try {
path = new File(ResourceUtils.getURL("classpath:").getPath());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (!path.exists())
path = new File("");
System.out.println("系统根节点地址::" + path.getAbsolutePath());

// 读取 application.json 是和 application.yum 同级目录文件
List list = readFileToList(path.getAbsolutePath() + "\\application.json");

 

获取jar 启动的地址


ApplicationHome home = new ApplicationHome(getClass());
File jarFile = home.getSource();
String jarUrl = jarFile.getParentFile().toString() + "/app.conf";
System.out.println("jar 启动地址 :" + jarUrl);
// 写入
writeListToFile(list, jarUrl);

 

然后读取和写入就看下边把

分行读取到集合

// 读取文件内容
public List readFileToList(String url) {
File file = new File(url);
List<String> listStr = new ArrayList<String>();
BufferedReader br = null;
String str = null;
try {
br = new BufferedReader(new FileReader(file));
while ((str = br.readLine()) != null) {
listStr.add(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return listStr;
}

 

分行写入到文件

/**
* 写入文件内容出创建文件
*
* @param listStr 数据
* @param url 地址
*/
private void writeListToFile(List<String> listStr, String url) {
File file = new File(url);// 要写入的文件路径
if (!file.exists()) {// 判断文件是否存在
try {
file.createNewFile();// 如果文件不存在创建文件
} catch (IOException e) {
e.printStackTrace();
}
} else {

}
for (String str : listStr) {// 遍历listStr集合
FileOutputStream fos = null;
PrintStream ps = null;
try {
fos = new FileOutputStream(file, true);// 文件输出流 追加
ps = new PrintStream(fos);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
String string = str + "\r\n";// +换行
ps.print(string); // 执行写操作
ps.close(); // 关闭流
}
System.out.println("文件写入完毕!");
}

 

上代码实例

boot 启动代码

package com.supermap;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import com.supermap.testCtr.InitInif;

/**
* 项目启动类
*
* @author yushen
*
*/
@SpringBootApplication
public class Provider_App {

/**
* 项目启动入口
*
* @param args
*/
public static void main(String[] args) {

SpringApplication.run(Provider_App.class, args);
InitInif.init();


}

}

实现类代码

package com.supermap.testCtr;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.springframework.boot.ApplicationHome;
import org.springframework.util.ResourceUtils;

/**
* 初始化一些信息
*
* @author yushen
*
*/
public class InitInfo {

public static void init() {
new InitInfo ().addFileConf();
}


// 启动spring boot 给jar 启动同目录 添加一个配置文件
public void addFileConf() {
File path = null;
try {
path = new File(ResourceUtils.getURL("classpath:").getPath());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (!path.exists())
path = new File("");
System.out.println("系统根节点地址::" + path.getAbsolutePath());

// 读取 application.json 是和 application.yum 同级目录文件
List list = readFileToList(path.getAbsolutePath() + "\\application.json");

ApplicationHome home = new ApplicationHome(getClass());
File jarFile = home.getSource();
String jarUrl = jarFile.getParentFile().toString() + "/app.conf";
System.out.println("jar 启动地址 :" + jarUrl);
// 写入
writeListToFile(list, jarUrl);
}

// 读取文件内容
public List readFileToList(String url) {
File file = new File(url);
List<String> listStr = new ArrayList<String>();
BufferedReader br = null;
String str = null;
try {
br = new BufferedReader(new FileReader(file));
while ((str = br.readLine()) != null) {
listStr.add(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return listStr;
}

/**
* 写入文件内容出创建文件
*
* @param listStr 数据
* @param url 地址
*/
private void writeListToFile(List<String> listStr, String url) {
File file = new File(url);// 要写入的文件路径
if (!file.exists()) {// 判断文件是否存在
try {
file.createNewFile();// 如果文件不存在创建文件
} catch (IOException e) {
e.printStackTrace();
}
} else {

}
for (String str : listStr) {// 遍历listStr集合
FileOutputStream fos = null;
PrintStream ps = null;
try {
fos = new FileOutputStream(file, true);// 文件输出流 追加
ps = new PrintStream(fos);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
String string = str + "\r\n";// +换行
ps.print(string); // 执行写操作
ps.close(); // 关闭流
}
System.out.println("文件写入完毕!");
}
}

 

 

 

 

 

文章持续更新

 

 

 

 

 

 

举报

相关推荐

0 条评论