java将文字信息追加到指定txt文件
java新手。制作了一个简易的人名生成器,但是生成的名称我只会将其输出到控制台,不会将其输出信息追加到某个指定txt文件中(注意是追加而不是覆盖),请问该怎么弄才能完成。...
java新手。制作了一个简易的人名生成器,但是生成的名称我只会将其输出到控制台,不会将其输出信息追加到某个指定txt文件中(注意是追加而不是覆盖),请问该怎么弄才能完成。import java.util.Random;class ChineseName { String[] FName = new String[4]; String[] LName = new String[4]; public ChineseName() { FName[0] = "艾|ai"; FName[1] = "白|bai"; FName[2] = "蔡|cai"; FName[3] = "曹|cao"; LName[0] = "ai|皑|艾|哀|埃"; LName[1] = "an|安|黯|谙|暗|岸"; LName[2] = "ao|奥|傲|敖|骜|遨|翱"; LName[3] = "ang|昂|盎"; } public static void main(String[] args) { ChineseName chineseName = new ChineseName(); int j = 0; for (int i = 0; i < 100; i++) { if (j == 10) { j = 0; System.out.println(); } j++; System.out.print(chineseName.getFName() + chineseName.getLName() + chineseName.getLName() + " "); } } public String getFName() { Random random = new Random(); int Z = random.nextInt(4); String FN = FName[Z].split("[|]")[0]; return FN; } public String getLName() { Random random = new Random(); int Z = random.nextInt(4) ; int z = random.nextInt(LName[Z].split("\\|").length - 1) + 1; String LN = LName[Z].split("[|]")[z]; return LN; }}
展开
1个回答
展开全部
代码如下:
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Random;
public class ChineseName {
String[] FName = new String[4];
String[] LName = new String[4];
public ChineseName() {
FName[0] = "艾|ai";
FName[1] = "白|bai";
FName[2] = "蔡|cai";
FName[3] = "曹|cao";
LName[0] = "ai|皑|艾|哀|埃";
LName[1] = "an|安|黯|谙|暗|岸";
LName[2] = "ao|奥|傲|敖|骜|遨|翱";
LName[3] = "ang|昂|盎";
}
public static void main(String[] args) throws IOException {
FileOutputStream outputStream = new FileOutputStream("d:/test.txt", true);
OutputStreamWriter streamWriter = new OutputStreamWriter(outputStream);
ChineseName chineseName = new ChineseName();
int j = 0;
for (int i = 0; i < 100; i++) {
if (j == 10) {
j = 0;
System.out.println();
streamWriter.append(System.lineSeparator());
}
j++;
String name = chineseName.getFName() + chineseName.getLName() + chineseName.getLName() + " ";
System.out.print(name);
streamWriter.append(name);
}
streamWriter.append(System.lineSeparator());
streamWriter.flush();
streamWriter.close();
outputStream.close();
}
public String getFName() {
Random random = new Random();
int Z = random.nextInt(4);
String FN = FName[Z].split("[|]")[0];
return FN;
}
public String getLName() {
Random random = new Random();
int Z = random.nextInt(4);
int z = random.nextInt(LName[Z].split("\\|").length - 1) + 1;
String LN = LName[Z].split("[|]")[z];
return LN;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询