📄 writecharacters.java
字号:
// 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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -