2013-02-19 65 views
0

我對Java相當陌生,而且我試圖讓它讀取文件。用Java讀取文本文件並轉換爲多個矢量

我有一個數據文本文件,需要作爲數組導入到Java中。

數據文件的第一行是名稱行。所有變量在第一行中都有它們的名稱,數據在列中聚集在一起。我只想將它導入到Java中,並且能夠按照我的意願導出所有變量,就像它們是MATLAB中的矢量一樣。所以基本上我們獲取數據並標記每個向量。我需要代碼儘可能通用,所以它應該讀取可變數量的列和行。我相信,我能夠使用非高效的方法創建陣列。現在我需要將數組分成多個數組,然後將它們轉換爲數字。但我需要根據它們所屬的矢量對數字進行分組。

該文本文件是從Excel電子表格創建的,因此它基本上具有用於不同測量的列,這將創建矢量。另一個向量中的每個列包含行中的數據。

我搜索了很多試圖實現的代碼,但它來到了一個我無法繼續前進的地步。有人可能會告訴我如何在任何意義上進行。也許甚至可以改進閱讀部分,因爲我知道這不是在Java中這樣做的最佳方式。以下是我手邊:

package Testing; 

import java.io.*; 
import java.nio.charset.Charset; 
import java.nio.file.Files; 
import java.nio.file.Path; 
import java.nio.file.Paths; 
import java.util.*; 

public class Read1 { 

    public static void main(String[] args) { 
     try { 
      FileReader fin = new FileReader("C:/jade/test/Winter_Full_clean.txt"); 
      BufferedReader in = new BufferedReader(fin); 
      String str = ""; 
      int count = 0; 
      String line; 
      while ((line=in.readLine()) != null) { 
       if (count==0) { 
        str = line; 
       } 
       else if (in.readLine()!=null) { 
        str = str + line; 
       } 
       count++;     
      } 

      in.close(); 
      //System.out.printf(str); 

      System.out.print(tokens); 
     } catch (Exception e) { 
      System.out.println("error crap" + e.getClass()); 
     } 

    } 


    //{ 

    // Path yourFile = Paths.get("C:/jade/test/Winter_Full_clean.txt"); 
    // Charset charset = Charset.forName("UTF-8"); 

    // List<String> lines = null; 
    // try { 
    //  lines = Files.readAllLines(yourFile, charset); 
    // } catch (IOException e) { 
       // TODO Auto-generated catch block 
    //  e.printStackTrace(); 
    // } 
    // String[] arr = lines.toArray(new String[lines.size()]); 
    // System.out.println(arr); 
    //} 



} 
+0

數據文件看起來像這個Excel文件: http://www.2shared.com/file/TMq8G4Gh/Book1.html – 2013-02-19 19:34:34

+0

這是您的最後一個代碼?它甚至不會編譯('tokens'沒有在任何地方定義)。你也有長字符串中的數據,而不是任何數組。 – madth3 2013-02-19 19:37:39

+0

//System.out.printf(str);之後的行 不起作用。將數據讀入數組後,我卡住了。 – 2013-02-19 19:44:13

回答

0

我有一些代碼,是功能和讀取文本文件到一個數組和分裂的標籤(它的一個TSV文件)。您應該能夠適應以此爲出發點,你的初始數據讀取,然後根據包含您的陣列中的數據,改變你的邏輯,以適應:

try (BufferedReader reader = new BufferedReader(new FileReader(path))) { //path is a String pointing to the file 
       int lineNum = 0; 
       String readLine; 
       while ((readLine = reader.readLine()) != null) { //read until end of stream 
        if (lineNum == 0) { //Skip headers, i.e. start at line 2 of your excel sheet 
         lineNum++; 
         continue; 
        } 
        String[] nextLine = readLine.split("\t"); //Split on tab 
        for (int x = 0; x < nextLine.length; x++) { //do something with ALL the lines. 
         nextLine[x] = nextLine[x].replace("\'", ""); 
         nextLine[x] = nextLine[x].replace("/'", ""); 
         nextLine[x] = nextLine[x].replace("\"", " "); 
         nextLine[x] = nextLine[x].replace("\\xFFFD", ""); 
        } 
       //In this example, to access data for a certain column, 
       //call nextLine[1] for col 1, nextLine[2] for col 2 etc. 
       } //close your loop and go to the next line in your file 
} //close your resources. 
相關問題