我正在读大学,我们获得了一项创建程序的任务,该程序允许用户从他们的计算机打开文件并从所选文件中获取信息。我的任务的一部分陈述如下:
逐行搜索文件以获取给定字符串。输出必须包含行号,后跟包含搜索参数的行的内容。例如,给定以下搜索字符串:Java,程序将逐行搜索文件,生成如下结果:
5:在爪哇岛上 9:JAVA的人喜欢jaVa。
使用LineNumberReader类进行此练习。
我的代码如下,我不确定我做错了什么。没有语法错误,似乎只是一个逻辑错误。当我运行代码时,我能够获取文件描述,备份文件,获取字数,并正确退出,但当被要求搜索上面描述的单词时,我没有得到我应该得到的输出,它只给我字数,没有搜索结果。
主类
import java.io.*;
import java.util.ArrayList;
import javax.swing.*;
public class BasicFile {
File file1;
JFileChooser selection;
File file2 = new File(".", "Backup File");
public BasicFile() {
selection = new JFileChooser(".");
}
public void selectFile() {
int status = selection.showOpenDialog(null);
try {
if (status != JFileChooser.APPROVE_OPTION) {
throw new IOException();
}
file1 = selection.getSelectedFile();
if (!file1.exists()) {
throw new FileNotFoundException();
}
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(null, "File Not Found ", "Error", JOptionPane.INFORMATION_MESSAGE);
} catch (IOException e) {
System.exit(0);
}
}
void backupFile() throws FileNotFoundException {
DataInputStream in = null;
DataOutputStream out = null;
try {
in = new DataInputStream(new FileInputStream(file1));
out = new DataOutputStream(new FileOutputStream(file2));
try {
while (true) {
byte data = in.readByte();
out.writeByte(data);
}
} catch (EOFException e) {
JOptionPane.showMessageDialog(null, "Success!!!",
"Backup Complete!", JOptionPane.INFORMATION_MESSAGE);
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "File Not Found ",
"Error", JOptionPane.INFORMATION_MESSAGE);
}
} finally {
try {
in.close();
out.close();
} catch (Exception e) {
display(e.toString(), "Error");
}
}
}
boolean exists() {
return file1.exists();
}
public String toString() {
return file1.getName() + "\n" + file1.getAbsolutePath() + "\n" + file1.length() + " bytes";
}
public String words() {
try {
int words = 0;
int numbers = 0;
int lines = 1;
int characters = 0;
int total = 0;
String c = " ";
FileReader r = new FileReader(file1);
LineNumberReader lnr = new LineNumberReader(r);
StreamTokenizer t = new StreamTokenizer(r);
ArrayList<String> results = new ArrayList<String>();
t.resetSyntax();
t.wordChars('0', '9');
t.wordChars('A', 'Z');
t.wordChars('a', 'z');
t.whitespaceChars(0, ' ');
t.eolIsSignificant(true);
while (t.nextToken() != StreamTokenizer.TT_EOF) {
switch (t.ttype) {
case StreamTokenizer.TT_NUMBER:
numbers++;
break;
case StreamTokenizer.TT_WORD:
characters += t.sval.length();
words++;
break;
case StreamTokenizer.TT_EOL:
lines++;
break;
case StreamTokenizer.TT_EOF:
break;
default:
}
}
BufferedReader bf = new BufferedReader(new FileReader(file1));
BufferedWriter out = new BufferedWriter(new FileWriter("test.txt"));
BufferedWriter output = new BufferedWriter(new FileWriter("output.txt"));
int recCount = 0;
String record = null;
while ((record = bf.readLine()) != null) {
recCount++;
out.write(recCount + ": " + record);
out.newLine();
}
out.close();
String ask = "Enter Word";
String find = JOptionPane.showInputDialog(ask);
String word = find;
String line = null;
while ((line = lnr.readLine()) != null) {
if (line.indexOf(word) >= 0) {
results.add(lnr.getLineNumber() + line);
}
}
r.close();
total = numbers + words;
lnr.close();
return file1.getName() + " has " + lines + " lines, "
+ total + " words, "
+ characters + " characters. ";
} catch (IOException e) {
display(e.toString(), "Error");
}
return " ";
}
void display(String msg, String s) {
JOptionPane.showMessageDialog(null, msg, s, JOptionPane.ERROR_MESSAGE);
}
}
测试类
import java.io.*;
import javax.swing.*;
public class TestBasicFile {
public static void main(String[] args) throws FileNotFoundException, IOException {
boolean done = false;
String menu = "Enter option\n1. Open File\n2. Backup File\n3. "
+ "Word Count\n4. Exit";
while (!done) {
BasicFile f = new BasicFile();
String s = JOptionPane.showInputDialog(menu);
try {
int i = Integer.parseInt(s);
switch (i) {
case 1:
JOptionPane.showMessageDialog(null, "When the file is selected, the name, path, and size will be displayed",
"File Selection", JOptionPane.INFORMATION_MESSAGE);
f.selectFile();
if (f.exists()) {
displayInfo(f.toString(), "File");
} else {
f.selectFile();
}
break;
case 2:
f.selectFile();
if (f.exists()) {
displayInfo(f.toString(), "File");
} else {
f.selectFile();
}
f.backupFile();
break;
case 3:
f.selectFile();
if (f.exists()) {
displayInfo(f.words(), "Word Count");
} else {
f.selectFile();
}
break;
case 4:
done = true;
break;
default:
}
} catch (NumberFormatException e) {
System.exit(0);
} catch (NullPointerException e) {
System.exit(0);
}
}
}
static void displayInfo(String s, String info) {
JOptionPane.showMessageDialog(null, s, info, JOptionPane.INFORMATION_MESSAGE);
}
}
答案 0 :(得分:1)
您将结果放在结果列表中,但从不打印。