gpt4 book ai didi

java - 搜索文件 : Beginner Code

转载 作者:太空宇宙 更新时间:2023-11-04 06:07:27 26 4
gpt4 key购买 nike

我在大学学习编程,我们的任务是创建一个程序,允许用户从计算机中打开文件并从所选文件中获取信息。我的作业的一部分内容如下:

逐行搜索文件中的给定字符串。输出必须包含行号,后跟包含搜索参数的行的内容。例如,给定以下搜索字符串:Java,程序将逐行搜索文件,生成如下结果:

5:在 java 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);
}

}

最佳答案

您将结果放入结果列表中,但从不打印它。

关于java - 搜索文件 : Beginner Code,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29127808/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com