writecharacters.java

来自「Java Classic Examples是我买的两本书:《JAVA经典实例》和」· Java 代码 · 共 45 行

JAVA
45
字号
// Appending data to a file
import java.io.*;

class WriteCharacters
{
  public static void main(String[] args)
  {
    try
    {
      String dirName = "c:\\JunkData\\"; // Directory for the output file
      String fileName = "Proverbs.txt";  // Name of the output file 
      File output = new File(dirName, fileName);
      output.createNewFile();            // Create a new file if necessary

      if(!output.isFile())               // Verify we have a file
      {
        System.out.println("Creating " + output.getPath() + " failed.");
        return;
      }

      BufferedWriter out = new BufferedWriter(
                                new FileWriter(output.getPath(), true));

      String[] sayings = { "Indecision maximixes flexibility.",
                           "Only the mediocre are always at their best.",
                           "A little knowledge is a dangerous thing.",
                           "Many a mickle makes a muckle.",
                           "Who begins too much achieves little.",
                           "Who knows most says least.",
                           "A wise man sits on the hole in his carpet."};

      // Write the proverbs to the file preceded by the string length
      for(int i = 0; i < sayings.length; i++)
      {
        out.write(sayings[i].length() + sayings[i]);
      }
      out.close();
    }
    catch(IOException e)
    {
      System.out.println("Error writing the file " + e);
    }
  }
}

⌨️ 快捷键说明

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