Java read large text file with separator

后端 未结 6 824
悲哀的现实
悲哀的现实 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<String> strings = new ArrayList<String>();
    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);
        }
    }
    
    0 讨论(0)
  • 2020-12-21 20:22

    You can read file using BufferedReader or any IO-classes.suppose you have that String in testing.txt file then by reading each line from file you can split it by separator (+). and iterate over array and print.

    BufferedReader br = null;
        try {
            String sCurrentLine;
            br = new BufferedReader(new FileReader("C:\\testing.txt"));//file name with path
            while ((sCurrentLine = br.readLine()) != null) {
                   String[] strArr = sCurrentLine.split("\\+");
                   for(String str:strArr){
                        System.out.println(str);
                          }
                }
    
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (br != null)br.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
    
    0 讨论(0)
  • 2020-12-21 20:26

    You should be able to get a String of length Integer.MAX_VALUE (always 2147483647 (231 - 1) by the Java specification, the maximum size of an array, which the String class uses for internal storage) or half your maximum heap size (since each character is two bytes), whichever is smaller

    How many characters can a Java String have?

    0 讨论(0)
  • 2020-12-21 20:32

    Try this one:

    private static void readLongString(File file){
        ArrayList<String> list = new ArrayList<String>();
        StringBuilder builder = new StringBuilder();
        int r;
        try{
            InputStream in = new FileInputStream(file);
            Reader reader = new InputStreamReader(in);
                while ((r = reader.read()) != -1) {
                    if(r=='+'){
                        list.add(builder.toString());
                        builder = new StringBuilder();
                    }
                    builder.append(r);
                }
        }catch (IOException ex){
            ex.printStackTrace();
        }
        for(String a: list){
            System.out.println(a);
        }
    }
    
    0 讨论(0)
  • 2020-12-21 20:33
    String inpStr = "datadfqsjmqfqs+dataqfsdqjsdgjheqf+qsdfklmhvqziolkdsfnqsdfmqdsnfqsdf+qsjfqsdfmsqdjkgfqdsfqdfsqdfqdfssdqdsfqdfsqdsfqdfsqdfs+qsfddkmgqjshfdfhsqdflmlkqsdfqdqdf+";
    
    String[] inpStrArr = inpStr.split("+");
    

    Hope this is what you need.

    0 讨论(0)
  • 2020-12-21 20:38

    Here is one way, caveat being you can't load more than the max int size (roughly one GB)

      FileReader fr=null;
      try {
          File f=new File("your_file_path");
          fr=new FileReader(f);
          char[] chars=new char[(int)f.length()];
          fr.read(chars);
          String s=new String(chars);
          //parse your string here
      } catch (Exception e) {
          e.printStackTrace();
      }finally {
          if(fr!=null){
              try {
                  fr.close();
              } catch (IOException e) {
    
              }
          }
      }

    0 讨论(0)
提交回复
热议问题