简体   繁体   English

Java - 文件读取和分隔符需要帮助

[英]Java - Help needed with file reading and delimiter

I have a file with the contents:我有一个包含内容的文件:

  • 85,70,95,82,75 85,70,95,82,75
  • 70,78,85,62,47,53,32 70,78,85,62,47,53,32
  • 99,88,75,85,69,72 99,88,75,85,69,72
  • 79,84,86,91,84,89,78,82,70,75,82 79,84,86,91,84,89,78,82,70,75,82
  • 56,68,0,56 56,68,0,56
  • 96,82,91,90,88 96,82,91,90,88

I need to write a code using the java Scanner class to read and output the code in this order:我需要使用 java 扫描仪 class 编写代码来读取和 output 代码按以下顺序:

  • 85,70,95,82,75 85,70,95,82,75
  • 85 70 95 82 75 85 70 95 82 75
  • 70,78,85,62,47,53,32 70,78,85,62,47,53,32
  • 70 78 85 62 47 53 32 70 78 85 62 47 53 32
  • 99,88,75,85,69,72 99,88,75,85,69,72
  • 99 88 75 85 69 72 etc... 99 88 75 85 69 72 等...

However, I cannot seem to get my code to work for the life of me and I can't figure out what I'm not understanding.但是,我似乎无法让我的代码为我的一生工作,我无法弄清楚我不理解什么。 I have gotten very close with my results but I have tried so many solution at this point I just gave up.我已经非常接近我的结果,但我已经尝试了很多解决方案,我只是放弃了。 This is the current code I have.这是我拥有的当前代码。 Which is almost the best result I've gotten.这几乎是我得到的最好结果。

import java.io.*;
import java.util.*;
public class FileIO
{
    public static void main(String[] args) throws IOException 
    {
        File fileObj2 = new File("Scores.txt");
        Scanner scan2 = new Scanner(fileObj2);
        String line = "";
        String x = "";      

        Scanner scan3 = null;
        scan3 = new Scanner(fileObj2);  
        scan3.useDelimiter(",");

        System.out.println();
        System.out.println("Second Files data below \n ------------- 
        ---------");
        while (scan2.hasNextLine()){
            line = scan2.nextLine();
            System.out.println(line);
            while (scan3.hasNext()){
                line2 = scan3.next();
                System.out.print(line2 + " " );
            }
        }
    }
}

Which give me the output这给了我 output

85,70,95,82,75
85 70 95 82 75
70 78 85 62 47 53 32
99 88 75 85 69 72
79 84 86 91 84 89 78 82 70 75 82
56 68 0 56
96 82 91 90 88 70,78,85,62,47,53,32
99,88,75,85,69,72
79,84,86,91,84,89,78,82,70,75,82
56,68,0,56
96,82,91,90,88

You can use replace method to get the required results.您可以使用replace方法来获得所需的结果。 See below snippet for your reference.请参阅下面的代码段供您参考。

String line = "87,88,89,90,91";
System.out.println(line);
System.out.println(line.replace(',',' '));

small changes in code, please find them below,代码中的小改动,请在下面找到它们,

            while (scan2.hasNextLine()){
            line = scan2.nextLine();
            System.out.println(line);
            scan3 = new Scanner(line);
            scan3.useDelimiter(","); 
            while (scan3.hasNext()){
                System.out.print(scan3.next());
            }
        }

You can use the replaceAll method to remove the commas.您可以使用replaceAll方法删除逗号。

Scores.txt:分数.txt:

85,70,95,82,75
70,78,85,62,47,53,32
99,88,75,85,69,72
79,84,86,91,84,89,78,82,70,75,82
56,68,0,56
96,82,91,90,88

Code:代码:

Scanner in = new Scanner(new File("Scores.txt"));
while(in.hasNextLine()) {
        String line = in.nextLine();
        String out = line.replaceAll(","," ");
        System.out.println(line);
        System.out.println(out);
}

Output: Output:

70,78,85,62,47,53,32
70 78 85 62 47 53 32
99,88,75,85,69,72
99 88 75 85 69 72
79,84,86,91,84,89,78,82,70,75,82
79 84 86 91 84 89 78 82 70 75 82
56,68,0,56
56 68 0 56
96,82,91,90,88
96 82 91 90 88

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM