将信息从一个文件传递到另一个文件

时间:2015-12-06 15:09:55

标签: java file-io

说我有两个不同的文件;一个包含一堆在其中包含数组的方法,另一个包含将一些数据保存到.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 个答案:

答案 0 :(得分:1)

saveArray类中,您应该调用您在 loadsaMethods类中创建的方法。

尝试:

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