JFileChooser和.txt文件解析问题
我在解决CCE时遇到问题。此分配要求用户在运行时打开.txt文件,然后让编译器 对特定正则表达式执行后台解析,以确定它们是否匹配或不匹配(例如,ACC或REJ),但捕获是该用户必须用JFileChooser打开该文件。我已经做了大量的阅读和 研究发布之前,问题似乎是 JFileChooser,据我了解使用java.io.File本身,似乎 只允许扫描仪解析输入的.txt文件在此场景。JFileChooser和.txt文件解析问题
http://docs.oracle.com/javase/1.5.0/docs/api/java/io/class-use/File.html
所以,当我单独使用扫描器读取文本的所有行的文件 在它的编译“,但所有的控制台输出表现为,例如,“空 空空空空” (例如,如果文件中有五行文字)。 为了方便起见,下面是.txt文件的一个例子,将被认为是可接受的 (或ACC,速记):EDIT这个例子中应具有的每个印刷在单独的行
S中的规则:AB 甲:0A 答:E B:1B B:è
我提供我的源代码。请注意,charSequences只是Scanner/BR必须解析的可行 正则表达式。此外,如果任何给定的行拒绝(REJ, 简写),我们需要提醒用户,然后立即终止。显然 我不明白这些类以及我以为我做了...也许我需要 使用类似FileReader的东西,我走的基础开始,但我只是 非常沮丧与Java IO的复杂性一般。这只是令人沮丧的 因为我把所有这些工作和努力,并很少有任何事情来显示它。
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
public class Simplified
{
public Simplified() throws Exception
{
readLines();
}
public void readLines() throws Exception
{
//MUST prompt user to use JFileChooser
JFileChooser ranFi = new JFileChooser();
//approve condition
if(ranFi.showOpenDialog(null)==JFileChooser.APPROVE_OPTION)
{
//get selected file
Comparable<File> file = ranFi.getSelectedFile();
//create a bufferedReader for the file
//BufferedReader br = new BufferedReader((Reader) file);
BufferedReader br = null;
//create local String object the refer to file's current line
String currentLine;
/**************************
* Length(LHS,RHS) = 1,1
* --CharSequence compilations
* ******************************
*/
//ACC
CharSequence fp100 = "[S][:][A-Ze0-1]";
//ACC iff is NOT the very first line of text file
CharSequence fp101 = "[A-Z&&[^S]][:][A-Ze0-1]";
/**************************
* Length(LHS,RHS) = 1,2
* --CharSequence compilations
* ******************************
*/
//ACC
CharSequence fp200 = "[S][:][A-Z0-1][A-Z0-1]";
//ACC iff is NOT the very first line of text file
CharSequence fp201 = "[A-Z&&[^S]][:][A-Z0-1][A-Z0-1]";
/**************************
* Length(LHS,RHS) = 1,3
* --CharSequence compilations
* ******************************
*/
//ACC
CharSequence fp300 = "[S][:][A-Z0-1][A-Z0-1][A-Z0-1]";
//ACC iff is NOT the very first line of text file
CharSequence fp301 = "[A-Z&&[^S]][:][A-Z0-1][A-Z0-1][A-Z0-1]";
/**
* Length(LHS,RHS) = 1,4
* --CharSequence compilations
* ******************************
*/
//REJ
CharSequence fp400 = "[.][.][.][.][.][.]";
//create a Scanner for the file
Scanner text = new Scanner((Readable) file);
try
{
br = new BufferedReader((Reader) file);
//while there are still lines to be read in the file
//where currentLine = present line of the bufferedReader
while((currentLine = br.readLine()) != null)
{
//while the scanner still has lines to read in the file
while(text.hasNext())
{
//to remove trailing whitespace in the file, starting
//@ the first line in the text file...
String trimStartLine = currentLine.trim();
//ALL feasible ACC permutations for starting line
if(trimStartLine.contains(fp100)||trimStartLine.contains(fp200)||trimStartLine.contains(fp300))
{
System.out.println("first line valid...");
}
//ALL feasible REJ permutations for starting line
else if(!trimStartLine.contains(fp100)||!trimStartLine.contains(fp200)||!trimStartLine.contains(fp300))
{
System.out.println("invalid first line..." +
"...terminating");
System.exit(0);
}
//once again removing trailing whitespace in the file,
//this time trimming the whitespace in the second line
//of the file, provided that iff the first line of the
//file was valid to begin with...
String trim2ndLine = currentLine.trim();
//permutations have now increased to 6 since "[S]" isn't
//technically required for ANY line other than the first line
if(trim2ndLine.contains(fp100)||trim2ndLine.contains(fp101)||trim2ndLine.contains(fp200)||
trim2ndLine.contains(fp201)||trim2ndLine.contains(fp300)||trim2ndLine.contains(fp301))
{
System.out.println("2nd line valid...");
//only included two line checks for now...
//as of now if the first two lines are valid
//then print out the remaining lines in the
//file...
System.out.println(text.nextLine());
}
else if(!trim2ndLine.contains(fp100)||!trim2ndLine.contains(fp101)||!trim2ndLine.contains(fp200)||
!trim2ndLine.contains(fp201)||!trim2ndLine.contains(fp300)||!trim2ndLine.contains(fp301))
{
System.out.println("invalid 2nd line..." +
"...terminating");
System.exit(0);
}
else
{
System.out.println("no file was selected");
}
}//end of inner while
}//end of outer while
}//end of try
catch (Exception ex)
{
System.out.println(ex.getMessage());
}
finally
{
try
{ //close text while lines remain,
//valid or not...
if(br != null && text != null){
br.close();
text.close();
}
}
catch (Exception ex)
{
}
}//end of finally
System.out.println("end of file successfully reached...");
}//end of approve option
}//end of method
public static void main(String[] args) throws Exception
{
Simplified mn = new Simplified();
mn.readLines();
}//end of main
}//end of Simplified.java
底线是,无论在源代码中也没有我的扫描仪和BufferedReader对象的类型转换的位置,我总是得到一个CCE。任何帮助将是巨大的。先谢谢你。
-
JFileChooser.getSelectedFile
返回File
,我不知道为什么你想将其包装在一个Comparable<File>
的?文件本身实现了Comparable
。 -
Scanner
将接受File
作为输入。尽管我对Scanner
API没有经验,但我认为您不需要BufferedReader
,只需创建Scanner
并使用File
即可,因为它的源代码应该足以让您运行。
他是一个快速测试,你可以尝试。
- 用你需要测试的内容创建一个简单的文本文件。
- 创建一个简单的
Class
,它只有一个public static void main(String args[]) {...}
方法。
在main
方法尝试类似...
Scanner scanner = new Scanner(new File("path/to/text/file/text.txt"));
while (scanner.hasNext()) {
System.out.println(scanner.next());
}
从那里开始添加在您的分析逻辑。
好酷的豆子我今天晚些时候会试试这个thx man – OcelotXL
你可以在这里粘贴你的堆栈跟踪 – Michael
@ Michael hello there。堆栈跟踪读取为:Thread [main](暂停(异常ClassCastException))Simplified.readLines()行:83简化。()line:21 Simplified.main(String [])line:179 –
OcelotXL