Java 如何从文件到JSP页面获取特定值?
我有一个包含版本号的文件。比如说Java 如何从文件到JSP页面获取特定值?,java,jsp,Java,Jsp,我有一个包含版本号的文件。比如说 foo.cf something somevalue somevalue ........ ....... version = "7.1" ........ ......... File file = new File("foo.cf"); InputStream inputStream = null; HashMap<String, String> defPropMap =
foo.cf
something
somevalue
somevalue
........
.......
version = "7.1"
........
.........
File file = new File("foo.cf");
InputStream inputStream = null;
HashMap<String, String> defPropMap = new HashMap<String, String>();
try {
inputStream = new FileInputStream(file);
Scanner scanner = new Scanner(inputStream);
while (scanner.hasNextLine()) {
String[] columns = scanner.nextLine().split("=");
if(columns.length == 2)
defPropMap.put(columns[0].trim(), columns[1].trim());
else
defPropMap.put(columns[0].trim(), null);
}
} catch (FileNotFoundException e) {
logger.warn("Caught FileNotFoundException", e);
}
finally{
try {
inputStream.close();
} catch (IOException e) {
logger.warn("Caught IOException", e);
}
}
我想将文件foo.cf中的值7.1(可以是任何值)输入我的JSP页面。
有人能帮我怎么做吗
我知道如何读取文件,但不知道如何获取特定值。这是我从JSP读取文件的方式。如果我的代码错了,请纠正我
BufferedReader reader = new BufferedReader(new FileReader("foo.cf"));
StringBuilder sb = new StringBuilder();
String line;
while((line = reader.readLine())!= null){
}
File file = new File("foo.cf");
InputStream inputStream = null;
HashMap<String, String> defPropMap = new HashMap<String, String>();
try {
inputStream = new FileInputStream(file);
Scanner scanner = new Scanner(inputStream);
while (scanner.hasNextLine()) {
String[] columns = scanner.nextLine().split("=");
if(columns.length == 2)
defPropMap.put(columns[0].trim(), columns[1].trim());
else
defPropMap.put(columns[0].trim(), null);
}
} catch (FileNotFoundException e) {
logger.warn("Caught FileNotFoundException", e);
}
finally{
try {
inputStream.close();
} catch (IOException e) {
logger.warn("Caught IOException", e);
}
}
可能您可以尝试以下方法(未测试):
File file = new File("foo.cf");
InputStream inputStream = null;
HashMap<String, String> defPropMap = new HashMap<String, String>();
try {
inputStream = new FileInputStream(file);
Scanner scanner = new Scanner(inputStream);
while (scanner.hasNextLine()) {
String[] columns = scanner.nextLine().split("=");
if(columns.length == 2)
defPropMap.put(columns[0].trim(), columns[1].trim());
else
defPropMap.put(columns[0].trim(), null);
}
} catch (FileNotFoundException e) {
logger.warn("Caught FileNotFoundException", e);
}
finally{
try {
inputStream.close();
} catch (IOException e) {
logger.warn("Caught IOException", e);
}
}
如果文件的格式如下:
name=foo.cf
xyz=abc
xyz1=abc1
version=7.1
File file = new File("foo.cf");
InputStream inputStream = null;
HashMap<String, String> defPropMap = new HashMap<String, String>();
try {
inputStream = new FileInputStream(file);
Scanner scanner = new Scanner(inputStream);
while (scanner.hasNextLine()) {
String[] columns = scanner.nextLine().split("=");
if(columns.length == 2)
defPropMap.put(columns[0].trim(), columns[1].trim());
else
defPropMap.put(columns[0].trim(), null);
}
} catch (FileNotFoundException e) {
logger.warn("Caught FileNotFoundException", e);
}
finally{
try {
inputStream.close();
} catch (IOException e) {
logger.warn("Caught IOException", e);
}
}
我的意思是关键=价值这类内容。下面的代码是一种复杂的检索版本的方法
File file = new File("foo.cf");
InputStream inputStream = null;
HashMap<String, String> defPropMap = new HashMap<String, String>();
try {
inputStream = new FileInputStream(file);
Scanner scanner = new Scanner(inputStream);
while (scanner.hasNextLine()) {
String[] columns = scanner.nextLine().split("=");
if(columns.length == 2)
defPropMap.put(columns[0].trim(), columns[1].trim());
else
defPropMap.put(columns[0].trim(), null);
}
} catch (FileNotFoundException e) {
logger.warn("Caught FileNotFoundException", e);
}
finally{
try {
inputStream.close();
} catch (IOException e) {
logger.warn("Caught IOException", e);
}
}
File File=new文件(“foo.cf”);
InputStream InputStream=null;
HashMap defPropMap=新HashMap();
试一试{
inputStream=新文件inputStream(文件);
扫描仪=新扫描仪(inputStream);
while(scanner.hasNextLine()){
String[]columns=scanner.nextLine().split(“”);
如果(columns.length==2)
defPropMap.put(列[0].trim(),列[1].trim());
其他的
defPropMap.put(列[0].trim(),null);
}
}catch(filenotfounde异常){
logger.warn(“捕获的文件NotFoundException”,e);
}
最后{
试一试{
inputStream.close();
}捕获(IOE异常){
logger.warn(“捕获IOException”,e);
}
}
其他方法是逐行读取。要实现这一点,可以使用正则表达式
File file = new File("foo.cf");
InputStream inputStream = null;
HashMap<String, String> defPropMap = new HashMap<String, String>();
try {
inputStream = new FileInputStream(file);
Scanner scanner = new Scanner(inputStream);
while (scanner.hasNextLine()) {
String[] columns = scanner.nextLine().split("=");
if(columns.length == 2)
defPropMap.put(columns[0].trim(), columns[1].trim());
else
defPropMap.put(columns[0].trim(), null);
}
} catch (FileNotFoundException e) {
logger.warn("Caught FileNotFoundException", e);
}
finally{
try {
inputStream.close();
} catch (IOException e) {
logger.warn("Caught IOException", e);
}
}
这是一个例子。您需要根据自己的要求修改此项
File file = new File("foo.cf");
InputStream inputStream = null;
HashMap<String, String> defPropMap = new HashMap<String, String>();
try {
inputStream = new FileInputStream(file);
Scanner scanner = new Scanner(inputStream);
while (scanner.hasNextLine()) {
String[] columns = scanner.nextLine().split("=");
if(columns.length == 2)
defPropMap.put(columns[0].trim(), columns[1].trim());
else
defPropMap.put(columns[0].trim(), null);
}
} catch (FileNotFoundException e) {
logger.warn("Caught FileNotFoundException", e);
}
finally{
try {
inputStream.close();
} catch (IOException e) {
logger.warn("Caught IOException", e);
}
}
package snippet;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) {
String test = "foo.cf something somevalue somevalue ........ ....... version = \" 7.1 \" ........ .........";
String exp = ".*version\\s*=\\s*\"([^\"]+)";
System.out.println(runSubRegex(exp, test));
}
private static String runSubRegex(String regex, String tag) {
Pattern p = Pattern.compile(regex);
Matcher matcher = p.matcher(tag);
if (matcher.find()) {
return matcher.group(1);
}
return null;
}
}
在本例中,这将打印
version=”“
与7.1之间的任何值,而不考虑空格。在Jsp中访问文件不是一种好做法
File file = new File("foo.cf");
InputStream inputStream = null;
HashMap<String, String> defPropMap = new HashMap<String, String>();
try {
inputStream = new FileInputStream(file);
Scanner scanner = new Scanner(inputStream);
while (scanner.hasNextLine()) {
String[] columns = scanner.nextLine().split("=");
if(columns.length == 2)
defPropMap.put(columns[0].trim(), columns[1].trim());
else
defPropMap.put(columns[0].trim(), null);
}
} catch (FileNotFoundException e) {
logger.warn("Caught FileNotFoundException", e);
}
finally{
try {
inputStream.close();
} catch (IOException e) {
logger.warn("Caught IOException", e);
}
}
请在Servlet的doPost()或doGet()方法中执行此操作
File file = new File("foo.cf");
InputStream inputStream = null;
HashMap<String, String> defPropMap = new HashMap<String, String>();
try {
inputStream = new FileInputStream(file);
Scanner scanner = new Scanner(inputStream);
while (scanner.hasNextLine()) {
String[] columns = scanner.nextLine().split("=");
if(columns.length == 2)
defPropMap.put(columns[0].trim(), columns[1].trim());
else
defPropMap.put(columns[0].trim(), null);
}
} catch (FileNotFoundException e) {
logger.warn("Caught FileNotFoundException", e);
}
finally{
try {
inputStream.close();
} catch (IOException e) {
logger.warn("Caught IOException", e);
}
}
将其添加到servlet内的请求对象中
File file = new File("foo.cf");
InputStream inputStream = null;
HashMap<String, String> defPropMap = new HashMap<String, String>();
try {
inputStream = new FileInputStream(file);
Scanner scanner = new Scanner(inputStream);
while (scanner.hasNextLine()) {
String[] columns = scanner.nextLine().split("=");
if(columns.length == 2)
defPropMap.put(columns[0].trim(), columns[1].trim());
else
defPropMap.put(columns[0].trim(), null);
}
} catch (FileNotFoundException e) {
logger.warn("Caught FileNotFoundException", e);
}
finally{
try {
inputStream.close();
} catch (IOException e) {
logger.warn("Caught IOException", e);
}
}
request.setAttribute("version",version);
在jsp页面中添加以下内容
File file = new File("foo.cf");
InputStream inputStream = null;
HashMap<String, String> defPropMap = new HashMap<String, String>();
try {
inputStream = new FileInputStream(file);
Scanner scanner = new Scanner(inputStream);
while (scanner.hasNextLine()) {
String[] columns = scanner.nextLine().split("=");
if(columns.length == 2)
defPropMap.put(columns[0].trim(), columns[1].trim());
else
defPropMap.put(columns[0].trim(), null);
}
} catch (FileNotFoundException e) {
logger.warn("Caught FileNotFoundException", e);
}
finally{
try {
inputStream.close();
} catch (IOException e) {
logger.warn("Caught IOException", e);
}
}
<% String version = (String)request.getAttribute("version");%>
你需要使用正则表达式你能举个例子看看我的答案吗使用正则表达式L。如果版本字符串行有多个“=”会失败。代码基于OP提供的示例字符串。此外,版本字符串包含两个“=”的可能性较小。我没有使用servlet。我可以直接从JSp访问它吗