1、文件内容如下
[
{
"name": "api-plat-auth",
"location": "/api-plat-auth/v2/api-docs",
"version": "2.0"
},
{
"name": "api-plat-datacenter",
"location": "/api-plat-datacenter/v2/api-docs",
"version": "2.0"
},
{
"name": "api-plat-gateway",
"location": "/api-plat-gateway/v2/api-docs",
"version": "2.0"
}
]
2、读取类如下:
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class SyloTestServiceController{
public static void main(String[] args) {
Resource resource = new ClassPathResource("templates/swagger.json");
try {
File sourceFile = resource.getFile();
String document = jsonRead(sourceFile);
System.out.println("document>>>>>>>>>"+document);
String myUrl=sourceFile.getPath();
System.out.println("myUrl>>>>>>>>>"+myUrl);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static String jsonRead(File file) {
Scanner scanner = null;
StringBuilder buffer = new StringBuilder();
try {
scanner = new Scanner(file, "utf-8");
while (scanner.hasNextLine()) {
buffer.append(scanner.nextLine());
}
} catch (Exception e) {
} finally {
if (scanner != null) {
scanner.close();
}
}
return buffer.toString();
}
}