⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testprintwriters.java

📁 此源码为机械工业出版社出版的《Java语言程序设计》第三版所配套的书中所有源代码。
💻 JAVA
字号:
// TestPrintWriters.java: Create a text file using PrintWriter
import java.io.*;

public class TestPrintWriters
{
  // Main method: args[0] is the output file
  public static void main(String[] args)
  {
    // Declare print stream
    PrintWriter pw = null;

    // Check usage
    if (args.length != 1)
    {
      System.out.println("Usage: java TestPrintWriters file");
      System.exit(0);
    }

    File tempFile = new File(args[0]);

    if (tempFile.exists())
    {
      System.out.println("The file " + args[0] +
        " already exists, delete it, rerun the program");
      System.exit(0);
    }

    // Write data
    try
    {
      // Create data output stream for tempFile
      pw = new PrintWriter(new FileOutputStream(tempFile), true);
      for (int i=0; i<10; i++)
        pw.print(" "+(int)(Math.random()*1000));
    }
    catch (IOException ex)
    {
      System.out.println(ex.getMessage());
    }
    finally
    {
      // Close files
      if (pw != null) pw.close();
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -