我需要存储粗体,斜体字以便进一步处理。以下是程序:
package JavaApplication14;
import java.io.*;
public class file_handling {
public static void main(String [] args) {
String s="a.txt";
// The name of the file to open.
String fileName = "C:\\Users\\ADMIN\\Desktop\\"+s;
`
` // This will reference one line at a time
String line;
try {
// FileReader reads text files in the default encoding.
FileReader fileReader =
new FileReader(fileName);
// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader =
new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null) {
String words[]=line.split(" ");
System.out.println(line);
int i=0;
for(i=0;i<words.length;i++){
{
System.out.println(words[i]);
};
}
}
// Always close files.
bufferedReader.close();
}
catch(FileNotFoundException ex) {
System.out.println(
"Unable to open file '" +
fileName + "'");
}
catch(IOException ex) {
System.out.println(
"Error reading file '"
+ fileName + "'");
// Or we could just do this:
// ex.printStackTrace();
}
}
}
我需要检查words []数组中的单词是粗体还是斜体。如果代码似乎不正确,还有其他方法可以执行此程序吗?
答案 0 :(得分:1)
* .txt文件中没有粗体或斜体字符,正如mk所说。
此外,当您执行for循环时,应避免在开始循环之前初始化int i。
而不是:
int i = 0
for (i = 0; i<words.length; i++) { ...
你应该这样做:
for (int i = 0; i<words.length; i++) { ...
这样你就可以在其他循环中重用变量i。