【问题标题】:Splitting textfile at whitespace在空白处拆分文本文件
【发布时间】:2015-05-03 07:39:03
【问题描述】:
Scanner sc = new Scanner("textfile.txt");
List<String> tokens = new ArrayList<String>();

for (int i =0 ; sc.hasNextLine(); i++)
{
    String temp = sc.nextLine();
    tokens.add(temp);
}

我的文本文件看起来像

A
B
C
*empty line* 
D
E
F
*empty line* 

等等..

我遇到的麻烦是我试图将每个部分存储到一个数组中(包括空行),但我不知道如何拆分这些部分。我所说的部分是指 A B C 空行,是一个部分。

【问题讨论】:

  • 所以你想在一个空行上拆分并且还希望这个空行保存为每个组的一部分?另外,当您说空行时,您的意思是没有字符还是只有空格的行?
  • 从外观上看 - 没有字符。

标签: java file split java.util.scanner delimiter


【解决方案1】:

如果您只是将其拆分为新行而不是空格,因为您使用的是hasNextLine()nexLine(),所以您可以试试这个。

final String NEW_LINE = System.getProperty("line.separator");
Scanner sc = new Scanner(new File("textfile.txt"));
List<String> tokens = new ArrayList<String>();
StringBuilder builder = new StringBuilder();
while(sc.hasNextLine()) {
    //Read the next line
    String temp = sc.nextLine();
    builder.append(temp);

    if(temp.trim().equals("")) {
        tokens.add(builder.toString() + NEW_LINE);  //Copy the gotten tokens to the list adding a new line since we read up to, not including, the new line
        builder = new StringBuilder();  //Clear the builder
    }
}
//Copy any remaining characters to the list
tokens.add(builder.toString() + NEW_LINE);

【讨论】:

    【解决方案2】:

    而不是在从文件中读取时将每一行添加到列表中,
    附加到字符串生成器或临时字符串。当您检测到新行时,在将其附加到临时字符串或字符串构建器后,将您到目前为止所获得的所有内容添加到列表中。重复直到出现线条。

    【讨论】:

    • 嗯,这听起来不错,但我尝试编写代码并没有走得太远。 String newline = System.getProperty("line.separator"); StringBuilder sb = new StringBuilder(); for (int i =0 ; sc.hasNextLine(); i++) { if(sc.equals(newline)) { //add all } }
    • 你能发布你从哪里得到的吗?
    • 我无法在我的智能手机上编码,呃。将 sc.nextLine 存储到临时字符串当前行。将当前行附加到字符串生成器。如果 { 当前行等于换行符,则将 stringbuilder.toString() 添加到列表中。通过清空它来重置字符串生成器。
    • 这是我到目前为止得到的 - 但它似乎没有将任何内容附加到空列表中。 for (int i =0 ; sc.hasNextLine(); i++) { StringBuilder sb = new StringBuilder();字符串 currentLine = sc.nextLine(); sb.append(currentLine); if(currentLine == newline) { tokens.add(sb.toString()); } 某人=空; }
    【解决方案3】:

    请参阅最初询问的完整示例:

    package com.raj;
    
    import java.io.File;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
    
    public class Echo {
    
    public static void main(String[] args) throws Exception {
    
    Scanner sc 
    = new Scanner(new File("textfile.txt"));
    List<StringBuilder> tokens 
    = new ArrayList<StringBuilder>();
    StringBuilder builder 
    = new StringBuilder();
    
    boolean saveFlag = true;
    
    
    while (sc.hasNextLine()) {
    String temp = sc.nextLine();
    
    if (temp.isEmpty()) {
    tokens.add(builder);
    builder = new StringBuilder();
    saveFlag = false;
    continue;
    }
    
    builder.append(temp + "\n");
    saveFlag = true;
    }
    sc.close();
    
    if (saveFlag) tokens.add(builder);
    
    for (StringBuilder sb : tokens) {
    System.out.println(sb);
    }
    
    }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-01-22
      • 1970-01-01
      • 2014-01-04
      • 1970-01-01
      • 1970-01-01
      • 2017-07-20
      • 2013-06-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多