1

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

datadfqsjmqfqs+dataqfsdqjsdgjheqf+qsdfklmhvqziolkdsfnqsdfmqdsnfqsdf+qsjfqsdfmsqdjkgfqdsfqdfsqdfqdfssdqdsfqdfsqdsfqdfsqdfs+qsfddkmgqjshfdfhsqdflmlkqsdfqdqdf+

I want to read this string in the text file as one big java String. Is this possible? I know the use of the split method.

It worked to read it line by line, but what I really need is to split this long text-string at the '+' sign. Afterwards I want to store it as an array, arraylist, list,...

Can anyone help me with this? Because every information on the internet is just about reading a file line by line. Thanks in advance!

1

6 Answers 6

3
String inpStr = "datadfqsjmqfqs+dataqfsdqjsdgjheqf+qsdfklmhvqziolkdsfnqsdfmqdsnfqsdf+qsjfqsdfmsqdjkgfqdsfqdfsqdfqdfssdqdsfqdfsqdsfqdfsqdfs+qsfddkmgqjshfdfhsqdflmlkqsdfqdqdf+";

String[] inpStrArr = inpStr.split("+");

Hope this is what you need.

Sign up to request clarification or add additional context in comments.

4 Comments

Okay, but how is it possible to read the string then? I think there will be an overflow
iterate the loop and extract each element from the array. for (int i = 0; i < inpStrArr .length; i++) { String str = inpStrArr [i]; System.out.println(str); }
It'd be inpStr.split("\\+")
No, what I mean is: how can I read the huge string from my .txt file to a java String?
2

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();
            }
        }

Comments

2

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);
    }
}

Comments

2

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?

Comments

0

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);
    }
}

Comments

0

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) {

          }
      }
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.