2014-02-24 27 views
0

我無法弄清楚如何通過這個循環來代替只是重複代碼,把地獄搞砸了!僅僅想學習如何將文件內容保存到for循環以便將來獲得知識,字符串數組已經轉入了使用5次迭代的代碼。我試了幾個小時,但它只是打印文件名,似乎無法得到文件內容。使用數組來存儲/打印Linux文件的問題

/************************************************************************* 
* LinuxSys.java 
* 
* This program reads text from a file 
**************************************************************************/ 

import java.util.*; 
import java.io.*; 

public class LinuxSys { 
    public static void main (String[] args) { 
     String systemInfo[] = new String [5]; 
     int i = 0; 
     // using _ to simulate file paths to test on local cpu, as it is easier/quicker 
     // than logging onto server/copy pasting code into new pico file 
     systemInfo[0] = "_proc_sys_kernel_hostname.txt"; //local files 
     systemInfo[1] = "_proc_meminfo.txt";    //local files 
     systemInfo[2] = "_proc_version.txt";    //local files 
     systemInfo[3] = "_proc_sys_kernel_hostname.txt"; //local files 
     //systemInfo[0] = "_proc_sys_kernel_hostname.txt"; //local files 


     // 1st try to print server host name file 
     try { 
      BufferedReader inputStream = 
      new BufferedReader(new FileReader(systemInfo[i])); 
      String line = "blank"; 
      while (line != null) { 
       if((line = inputStream.readLine()) != null) { 
        System.out.println(line); 
        i++; // increment systemInfo[] array position 
       } // end if 
      } //end while 
      System.out.println(); // create space 
      inputStream.close(); 
     } // end try 
     catch(FileNotFoundException e) { 
      System.out.println("File was not found"); 
      System.out.println("or could not be opened"); 
     } //end catch 
     catch(IOException e) { 
      System.out.println("Error reading from file"); 
     } //end catch 

     // 2nd try to print server memory file 
     { 
      BufferedReader inputStream = 
      new BufferedReader(new FileReader(systemInfo[i])); 
      String line = "blank"; 
      while (line != null) { 
       if((line = inputStream.readLine()) != null) { 
        System.out.println(line); 
        i++; // increment systemInfo[] array position 
       } // end if 
      } //end while 
      System.out.println(); // create space 
      inputStream.close(); 
     } // end try 
     catch(FileNotFoundException e) { 
      System.out.println("File was not found"); 
      System.out.println("or could not be opened"); 
     } //end catch 
     catch(IOException e) { 
      System.out.println("Error reading from file"); 
     } //end catch 

     // 3rd try to print version file 
     try { 
      BufferedReader inputStream = 
      new BufferedReader(new FileReader(systemInfo[i])); 
      String line = "blank"; 
      while (line != null) { 
       if((line = inputStream.readLine()) != null) { 
        System.out.println(line); 
        i++; 
       } // end if 
      } //end while 
      System.out.println(); // create space 
      inputStream.close(); 
     } // end try 
     catch(FileNotFoundException e) { 
      System.out.println("File was not found"); 
      System.out.println("or could not be opened"); 
     } //end catch 
     catch(IOException e) { 
      System.out.println("Error reading from file"); 
     } //end catch 
    } // end main 
} // end class 
+0

是否打印過我的值並查看其指向的內容? – Kakarot

+0

你能否也不是你想要在代碼中實現什麼? – Kakarot

+0

只是FYI - 你錯過了第二個'try'。 – admdrew

回答

0
for(int i=0; i < systemInfo.size; ++i) 
{ 
    // the code you want to repeat with i varying each time 
} 

int i=0; 
while(i < systemInfo.size) 
{ 
    // the code you want to repeat with i varying each time 
    ++i; 
} 

int i=0; 
while(i++ < systemInfo.size) 
{ 
    // the code you want to repeat with i varying each time 
} 

int i=0; 
do 
{ 
    // the code you want to repeat with i varying each time 
} 
while(++i < systemInfo.size) 

或...