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

标签 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);

   }
}

这段代码想要保存数组值。

我只是对如何将一个程序刚刚确定的值传递给另一个程序有点困惑。

最佳答案

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

尝试:

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

关于java - 将信息从一个文件传递到另一个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34118910/

相关文章:

c++ - 使用 UNC 路径读取/写入文件 - 在 C++ 中

java - dis.readchar 中文字母 - 错误解释的字符!

java - Scala 和 util.Iterator[_]

java - <fmt :formatNumber in jSTL need to $ symbol

java - Java 中添加到文件夹的文件的 FIFO 处理

javascript - 应用程序重新启动后追加到文件 - Phonegap

具有自引用类型的泛型类型的 Java 子类化问题

java - 在 jMeter 中创建 Java 请求以验证多个变量

JAVA循环问题

c - 有没有一种简单的方法可以从文件中读取未经验证的十进制值?