对于更有经验的程序员来说,这应该是一个相对快速的解决方案……我只是想输入一个文本文件,并在不以“>”或“+”开头的每一行前面放置一个“#”。问题是我遇到了一个异常, .charAt(0) 超出了字符串的范围。但是在控制台执行该行之前,应该从文本文件中读取字符串——我错过了什么?我是否需要插入一行内容以使前面声明的 lineFromFile String 不为空?我不知道该怎么办...
导入java.io.*;
公共类 FirstTry {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
// Declare variables corresponding to names of i/o files
String oldFilename = "";
String newFilename = "";
// Console input of i/o filenames
BufferedReader cin;
cin = new BufferedReader(new InputStreamReader(System.in));
System.out.println("What is the input text? Include path and .txt extension) ");
oldFilename = cin.readLine();
System.out.print(oldFilename);
System.out.println("What is the output text? Include path and .txt extension) ");
newFilename = cin.readLine();
System.out.print(newFilename);
cin.close();
// Read text from old file,
BufferedReader fin;
fin = new BufferedReader(new FileReader(oldFilename));
while (fin.ready())
{
String lineFromFile;
lineFromFile = fin.readLine();
System.out.println(lineFromFile);
if (lineFromFile.charAt(0) != '>' || lineFromFile.charAt(0) != '+')
{
PrintWriter fout;
fout = new PrintWriter(new FileWriter(newFilename));
fout.println('#' + lineFromFile);
fout.close();
}
} // while
fin.close();
System.out.print("Text processing complete.");
}//main
}//公开课