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!

arnoutvh
  • 141
  • 1
  • 3
  • 13
  • 1
    here is the answer for your question: http://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java – m.cekiera Apr 28 '15 at 13:36

6 Answers6

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

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

Hope this is what you need.

Saurabh Jhunjhunwala
  • 2,832
  • 3
  • 29
  • 57
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);
    }
}
Ori Lentz
  • 3,668
  • 6
  • 22
  • 28
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();
            }
        }
Prashant
  • 2,556
  • 2
  • 20
  • 26
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?

Community
  • 1
  • 1
Archana Mundaye
  • 523
  • 1
  • 5
  • 19
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);
    }
}
m.cekiera
  • 5,365
  • 5
  • 21
  • 35
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) {

          }
      }
  }
factotum
  • 900
  • 10
  • 13