Java read large text file with separator

后端 未结 6 823
悲哀的现实
悲哀的现实 2020-12-21 20:00

I\'m trying to read a large text file in the form of:

datadfqsjmqfqs+dataqfsdqjsdgjheqf+qsdfklmhvqziolkdsfnqsdfmqdsnfqsdf+qsjfqsdfmsqdjkgfqdsfqdfsqdfqdfssdqd         


        
6条回答
  •  醉梦人生
    2020-12-21 20:16

    It seems to me like your problem is that you don't want to read the file line by line. So instead, try reading it in parts (say 20 characters each time and building your string):

    char[] c = new char[20]; //best to save 20 as a final static somewhere
    
    ArrayList strings = new ArrayList();
    StringBuilder sb = new StringBuilder();
    
    BufferedReader br = new BufferedReader(new FileReader(filename));
    
    while (br.read(c) == 20) {
    
        String str = new String(c);
    
        if (str.contains("+") {
    
            String[] parts = str.split("\\+");
            sb.append(parts[0]);
            strings.add(sb.toString());
    
            //init new StringBuilder:
            sb = new StringBuilder();
            sb.add(parts[1]);
    
        } else {
            sb.append(str);
        }
    }
    

提交回复
热议问题