如何将Vector的内容转储到新文件中?

时间:2014-12-11 18:56:01

标签: java linux

这是我计算92之前的素数的程序。它就像一个魅力。我希望将输出放入一个名为VectorDump的新文件中,而不是只显示在常规屏幕上。我在哪里开始这个程序?下面是我的代码,我的代码输出位于页面底部。

  • 这是我得到的输出

92之前的素数是:

2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89

import java.util.*;

public class PrimeByVector
{
        public static void main(String[] args)
        {
                final int NUMBER_PER_LINE = 10;
                int count = 0;
//              java.util.Vector vector = new java.util.Vector();
                Vector vector = new Vector();
                System.out.println("The primes before 92 are: \n");

                for (int n = 2; n < 92; n++)

                  PrintWriter writer = null;
                        try{
                                writer = new PrintWriter("VectorDump", "UTF-8");
                     } catch (Exception e) {
                      break;
}
                        Iterator it = vector.iterator();
                      while(it.hasNext())
                {
                        writer.print(
                     it.next() + " "
                );
                }
                writer.close();
{
                        boolean isPrime = true;
                        for (int i = 0; i < vector.size(); i++)
                        {
                                int primeNumber =
                                        ((Integer)(vector.elementAt(i))).intValue();
                                if (primeNumber > Math.sqrt(n)) break;

                                if (n % primeNumber == 0)
                                {
                                        isPrime = false;
                                        break;
                                }
                        }
if (isPrime)
                        {
                                count ++;
                                vector.addElement(new Integer(n));
                                if (count % NUMBER_PER_LINE == 0)
                                {
                                        System.out.println(n);
                                }
                                else
                                        System.out.print(n + " ");

                        }
                }
        }
}



PrimeByVector.java:15: error: not a statement
                  PrintWriter writer = null;
                  ^
PrimeByVector.java:15: error: ';' expected
                  PrintWriter writer = null;
                             ^
2 errors

2 个答案:

答案 0 :(得分:1)

而不是将其打印到System.out,而不是使用FileOutputStream,而且一切都应该有用。

不要忘记在输出流上调用close()

答案 1 :(得分:1)

在你的第一个for(不是为了身体!!)循环之后立即:

          PrintWriter writer = null;
          try {
              writer = new PrintWriter("VectorDump", "UTF-8");
          } catch (Exception e) {
              break;
          }

            Iterator it = vector.iterator();

          while(it.hasNext())
          {
           writer.print(
                     it.next() + " "
              );
          }

          writer.close();