Java 验证txt文件
我必须根据下面的格式从文本文档中验证和提取代码字符串Java 验证txt文件,java,validation,Java,Validation,我必须根据下面的格式从文本文档中验证和提取代码字符串 Title: title YLabel: YLabel XLabel: XLabel XStart: numeric value XInterval: numeric value 3.5, 5, 7, 9..etc 我需要捕获所有数据,并根据此模式进行验证。我试着使用.split()函数,但是弄糊涂了。 以下是我目前掌握的情况: public boolean validateFile(String info) { // split
Title: title
YLabel: YLabel
XLabel: XLabel
XStart: numeric value
XInterval: numeric value
3.5, 5, 7, 9..etc
我需要捕获所有数据,并根据此模式进行验证。我试着使用.split()函数,但是弄糊涂了。
以下是我目前掌握的情况:
public boolean validateFile(String info) {
// split all the words and trim commas/whitespaces
String[] validate = info.split("\\s");
for(int i=0; i<validate.length; i++) {
if(validate[i].equals("XStart:") || validate[i].equals("XInterval:")) {
try {
double d = Double.parseDouble(validate[i+1]);
info += validate[i+1]+" ";
}
catch(NumberFormatException x) {
info = "invalid file format";
return false;
}
}
}
return true;
}
public boolean validateFile(字符串信息){
//拆分所有单词并修剪逗号/空格
字符串[]验证=info.split(\\s”);
对于(inti=0;i使用属性名数组,因此可以处理所有泛型
持有模型,表格:
String[] properties = {"YLabel", "XLabel", ...};
将信息拆分为行:
String[] lines = info.split("\r?\n"); // Split on line end
if (lines.length != properties.length + 1) {
return false;
}
for (int lineno = 0; lineno < properties.length; ++lineno) {
if (!lines[lineno].startsWith(properties[lineno] + ":")) {
return false;
}
}
String values = ... and the rest using split("\\s+").
String[]lines=info.split(“\r?\n”);//在行尾拆分
if(lines.length!=properties.length+1){
返回false;
}
对于(int lineno=0;lineno