将文本文件读取到树中 [英] Reading a text file to a tree

查看:135
本文介绍了将文本文件读取到树中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个测试类和一个二叉搜索树来解析文本文件并将其添加到搜索树中。但是我在阅读文本文件时遇到以下错误:

线程main中的异常java.io.FileNotFoundException:PoblachtNaHEireann.txt(系统找不到指定的文件)
at java.io.FileInputStream.open(Native Method)$ b java.io.FileInputStream上的$ b。< init > (未知来源)
at java.io.FileInputStream。< init > (未知来源)
在TestRunner.main(TestRunner.java:20)





这是我输入的代码片段:

 BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(PoblachtNaHEireann.txt)) ); 

尝试
{
扫描仪输入=新扫描仪(PoblachtNaHEireann.txt);

processLineByLine(输入);

while(input.hasNext()){
String nextToken = input.next();
if(!nextToken.isEmpty())
wordList.add(nextToken);

//或逐行处理
String nextLine = input.nextLine();
}
input.close();
BinarySearchTree bst = new BinarySearchTree(3);
FileReader input1 = new FileReader(PoblachtNaHEireann.txt);
BufferedReader bufRead = new BufferedReader(input1);
String myLine = null;
ArrayList< string> array = new ArrayList< string>();
int i = 0;

while((myLine = bufRead.readLine())!= null)
{
String [] tmpArray = myLine.split();
for(int j = 0; j< tmparray.length;> {
array.add(tmpArray [j] .toUpperCase());
bst.insertCharacters(tmpArray [j] .toUpperCase());
}
i ++;< / string>< / string>

解决方案

好的,我已经在eclipse中使用 bin 上面的目录中的文本文件运行了它,并且它运行正常。我注意到上面的代码在设置<$ c时遇到问题$ c>扫描仪对象。

  //  你有这个,它扫描字符串PoblachtNaHEireann.txt 
扫描仪输入= 扫描仪( PoblachtNaHEireann.txt);

/ / 它应该如下,读取文件
扫描仪输入= new 扫描仪( new 文件( PoblachtNaHEireann.txt) );


Im have created a test class and a binary search tree to parse a text file and add it to a search tree.But I''m getting the following errors when reading the text file:

Exception in thread "main" java.io.FileNotFoundException: PoblachtNaHEireann.txt (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(Unknown Source)
    at java.io.FileInputStream.<init>(Unknown Source)
    at TestRunner.main(TestRunner.java:20)



This is my input code snippet:

BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("PoblachtNaHEireann.txt")));
		
                try 
		{
			Scanner input = new Scanner("PoblachtNaHEireann.txt");
			
			processLineByLine(input);
			
			while(input.hasNext()) {
			    String nextToken = input.next();
			    if(!nextToken.isEmpty())
			    	wordList.add(nextToken);
			    	
			    //or to process line by line
			    String nextLine = input.nextLine();
			}
			input.close();
			BinarySearchTree bst = new BinarySearchTree(3);
			FileReader input1 = new FileReader("PoblachtNaHEireann.txt");
			BufferedReader bufRead = new BufferedReader(input1);
			String myLine = null;
			ArrayList<string> array = new ArrayList<string>();
			int i = 0;
			
			while ( (myLine = bufRead.readLine()) != null)
			{    
				String[] tmpArray = myLine.split(" ");
				for(int j = 0; j<tmparray.length;>				{
					array.add(tmpArray[j].toUpperCase());
					bst.insertCharacters(tmpArray[j].toUpperCase());
				}
			    i++;</string></string>

解决方案

OK, I have run this in eclipse with the text file in the directory one above bin, and it works fine. I did notice that your code above has a problem in your setup of your Scanner object.

// you have this, which scans the string "PoblachtNaHEireann.txt"
Scanner input = new Scanner("PoblachtNaHEireann.txt");

// it should be as follows, to read a file
Scanner input = new Scanner(new File("PoblachtNaHEireann.txt"));


这篇关于将文本文件读取到树中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆