2015-04-28 66 views
1

我試圖讀取一個大的文本文件的形式:Java的閱讀大量文本文件分隔符

datadfqsjmqfqs+dataqfsdqjsdgjheqf+qsdfklmhvqziolkdsfnqsdfmqdsnfqsdf+qsjfqsdfmsqdjkgfqdsfqdfsqdfqdfssdqdsfqdfsqdsfqdfsqdfs+qsfddkmgqjshfdfhsqdflmlkqsdfqdqdf+ 

我想讀這個字符串中的文本文件作爲一個大的Java字符串。這可能嗎?我知道使用拆分方法。

它一行一行地讀取它,但我真正需要的是將這個長文本字符串拆分爲'+'號。之後我想將它作爲數組,數組列表,列表存儲...

任何人都可以幫助我嗎?因爲互聯網上的每一條信息都是關於逐行閱讀文件的。 在此先感謝!

+1

這裏是你的問題的答案:http://stackoverflow.com/questions/3481828/如何在Java中拆分字符串 –

回答

2

可以使用BufferedReader讀取文件或任何IO-classes.suppose你在testing.txt文件中的字符串然後通過讀文件中的每一行都可以通過分隔符分割(+)。並遍歷數組和打印。

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

非常感謝!你幫我了。 – ernie379

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

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

希望這是你所需要的。

+0

好的,但是如何讀取字符串呢?我認爲會有一個溢出 – ernie379

+0

迭代循環並從數組中提取每個元素。 for(int i = 0; i

+0

這將是'inpStr.split(「\\ +」)' – Saravana

2

在我看來,你的問題是你不想逐行閱讀文件。因此,相反,嘗試在部分(比如每次20個字符,並建立您的字符串)閱讀它:

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

謝謝你archana這是有幫助的。 –

2

你應該能夠得到長度Integer.MAX_VALUE的的字符串(總是2147483647(231 - 由Java規範,數組,String類用於內部存儲的最大大小1))或一半的最大堆大小(因爲每個字符是兩個字節),取其較小者

How many characters can a Java String have?

0

試試這個:

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

這裏有一種方法,CAV吃是不能加載超過最大INT規模以上(大約一個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) { 
 

 
      } 
 
     } 
 
    }