【问题标题】:Read a string from a file, skip the rest of the line after a certain character [closed]从文件中读取一个字符串,在某个字符之后跳过该行的其余部分[关闭]
【发布时间】:2015-04-27 11:28:52
【问题描述】:

我目前正在学习如何使用 Java。我正在尝试使用扫描仪类读取文件,并且我想读取文件但忽略某个字符之后的其余行,例如#。说文件读取

 P5 #ignorethis
 #ignorealso
 123 123 123 #thisneedstogo
 355 255 345 #alsothis goes

我试图读取的文件在符号“#”之后有 cmets,它们一直持续到行尾。我想读取文件的字符串,同时忽略“#”以及之后的所有内容。 任何帮助将不胜感激,谢谢:)

【问题讨论】:

  • 您在哪个部分有问题?读取指定字符后的行或跳过?你试过什么?

标签: java string file java.util.scanner


【解决方案1】:

一次读取一行文件,然后考虑使用replaceAll(String string) 方法,该方法在您刚刚读取的行上采用正则表达式。然后你会使用类似这样的东西:#.*$ 来替换 # 字符以及后面的任何内容,直到字符串的末尾用一个空字符串。

完成后,您可以将字符串写回其他文件或控制台。

【讨论】:

    【解决方案2】:

    来自 Scanner 的课程文档:

    扫描器使用分隔符模式将其输入分解为标记, 默认情况下匹配空格。

    你可以使用 useDelimiter 方法和正则表达式来做到这一点

    举个例子:

        public static void main(String[] args) {
        String s = " P5 #ignorethis\n" +
                " #ignorealso\n" +
                " 123 123 123 #thisneedstogo\n" +
                " 355 255 345 #alsothis goes";
        Scanner scanner = new Scanner(s).useDelimiter("#.*");
        while (scanner.hasNext()){
            System.out.print(scanner.next());
        }
    }
    

    【讨论】:

      【解决方案3】:

      你可以这样写:

      Scanner scanner = new Scanner(new FileInputStream(new File("input.txt")));
      
             while(scanner.hasNextLine()){
                 String str = scanner.nextLine();
                 System.out.println(str.substring(0, str.indexOf("#")));
             }
             scanner.close;
      

      希望这会有所帮助。

      【讨论】:

        【解决方案4】:

        您也可以阅读整行,然后只使用其中的一部分,如下所示:

        public void readFile(File file){
            String line = "";
            try{
            scanner = new Scanner(file);
            }catch (FileNotFoundException ex){
                ex.printStackTrace();
                System.out.println("File not found");
            }
            while (scanner.hasNext()){
                line = scanner.nextLine();
                String parts[] = line.split("#");
                System.out.println(parts[0]);
            }
        }
        

        此方法将换行读取到字符串,将字符串拆分到“#”的位置,并使用“#”出现之前的部分。这是输出:

        P5

        123 123 123
        355 255 345

        【讨论】:

          【解决方案5】:
          BufferedReader br = new BufferedReader(new FileReader("name_file"))) {
          String line;
          String result = "";
          
                while ((line = br.readLine()) != null) {
          
                        if(line.trim().startsWith("#"))
                             {
                               System.out.println("next line");
                             }
                         else
                             {
                                 int index = line.indexOf("#"); 
                                 if(index != -1){                        
                                 String split = line.substring(0,index);
                                 String[] sLine = line.trim().split("\t");
                                 result = result + " " +split;
                                 }
                         else
                              {
          
                                 String[] sLine = line.trim().split("\t");
                                 result = result + " " +line;
                               }
          
                             }
          
                      }
          
                    br.close();
                    System.out.println(result);
          

          【讨论】:

            猜你喜欢
            • 2020-11-23
            • 2015-04-10
            • 2013-03-28
            • 2023-03-27
            • 2021-12-22
            • 2015-03-04
            • 1970-01-01
            • 2021-05-30
            相关资源
            最近更新 更多