2016-03-06 88 views
0

我有了標記化數據從文本文件

我試圖來標記每行的數據的文本文件成陣列。然而,代幣[0]在讀1 2 3 4。我如何讓它以這樣的方式,其中

tokens[0] = 1 

tokens[1] = 2; 

tokens[2] = 3; 

什麼是錯我的代碼基本上是這樣。

public static void readFile() 
    { 

     BufferedReader fileIn; 

     String[] tokens; 
     String inputLine; 

     try 
     { 
      fileIn = new BufferedReader(new FileReader("test.txt")); 
      inputLine = fileIn.readLine(); 

      while (inputLine != null) 
      { 
       tokens = inputLine.trim().split("\\s+"); 

       System.out.println(tokens[0]); 
       inputLine = fileIn.readLine(); 



      } 
      fileIn.close(); 
     } 

     catch (IOException ioe) 
     { 
      System.out.println("ERROR: Could not open file."); 
      System.out.println(ioe.getMessage()); 
     }  
    } 
} 
+0

你在文件中是否有行分隔符?如果是,那麼你不應該分裂你可能讀的每一行並分配給標記[i] –

回答

1

我認爲你的問題是你使用令牌數組的方式。

使用ArrayList作爲NullOverFlow建議會給你想要的行爲。

下面是一個使用ArrayList的快速解決方案,Raghu K Nair建議採用整行代替分裂。它是完整的 - 你可以運行它自己驗證:

import java.io.FileInputStream; 
import java.io.InputStreamReader; 
import java.io.BufferedReader; 
import java.io.IOException; 
import java.util.List; 
import java.util.ArrayList; 

public class tokenize 
{ 
    public static List<String> readFile(String fileName) 
    { 
     FileInputStream fileStrm = null; 
     InputStreamReader reader = null; 
     BufferedReader buffReader = null; 
     List<String> tokens = null; 
     try 
     { 
      // Set up buffered reader to read file stream. 
      fileStrm = new FileInputStream(fileName); 
      reader = new InputStreamReader(fileStrm); 
      buffReader = new BufferedReader(reader); 
      // Line buffer. 
      String line; 
      // List to store results. 
      tokens = new ArrayList<String>(); 

      // Get first line. 
      line = buffReader.readLine(); 
      while(line != null) 
      { 
       // Add this line to the List. 
       tokens.add(line); 
       // Get the next line. 
       line = buffReader.readLine(); 
      } 
     } 
     catch(IOException e) 
     { 
      // Handle exception and clean up. 
      if (fileStrm != null) 
      { 
       try { fileStrm.close(); } 
       catch(IOException e2) { } 
      } 
     } 
     return tokens; 
    } 

    public static void main(String[] args) 
    { 
     List<String> tokens = readFile("foo.txt"); 
     // You can use a for each loop to iterate through the List. 
     for(String tok : tokens) 
     { 
      System.out.println(tok); 
     } 
    } 
} 

這依賴於在你的問題中所述格式的文本文件。

0

我想這可能會解決你的問題

public static void readFile() { 

    try { 
     List<String> tokens = new ArrayList<>(); 
     Scanner scanner; 
     scanner = new Scanner(new File("test.txt")); 
     scanner.useDelimiter(",|\r\n"); 
     while (scanner.hasNext()) { 
      tokens.add(scanner.next()); 
      System.out.println(tokens); 
     } 
    } catch (FileNotFoundException ex) { 
     Logger.getLogger(MaxByTest.class.getName()).log(Level.SEVERE, null, ex); 
    } 
} 
0

我會建議使用一個ArrayList對於這件事情,如果你想,那麼你可以隨時將其轉換爲字符串數組:

String[] 

試一試:

public void readFromFile(String path) { 

    File file = new File(path); 

    ArrayList<String[]> tokens = new ArrayList<String[]>(); //The reason why we store an array of strings is only because of the split method below. 
    //also, why are you using split? if i were you i would totally avoid using split at all. if that is the case then you should change the above arrayList to this: 
    //ArrayList<String> tokens = new ArrayList<String>(); 

    String inputLine; //the line to be read 

    try (BufferedReader br = new BufferedReader(new FileReader(file))) { //use the "enhanced" try-catch that way you don't have to worry about closing the stream yourself. 

     while ((inputLine = br.readLine()) != null) { //check line 
      tokens.add(inputLine.trim().split("\\s+")); //put in the above arraylist 
     } 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 


    //Testing 
    for (String[] token : tokens) { 
     System.out.println(Arrays.toString(token)); 
    } 

}