假设我有两个不同的文件;一个包含一堆内部有数组的方法,另一个包含将一些数据保存到 .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);
}
}
这段代码想要保存数组值。
我只是对如何将一个程序刚刚确定的值传递给另一个程序有点困惑。
最佳答案
在您的saveArray类中,您应该调用您在loadsaMethods类中创建的方法。
尝试:
loadsaMethods data = new loadsaMethods();
int[] scorep1 = data.myArray();
关于java - 将信息从一个文件传递到另一个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34118910/