• <bdo id='i8HPV'></bdo><ul id='i8HPV'></ul>

    1. <i id='i8HPV'><tr id='i8HPV'><dt id='i8HPV'><q id='i8HPV'><span id='i8HPV'><b id='i8HPV'><form id='i8HPV'><ins id='i8HPV'></ins><ul id='i8HPV'></ul><sub id='i8HPV'></sub></form><legend id='i8HPV'></legend><bdo id='i8HPV'><pre id='i8HPV'><center id='i8HPV'></center></pre></bdo></b><th id='i8HPV'></th></span></q></dt></tr></i><div id='i8HPV'><tfoot id='i8HPV'></tfoot><dl id='i8HPV'><fieldset id='i8HPV'></fieldset></dl></div>
        <tfoot id='i8HPV'></tfoot>

        <small id='i8HPV'></small><noframes id='i8HPV'>

        <legend id='i8HPV'><style id='i8HPV'><dir id='i8HPV'><q id='i8HPV'></q></dir></style></legend>

        java中的txt文件格式验证

        txt file format validation in java(java中的txt文件格式验证)
        <legend id='qWle9'><style id='qWle9'><dir id='qWle9'><q id='qWle9'></q></dir></style></legend>
        <tfoot id='qWle9'></tfoot>
      1. <small id='qWle9'></small><noframes id='qWle9'>

            • <bdo id='qWle9'></bdo><ul id='qWle9'></ul>

                    <tbody id='qWle9'></tbody>
                  <i id='qWle9'><tr id='qWle9'><dt id='qWle9'><q id='qWle9'><span id='qWle9'><b id='qWle9'><form id='qWle9'><ins id='qWle9'></ins><ul id='qWle9'></ul><sub id='qWle9'></sub></form><legend id='qWle9'></legend><bdo id='qWle9'><pre id='qWle9'><center id='qWle9'></center></pre></bdo></b><th id='qWle9'></th></span></q></dt></tr></i><div id='qWle9'><tfoot id='qWle9'></tfoot><dl id='qWle9'><fieldset id='qWle9'></fieldset></dl></div>
                1. 本文介绍了java中的txt文件格式验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  验证 .txt 文件是否为:

                  What is the best way to validate whether a .txt file is:

                  • 实际上是一个 .txt 文件,而不是其他类型的文件,只是更改了扩展名.

                  • In fact a .txt file and not another type of file with only the extension changed.

                  .txt文件的格式与指定的格式匹配(因此能够正确解析,包含所有相关信息等)

                  The format of the .txt file matches the specified format (so it is able to be parsed correctly, contains all the relevant information, etc.)

                  这一切都是在 Java 中完成的,其中将检索一个文件,然后需要检查它以确保它是应该的.到目前为止,我只发现 JHOVE(现在是 JHOVE2)作为这项任务的工具,但在 Java 代码中而不是通过命令行实现它的文档方式中没有找到太多.感谢您的帮助.

                  This is all being done in Java, where a file will be retrieved and then needs to be checked to make sure it is what it is supposed to be. So far I have only found JHOVE (and now JHOVE2) as tools for this task but have not found much in the way of documentation for implementing it within Java code as opposed to through the command line. Thanks for your help.

                  推荐答案

                  听起来您正在寻找一种通用的格式化选项,我可以向您推荐正则表达式吗?您可以使用正则表达式进行各种不同类型的匹配.我在下面写了一个简单的例子[对于所有那些正则表达式专家,如果我没有使用完美的表达,请怜悯我;)].您可以将 REGEX 和 MAX_LINES_TO_READ 常量放入属性文件并对其进行修改以使其更加通用.

                  As it sounds like you're looking for a general sort of formatting option, could I recommend regular expressions to you? You can do all sorts of different kinds of matching using regex. I've written a simple example below [for all those regex experts out there, have mercy on me if I didn't use the perfect expression ;) ]. You could put the REGEX and MAX_LINES_TO_READ constants into a properties file and modify that to make it even more generalized.

                  您基本上会测试您的.txt"文件的最大行数(但是需要很多行才能确定格式是否良好 - 您也可以将正则表达式用于标题行或根据需要执行多个不同的正则表达式测试格式),如果所有这些行都匹配,文件将被标记为有效".

                  You would basically test your ".txt" file for a maximum number of lines (however many lines are needed to establish the formatting is good - you could also use regular expressions for a header line or do multiple different regular expressions as needed to test the formatting) and if all those lines matched, the file would be flagged as "valid".

                  这只是您可能运行的示例.您应该实施适当的异常处理,而不仅仅是为一个捕获异常".

                  This is just an example for you to possibly run with. You should implement proper exception handling other than just catching "Exception" for one.

                  要在 Java 中测试您的正则表达式,http://www.regexplanet.com/simple/index.html 效果很好.

                  For testing your regular expressions in Java, http://www.regexplanet.com/simple/index.html works very nice.

                  这里是ValidateTxtFile"源...

                  Here's the "ValidateTxtFile" source...

                  import java.io.*;
                  
                  public class ValidateTxtFile {
                  
                      private final int MAX_LINES_TO_READ = 5;
                  
                      private final String REGEX = ".{15}[ ]{5}.{15}[ ]{5}[-]\d{2}\.\d{2}[ ]{9}\d{2}/\d{2}/\d{4}";
                  
                      public void testFile(String fileName) {
                  
                          int lineCounter = 1;
                  
                          try {
                  
                              BufferedReader br = new BufferedReader(new FileReader(fileName));
                  
                              String line = br.readLine();
                  
                              while ((line != null) && (lineCounter <= MAX_LINES_TO_READ)) {
                  
                                  // Validate the line is formatted correctly based on regular expressions                
                                  if (line.matches(REGEX)) {
                                      System.out.println("Line " + lineCounter + " formatted correctly");
                                  }
                                  else {
                                      System.out.println("Invalid format on line " + lineCounter + " (" + line + ")");
                                  }
                  
                                  line = br.readLine();
                                  lineCounter++;
                              }
                  
                          } catch (Exception ex) {
                              System.out.println("Exception occurred: " + ex.toString());
                          }
                      }
                  
                      public static void main(String args[]) {
                  
                          ValidateTxtFile vtf = new ValidateTxtFile();
                  
                          vtf.testFile("transactions.txt");
                      }   
                  }
                  

                  这是transactions.txt"中的内容...

                  Here's what's in "transactions.txt"...

                  Electric            Electric Co.        -50.99         12/28/2011
                  Food                Food Store          -80.31         12/28/2011
                  Clothes             Clothing Store      -99.36         12/28/2011
                  Entertainment       Bowling             -30.4393       12/28/2011
                  Restaurant          Mcdonalds           -10.35         12/28/11
                  

                  我运行应用程序时的输出是...

                  The output when I ran the app was...

                  Line 1 formatted correctly
                  Line 2 formatted correctly
                  Line 3 formatted correctly
                  Invalid format on line 4 (Entertainment       Bowling             -30.4393       12/28/2011)
                  Invalid format on line 5 (Restaurant          Mcdonalds           -10.35         12/28/11)
                  


                  编辑 2011 年 12 月 29 日上午 10:00 左右
                  不确定这是否存在性能问题,但仅供参考,我多次复制transactions.txt"中的条目以构建一个包含大约 130 万行的文本文件,并且我能够通过整个文件在我的电脑上大约 7 秒.我将 System.out 更改为仅在无效 (524,288) 和有效 (786,432) 格式条目的末尾显示总计数.transactions.txt"的大小约为 85mb.


                  EDIT 12/29/2011 about 10:00am
                  Not sure if there is a performance concern on this or not, but just as an FYI I duplicated the entries in "transactions.txt" several times to build a text file with about 1.3 million rows in it and I was able to get through the whole file in about 7 seconds on my PC. I changed the System.out's to just show a grand total count at the end of invalid (524,288) and valid (786,432) formatted entries. "transactions.txt" was about 85mb in size.

                  这篇关于java中的txt文件格式验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Lucene Porter Stemmer not public(Lucene Porter Stemmer 未公开)
                  How to index pdf, ppt, xl files in lucene (java based or python or php any of these is fine)?(如何在 lucene 中索引 pdf、ppt、xl 文件(基于 java 或 python 或 php 中的任何一个都可以)?)
                  KeywordAnalyzer and LowerCaseFilter/LowerCaseTokenizer(KeywordAnalyzer 和 LowerCaseFilter/LowerCaseTokenizer)
                  How to search between dates (Hibernate Search)?(如何在日期之间搜索(休眠搜索)?)
                  How to get positions from a document term vector in Lucene?(如何从 Lucene 中的文档术语向量中获取位置?)
                  Java Lucene 4.5 how to search by case insensitive(Java Lucene 4.5如何按不区分大小写进行搜索)

                    • <legend id='8IUfm'><style id='8IUfm'><dir id='8IUfm'><q id='8IUfm'></q></dir></style></legend>
                    • <small id='8IUfm'></small><noframes id='8IUfm'>

                      <tfoot id='8IUfm'></tfoot>
                    • <i id='8IUfm'><tr id='8IUfm'><dt id='8IUfm'><q id='8IUfm'><span id='8IUfm'><b id='8IUfm'><form id='8IUfm'><ins id='8IUfm'></ins><ul id='8IUfm'></ul><sub id='8IUfm'></sub></form><legend id='8IUfm'></legend><bdo id='8IUfm'><pre id='8IUfm'><center id='8IUfm'></center></pre></bdo></b><th id='8IUfm'></th></span></q></dt></tr></i><div id='8IUfm'><tfoot id='8IUfm'></tfoot><dl id='8IUfm'><fieldset id='8IUfm'></fieldset></dl></div>

                          <bdo id='8IUfm'></bdo><ul id='8IUfm'></ul>
                            <tbody id='8IUfm'></tbody>