如何从文本文件

时间:2018-02-02 21:15:29

标签: java

我有以下txt文件的内容:

 Some text I would like not to inclcude

#id: col1, name, money\

  2017-01-02, Michael,  200 \
  2017-01-01, Tobias,   300 \
  2017-02-03, Susan,    400 \
  2017-05-04, John,     200 \
     ...     ...    ...
 Some text I would like not to inclcude      

我想创建一个Treemap并将col1作为我的密钥:

    import java.io.*;
    import java.util.*;


    class earnings
    {
        public static void main(String[] args) throws FileNotFoundException {

            Scanner scanner = new Scanner(new FileReader("example.txt"));

            TreeMap<String, String> map = new TreeMap<String, String>();

            while (scanner.hasNextLine()) {
                String[] columns = scanner.nextLine().split("\t");
                map.put(columns[0],columns[0]);
            }

            System.out.println(map);
        }
    }

使用Some text I would like not to inclcude输出:

    {=,  2017-01-01,  Tobias,   300 \= 2017-01-01,  Tobias,   300 \,  
 2017-01-02, Michael,  200 \= 2017-01-02, Michael,  200 \,  2017-02-03,  
 Susan,    400 \= 2017-02-03,  Susan,    400 \,  2017-05-04,  John,     
 200 \= 2017-05-04,  John,     200 \,  Some text I would like not to 
 inclcude= Some text I would like not to inclcude, Some text I would 
 like not to inclcude=Some text I would like not to inclcude}

没有Some text I would like not to inclcude的输出仍有一些重复:

    {=,  2017-01-01,  Tobias,   300 \= 2017-01-01,  Tobias,   300 \,  
   2017-01-02, Michael,  200 \= 2017-01-02, Michael,  200 \,  2017-02-
  03,  Susan,    400 \= 2017-02-03,  Susan,    400 \,  2017-05-04,  
  John,     200 \= 2017-05-04,  John,     200 \}

如何设置创建一个Some text I would like not to inclcude并看起来像

的树形图
{ 2017-01-01, Tobias,  300 \ 2017-01-02, Michael, 200 \ 2017-02-03, Susan, 400 \  2017-05-04,  John,     200 \}

或者至少没有重复

2 个答案:

答案 0 :(得分:2)

您将不需要的数据放入集合的原因是您的文件输入很复杂,并且您需要处理规则来确定文件的哪些部分有效。你已经声明你有一个开始触发器和一个停止触发器。

处理的状态是:

  1. 未就绪
  2. 就绪(到达开始触发后) - 在此状态下读入您的地图
  3. 完成(在达到停止触发后)
  4. 目前尚不清楚地图中的值是什么(关键是日期)。现在,我已经连接了名称和金钱值:如果这不正确,请修复此问题。请注意,您需要使用&#34;,&#34 ;;

    拆分字符串

    此代码管理这些状态,并确保进入Map的任何数据都是有效数据(在启动和停止触发器之间)

    public class Earnings {
    
    final static String START_TRIGGER = " Some text I would like not to inclcude";
    final static String STOP_TRIGGER = " Some text I would like not to inclcude";
    
    enum ProcessingState {
        NOT_READY,
        READY,
        FINISHED;
    }
    
    public static void main(String[] args) throws FileNotFoundException {
    
        Scanner scanner = new Scanner(new FileReader("Example.txt"));
    
        TreeMap<String, String> map = new TreeMap<String, String>();
    
        ProcessingState processingState = ProcessingState.NOT_READY;
    
        while (scanner.hasNextLine() && processingState != ProcessingState.FINISHED) {
    
            String lineToProcess = scanner.nextLine();
    
            if (processingState == ProcessingState.READY && lineToProcess.startsWith(STOP_TRIGGER))
                processingState = ProcessingState.FINISHED;
    
            if (processingState == ProcessingState.READY) {
                String[] columns = lineToProcess.split(",");
                map.put(columns[0],columns[1]+", "+columns[2]);
            }
    
            if (processingState == ProcessingState.NOT_READY && lineToProcess.startsWith(START_TRIGGER))
                processingState = ProcessingState.READY;
    
    
        }
    
        System.out.println(map);
    }
    
    }
    

    我测试了你的数据,这产生了:

    {  2017-01-01= Tobias,    300 \,   2017-01-02= Michael,   200 \,   2017-02-03= Susan,     400 \,   2017-05-04= John,      200 \}
    

答案 1 :(得分:0)

String s = "2017-01-01, Tobias,   300 ";
String[] split = s.split(",");
if (split[0].trim().matches("\\d{4}-\\d{2}-\\d{2}")) {
    System.out.println(split[0].trim());
}

在您的方法中,您可以执行以下操作:

while (scanner.hasNextLine()) {
String[] columns = scanner.nextLine().split(",");
    if(columns[0].trim().matches("\\d{4}-\\d{2}-\\d{2}")        
    map.put(columns[0],columns[0]);
}