2015-11-24 43 views
-2

我應該讀取文本文件並在每行中打印出一個引用的字符串。引用字符串的大小也必須相同。JAVA讀取文本文件並打印每行的引用字符串

這是讀文件:

"check on","SKY yelow, blue ocean","","1598" 
"6946","Jaming","Mountain range","GOOO, five, three!","912.3" 

而且這是預期的輸出:

check on 
SKY yelow, blue ocean 
1598 
6946 
jaming 
Mountain range 
GOOO, five, three! 
912.3 

我知道如何讀取該文件,但我怎麼會得到輸出如上圖所示?

在此先感謝!

+1

您可以使用Java String split。看看這裏:http://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java另外,你應該嘗試更多,然後再問這裏... –

+0

不知道'吉普'來到哪裏從.. –

+0

@VinodMadyalkar :-) –

回答

0

使用模式和匹配器類。

List<String> lst = new ArrayList(); 
Matcher m = Pattern.compile("\"([^\"]+)\"").matcher(string); 
while(m.find()) 
{ 
lst.add(m.group(1)); 
} 
System.out.println(lst); 
0

你可以做類似下面

Scanner scanner = new Scanner(new File("path")); 
     String input = scanner.next(); 
     Pattern p = Pattern.compile("\"([^\"]*)\""); 
     Matcher m = p.matcher(input); 
     while (m.find()) { 
      System.out.println(m.group(1)); 
     } 
0

你可以從這個代碼需要幫助:

String a = "\"abc\",\"xyzqr\",\"pqrst\",\"\""; // Any string (of your specified type) 
String an[] = a.split(","); 
for (String b : an) { 
     System.out.println(b.substring(1, b.length() - 1)); 
} 

逐行讀取數據線,並使用上面的代碼打印預期的結果。

+0

- 這是行不通的,因爲我讀取文件中的某些引用字符串包含「,」。如果我們分割「,」它會將這些單詞分隔在引用的字符串中。 – HongHua

2

這裏包含了從TXT file.That讀取數據將打印爲你的願望,我所提到的數據的代碼,在txt文件下面

「維魯」,「薩欽」,「德拉威」,」 「Ganguly」,「Rohit」

import java.io.BufferedReader; 
    import java.io.FileReader; 
    import java.io.IOException; 



    public class logic { 

     public static void main(String[] args) 
     { 
      BufferedReader br = null; 

      try { 

       String sCurrentLine; 
       br = new BufferedReader(new FileReader("C:/Users/rajmohan.ravi/Desktop/test.txt")); 

       while ((sCurrentLine = br.readLine()) != null) { 
        reArrange(sCurrentLine.split(",")); 
       } 

      } catch (IOException e) { 
       e.printStackTrace(); 
      } finally { 
       try { 
        if (br != null)br.close(); 
       } catch (IOException ex) { 
        ex.printStackTrace(); 
       } 
      } 
     } 
     public static void reArrange(String[] dataContent) 
     { 
      for(String data : dataContent) 
      { 
       System.out.print(data); 
       System.out.print("\r\n"); 
      } 
     } 


    } 
相關問題