2015-12-06 43 views
0

說我有兩個不同的文件;一個包含一組內部有數組的方法,另一個包含將某些數據保存到.txt文件的代碼。如何將信息(在本例中是數組)從第一個文件傳遞到第二個文件以便寫入?處理信息從一個文件到另一個

例如

public class loadsaMethods 
{ 
    public static void main(String[] param) 
    { 
     howFast(); //arbitrary methods 
     howSlow(); //that have some random uses 
     canYouGo(); // e.g this calculates the speed of something 
     myArray(); // this holds the data I want to write in the other file 
    } 
     /*assume code for other methods is here*/ 
    public static int[] myArray() 
    { 
     int[] scorep1 = new int[4]; 
     return new int[4]; // this array gets given values from one of the other methods 
    } 
} 

上面的代碼中有一個陣列,我想掐

public class saveArray 
{ 
    public static void main(String[] params) throws IOException 
    { 
     PrintWriter outputStream = new PrintWriter(new FileWriter("mydata2.txt")); 

     int NumberofNames = 3; 
     outputStream.println(NumberofNames); 

     String [] names = {"Paul", "Jo", "Mo"}; //this is the line that needs to contain the 
               //array but it doesn't know what values are 
               //stored in the array until the previous 
               //program has terminated 
     for (int i = 0; i < names.length; i++) 
     { 
      outputStream.println(names[i]); 
     } 

     outputStream.close(); 

     System.exit(0); 

    } 
} 

而這個代碼要保存的陣列值。

我只是有點困惑,如何通過一個程序剛剛確定的值,以不同的程序。

回答

1

在你saveArray類你應該叫你在loadsaMethods類創建的方法。

嘗試:

loadsaMethods data = new loadsaMethods(); 
int[] scorep1 = data.myArray(); 
+0

謝謝你這麼多,這完美!你是一個絕對的星形 – Overclock

+1

由於loadsaMethods.myArray是一個靜態方法,所以你不需要創建一個loadsamethods實例;只需使用loadsaMethods.myArray –

相關問題