我正在处理一个请求文件名的作业,然后将文件输出到带有编号行格式的.txt文件中,如:
[001]
[002]
我将代码拼凑在一起以使程序正常工作,但我似乎无法以请求的格式编写代码。任何帮助将不胜感激。
到目前为止,这是我的代码。
try {
System.out.println("Enter your source code file name: ");
Scanner scanner = new Scanner(System.in);
String file = scanner.nextLine();
in = new FileInputStream(file);
out = new FileOutputStream(file + ".txt");
int c;
while ((c = in.read()) != -1) {
out.write(c);
}
} catch (IOException e) {
System.out.println("Error!");
}
答案 0 :(得分:2)
您只需要一个计数器来计算行数:
BufferedReader in = new BufferedReader(new FileReader(fileName));
BufferedWriter out = new BufferedWriter(new FileWriter(fileName + '.txt');
String line;
while ((line = in.read()) != -1) {
counter++;
out.write(String.format(..., counter, line)+"\r\n");
}
有关String.format()
方法的其他信息,请查看此link
答案 1 :(得分:1)
尝试使用BufferedReader / BufferedWriter来更轻松地读取和写入文本行:
function onCancellationAgentsReceived(series) {
$("#agentcancellations").empty();
$.plot("#agentcancellations", series, {
bars: { show: true, barWidth: 0.7, align: "center" }, xaxis: {mode: "categories", tickLength: 0, position: "bottom"}});
}