2014-10-07 75 views
-2

該程序從一個file.txt的讀取和文件到一個數組元素中的數據分配分配的行數。 現在我不知道該文件有多少行,所以我不能初始化數組的大小,我不想使用arrayList。在files.txt到陣列長度的Java

有沒有一種方法來計算行的文件的數量和分配,這個數字將數組的大小?

文件中的數據的例子:

Item 2333 $283 

    Item 2343 $363 

    Item 2655 $243 

    Item 2853 $223 

計數數組大小會被設置爲4

後請善待,我是一個初學者。 在此先感謝。

+1

是的,你可以先閱讀整個文件計數線,創建一個數組,然後循環數據重讀。 – 2014-10-07 06:54:55

+0

或者您可以將數組設置爲較大的任意長度,用'nulls'填充它。如果它不夠大請使用'Array.copyOf' – 2014-10-07 06:56:45

+0

這是一個正確的方法嗎?這只是代碼的一部分。 int arrayl = 0; String [] items = new String [arrayl] ;; 而(inputFile.hasNextLine()){ \t arrayl ++; } – danny 2014-10-07 07:07:14

回答

-2

有跡象表明,你可以考慮幾種選擇。

選項1,每次調整的數組,你得到一個溢出

private static final int INITIAL_SIZE = 10; 
    private static final int INCREMENT = 5; 

    String[] lines = new String[INITIAL_SIZE]; 
    int amountOfLinesRead = 0; 

    // loop until end of file 
    // read line 
    if(amountOfLinesRead == lines.length) { 
     lines = resizeArray(lines); 
    } 
    lines[amountOfLinesRead++] = line; 

    public String[] resizeArray(final String[] originalArray) { 
    // create bigger array 
    String[] newArray = new String[originalArray.length + INCREMENT]; 
    // copy array 
    for(int amountOfLinesCopied = 0; amountOfLinesCopied < originalArrayString.length ; amountOfLinesCopied++) { 
     newArray[amountOfLinesCopied] = originalArray[amountOfLinesCopied]; 
    } 
    return newArray; 
    } 

選項2.經過文件兩次,一次爲多少行計數,一次用於加工線

int amountOfLines; 
    // read lines until end of file is reached 
     amountOfLines ++; 
    // create array 
    String[] lines = new String[amountOfLines]; 
    // loop again 
     // put lines in the array 

選項3.添加標題行到包含一些元數據的文件中,在這種情況下行數(這並不少見)

// read first line of file 
// get the amountOfRecords field from the line 
String[] lines = new String[amountOfRecords] 
int amountOfLinesInArray = 0; 
// loop through file till end of file 
    if(amountOfLinesInArray >= amountOfRecords) { 
    // error! header information was corrupt! 
    } else { 
    lines[amountOfLinesInArray++] = line; 
    } 
+0

我認爲您需要更全面地回答這個問題。你怎麼知道你會溢出?你如何調整大小? – 2014-10-07 07:03:37

+0

工作:)。 – Juru 2014-10-07 07:04:32

+0

考慮使用'Arrays.copyOf' – 2014-10-07 07:10:41

0

你去那裏: 這是很簡單的:

File fout = new File("xxx.txt"); //File Name 

if (!fout.isFile()) // IF CANNOT FIND FILE 
    { 
    OptionPane.showMessageDialog(null, "Can't find: xxx.txt - is not an existing file"); 
    System.out.println("xxx.txt - Parameter is not an existing file"); 
    return; 
    } 

    BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(fout),"UTF-8")); 

    String line = null; 

    int lineCount = 0; 

    while ((line = br.readLine()) != null) 
    { 
    if (line.length()>1) // checks if he line is full and/or not blank 
    { 
    line.trim(); 
    lineCount++; 
    } 
    } 

    br.close(); //closing the BufferedReader 

String[] tempArray = new String[lineCount]; //declare an array with the line number of the file. 

希望這有助於:)

戴夫。

0

我的問題是爲什麼要使用在首位陣列時,你沒有數據的固定長度?你的文件今天可能只有5行和明天30.

BufferedReader br = new BufferedReader(new FileReader("/Users/mn/Desktop/"+ "test")); 
     String line; 
     ArrayList<String> fLines = new ArrayList<String>(); 
     while((line = br.readLine()) != null){ 
      fLines.add(line); 
     } 
     for(int i = 0; i<fLines.size(); i++){ 
      System.out.println(fLines.get(i)); 
     } 

我看不到任何理由你必須使用數組這種工作。