我必须生成的是添加到每一行的txt中,例如0001,在下一行中添加0002等。如果代码在运行时检测到错误,程序a iit会显示消息例如数据没有“) “类似这样的事情。
public static void main(String[] args) {
FileReader fr = null;
FileWriter fw = null
try {
fr = new FileReader("C:\\Users\\etc");
fw = new FileWriter("C:\\Users\\etc\\example.txt");
int c = fr.read();
while(c!=-1) {
fw.write(c);
c = fr.read();
}
} catch(IOException e) {
} finally {
close(fr);
close(fw);
}
}
public static void close(Closeable stream) {
try {
if (stream != null) {
stream.close();
}
} catch(IOException e) {
//...
}
}
}
我需要在 .txt 文件的每一行生成从 0001 及以上的数字,如果代码在 .vb 分析期间遇到错误,则必须在 .txt 中显示该错误并显示一条消息。例如错误 01 代码没有 ;在这一行或类似的东西
我希望这有帮助
import java.io.*;
public class Main {
public static void main(String[] args) {
FileReader fr = null;
FileWriter fw = null;
BufferedReader br = null;
BufferedWriter bw = null;
try {
fr = new FileReader("C:\\Users\\etc\\example.vb");
fw = new FileWriter("C:\\Users\\etc\\example.txt");
br = new BufferedReader(fr);
bw = new BufferedWriter(fw);
String line;
int lineNumber = 1;
while ((line = br.readLine()) != null) {
// Check if the line contains an error
if (line.contains(")")) {
bw.write("Error " + String.format("%02d", lineNumber) + ": The code doesn't have ';' in this line.\n");
}
// Append line number and write the line to the file
bw.write(String.format("%04d", lineNumber++) + ": " + line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
close(br);
close(fr);
close(bw);
close(fw);
}
}
public static void close(Closeable stream) {
try {
if (stream != null) {
stream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
此代码从输入 .vb 文件中读取每一行,检查其是否包含 ) 字符,如果包含,则将错误消息写入输出 .txt 文件。它还以您指定的格式(例如“0001:”、“0002:”等)在输出文件中的每行之前添加行号。