java-如何通过跳过某些部分来读取文件并输入数据信息二维数组

前端之家收集整理的这篇文章主要介绍了java-如何通过跳过某些部分来读取文件并输入数据信息二维数组 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在尝试流式传输文件并将其内容输入2D数组.我有这段代码可以成功读取文件的所有内容.我想以这样的方式调整代码,使其跳过文件的第一行(#R1 R2 R3 R4 R5)以及文件每一行上的数字.在某种程度上,我想删除arr [X] [Y]中所有X = 0和Y = 0的元素.我想在使用扫描仪读取文件的同时执行此操作,而不是创建一个新数组并遍历第一个数组并将所需数据存储在新数组中.

这是流文件

  1. # R1 R2 R3 R4 R5
  2. 1 J S2 Q S2 J
  3. 2 J S2 Q S2 J
  4. 3 J S2 Q S2 J
  5. 4 J S2 Q S2 J
  6. 5 J Q S5 Q J
  7. 6 S3 Q S5 Q S3
  8. 7 S3 Q S5 Q S3
  9. 8 S3 Q S5 Q S3
  10. 9 S3 S1 S5 S1 S3
  11. 10 A S1 S5 S1 A
  12. 11 A S1 K S1 A
  13. 12 A S1 K S1 A
  14. 13 A S1 K S1 A
  15. 14 A S1 K S1 A
  16. 15 S2 A K A S2
  17. 16 S2 A S3 A S2
  18. 17 S2 A S3 A S2
  19. 18 S2 A S3 A S2
  20. 19 S2 A S3 A S2
  21. 20 Q S5 S3 S5 Q
  22. 21 Q S5 J S5 Q
  23. 22 Q S5 J S5 Q
  24. 23 Q S5 J S5 Q
  25. 24 Q S5 J S5 Q
  26. 25 S4 K S2 K S4
  27. 26 S4 K S2 K S4
  28. 27 S4 K S2 K S4
  29. 28 S4 K S2 K S4
  30. 29 S4 K S2 K S4
  31. 30 K S4 S2 S4 K
  32. 31 K S4 A S4 K
  33. 32 K S4 A S4 K
  34. 33 K S4 A S4 K
  35. 34 K S4 A S4 K
  36. 35 S1 S4 A S4 S1
  37. 36 S1 A S1 A S1
  38. 37 S1 A S1 A S1
  39. 38 S1 A S1 A S1
  40. 39 S1 A S1 A S1
  41. 40 Q S3 S1 S3 Q
  42. 41 Q S3 Q S3 Q
  43. 42 Q S3 Q S3 Q
  44. 43 Q S3 Q S3 Q
  45. 44 Q J Q J Q
  46. 45 S2 J S4 J S2
  47. 46 S2 J S4 J S2
  48. 47 S2 J S4 J S2
  49. 48 S2 S2 S4 S2 S2
  50. 49 S2 S2 A S2 S2
  51. 50 J S2 A S2 J
  52. 51 J S2 A S2 J
  53. 52 J Q A Q J
  54. 53 J Q A Q J
  55. 54 J Q J Q J
  56. 55 S5 Q J Q S5
  57. 56 S5 J J J S5
  58. 57 S5 J J J S5
  59. 58 S5 J S1 J S5
  60. 59 S5 J S1 J S5
  61. 60 A S3 S1 S3 A
  62. 61 A S3 S1 S3 A
  63. 62 A S3 Q S3 A
  64. 63 A S3 Q S3 A
  65. 64 A K Q K A
  66. 65 S1 K Q K S1
  67. 66 S1 K S4 K S1
  68. 67 S1 K S4 K S1
  69. 68 S1 Q S4 Q S1
  70. 69 S1 Q S4 Q S1
  71. 70 K Q K Q K
  72. 71 K Q K Q K
  73. 72 K J K J K
  74. 73 K J K J K
  75. 74 K J K J K
  76. 75 S3 J J J S3
  77. 76 S3 NA J NA S3
  78. 77 S3 NA J NA S3
  79. 78 S3 NA J NA S3
  1. public static void main(String[] args) throws FileNotFoundException
  2. {
  3. Scanner sc = new Scanner(new BufferedReader(new FileReader("reels_template.txt")));
  4. int arrX = 78;
  5. int arrY = 6;
  6. String [][] arr = new String[arrX][arrY];
  7. String[] line = {};
  8. for (int i=0; i<arrX; i++) {
  9. if (sc.hasNextLine()) {
  10. line = sc.nextLine().trim().split("\\s+");
  11. }
  12. for (int j=0; j<arrY; j++) {
  13. arr[i][j] = line[j] + " ";
  14. }
  15. }
  16. for (int i = 0; i < arrX; i++) {
  17. for ( int j = 0; j < arrY; j++) {
  18. System.out.print(arr[i][j]);
  19. }
  20. System.out.println();
  21. }
  22. }

这是上面代码输出

  1. # R1 R2 R3 R4 R5
  2. 1 J S2 Q S2 J
  3. 2 J S2 Q S2 J
  4. 3 J S2 Q S2 J
  5. 4 J S2 Q S2 J
  6. 5 J Q S5 Q J
  7. 6 S3 Q S5 Q S3
  8. 7 S3 Q S5 Q S3
  9. 8 S3 Q S5 Q S3

这是所需的输出

  1. J S2 Q S2 J
  2. J S2 Q S2 J
  3. J S2 Q S2 J
  4. J S2 Q S2 J
  5. J Q S5 Q J
  6. S3 Q S5 Q S3
  7. S3 Q S5 Q S3
  8. S3 Q S5 Q S3

编辑:通过使用扫描仪读取第一行,然后循环删除不需要的行并重新调整第二个for循环,解决了问题.代码示例:

  1. public static void main(String[] args) throws FileNotFoundException
  2. {
  3. Scanner sc = new Scanner(new BufferedReader(new FileReader("reels_template.txt")));
  4. final int arrX = 78;
  5. final int arrY = 5;
  6. String[][] arr = new String[arrX][arrY];
  7. String[] line = {};
  8. line = sc.nextLine().trim().split("\\s+");
  9. for (int i=0; i<arrX; i++) {
  10. if (sc.hasNextLine()) {
  11. line = sc.nextLine().trim().split("\\s+");
  12. }
  13. for (int j=0; j<=arrY; j++) {
  14. if (j!=0) {
  15. arr[i][j-1] = line[j] + " ";
  16. }
  17. }
  18. }
  19. for (int i = 0; i < arrX; i++) {
  20. for ( int j = 0; j < arrY; j++) {
  21. System.out.print(arr[i][j]);
  22. }
  23. System.out.println();
  24. }
  25. }
最佳答案
最简单的解决方案(即使很明显)将是根本不阅读您不想阅读的元素.您的代码将被这样修改

  1. public static void main(String[] args) throws FileNotFoundException {
  2. Scanner sc = new Scanner(new BufferedReader(new FileReader("reels_template.txt")));
  3. int arrX = 77;
  4. int arrY = 4;
  5. String [][] arr = new String[arrX][arrY];
  6. String[] line = {};
  7. sc.nextLine(); // remove the first line from the scanner
  8. for (int i=0; i<arrX; i++) {
  9. if (sc.hasNextLine())
  10. line = sc.nextLine().trim().split("\\s+");
  11. for (int j=0; j<arrY; j++) {
  12. arr[i][j] = line[j+1] + " "; //always skips first element of 'line'
  13. }
  14. }
  15. for (int i = 0; i < arrX; i++) {
  16. for ( int j = 0; j < arrY; j++)
  17. System.out.print(arr[i][j]);
  18. System.out.println();
  19. }
  20. }

但是,这段代码仅适用于具有特定数量的“行”和每行中具有特定数量的元素的文件.如何使其更具“动态性”?

以下方法会将文件的每一行作为字符串添加到ArrayList< String>中.然后,它将删除第一行,然后删除每行前面的第一个数字.

  1. public ArrayList<String> readFileText (String filename){
  2. //ArrayList to hold all the lines
  3. ArrayList<String> lines = null;
  4. //Get lines of text (Strings) as a stream
  5. try (Stream<String> stream = Files.lines(Paths.get(filename))){
  6. // convert stream to a List-type object
  7. lines = (ArrayList<String>)stream.collect(Collectors.toList());
  8. }
  9. catch (IOException ioe){
  10. System.out.println("\nCould not read lines of text from the file.\n" +
  11. "Reason: parameter \"" + ioe.getMessage() + "\"");
  12. }
  13. catch (SecurityException se){
  14. System.out.println("Could not read the file provided." +
  15. "Please check if you have permission to access it.");
  16. }
  17. lines.remove(0); //Remove the first line of the file. Line at index 0.
  18. for(String line : lines)
  19. //Fine the first occurrence of white-space in each line,//and use that substring,to remove the number of the line.
  20. line = line.substring(line.indexOf(" "));
  21. return lines;
  22. }

要在您的代码中使用此方法

  1. public static void main(String[] args){
  2. ArrayList<String> allFileLines = readFileText("reels_template.txt");
  3. for(String line : allFileLines)
  4. System.out.println(line);
  5. }

看看主要方法现在看起来有多优雅?这就是方法的使用方式.始终要使它们具有更动感的特性,而不是硬编码的样式.

猜你在找的Java相关文章