【问题标题】:How to check if a string in a text file is correct如何检查文本文件中的字符串是否正确
【发布时间】:2017-02-02 14:52:15
【问题描述】:

我有一个文本文件,我需要检查它是否正确。该文件的类型应为:

XYab 
XYab
XYab

其中 X,Y,a,b 只能取一定范围的值。例如 b 必须是 1 到 8 之间的值。这些值由 4 个枚举定义(1 个枚举用于 X,1 个枚举用于 Y,等等)。我唯一想到的是这样的:

BufferedReader br = new BufferedReader(new FileReader(FILENAME)
String s;
while ((s = br.readLine()) != null) {
    if(s.charAt(0)==Enum.example.asChar())
}

当然,它只检查文件的第一行。关于如何检查所有文件行的任何建议?

【问题讨论】:

  • 所有行只有一个XYab序列?或者可以逐行多次
  • 一行只能有一个XYab序列
  • 你应该试试正则表达式。
  • 你能为我的程序做一个简单的正则表达式示例吗?

标签: java text-files controls


【解决方案1】:

你可以试试这样的,(根据你的枚举修改)

@Test
public void findDates() {
        File file = new File("pathToFile");
        try{
            Assert.assertTrue(validateFile(file));
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }

    private boolean validateFile(File file) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;
        // add yours enumns to one list
        List<Enum> enums = new ArrayList<>();
        enums.add(EnumX);
        enums.add(EnumY);
        enums.add(EnumA);
        enums.add(EnumB);

        while ((line = br.readLine()) != null) {
            // process the line.
            if(line.length() > 4){
                return false;
            }
            //for each position check if the value is valid for the enum
            for(int i = 0; i < 4; i++){
                if(enums.get(i)).valueOf(line.charAt(i)) == null){
                    return false;
                }
            }
        }
        return true;
    }

【讨论】:

    【解决方案2】:

    不要忘记冲突,但如果你有数字或字符串,你可以使用正则表达式来做到这一点。

    在每个 Enum 上,您必须像这样执行函数正则表达式。您可以改用外部 Helper。

    enum EnumX {
        A,B,C,D, ...;
        ...
        // you enum start to 0
        public static String regexp(){
            return "[0-" + EnumX.values().length +"]"; 
        }
    }
    
    // it is working also if you have string in your file
    enum EnumY{
        A("toto"),B("titi"),C("tata"),D("loto");
    
        public static String regexp(){
            StringBuilder regexp = new StringBuilder("[");
            for(EnumY value : EnumY.values()){
                    regexp.append(value).append(",");
            }
            regexp.replace(regexp.length()-1, regexp.length(), "]");
            return regexp.toString();
        }
    }   
    
    public boolean isCorrect(File file){
            // build the regexp         
            String regexp = EnumX.regexp() + EnumY.regexp() + EnumA.regexp() +EnumB.regexp();
    
            // read the file
            BufferedReader br = new BufferedReader(new FileReader(file));
            String line;
    
            while ((line = br.readLine()) != null) {
                if (line.matches(regexp) == false){
                    // this line is not correct
                    return false;
                }
            }
            return true;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-11
      • 2013-12-15
      • 2011-09-30
      • 2019-08-25
      • 2012-02-19
      • 2019-02-27
      • 1970-01-01
      • 2022-10-13
      相关资源
      最近更新 更多