我正在尝试读取一个txt文件并尝试将其保存在一个数组中。

以下是txt文件的格式:

A B 5结果

A C 2结果

A D 4结果

.....

public class search {

public static void main(String[] args) throws ParseException {

try {

Scanner user_input = new Scanner(System.in); // for user input

System.out.println("Enter the file name: ");

String filename1 = user_input.next();

File file = new File(filename1);

search bd = new search();

Node[] nodes;

nodes = bd.getNodes(file);

bd.printNodes(nodes);

}

catch(Exception e) {

System.out.println("Error reading file " + e.getMessage());

}

}

public Node[] getNodes(File file) throws IOException {

FileReader bd = new FileReader(file);

BufferedReader bufferReader = new BufferedReader(bd);

String line;

ArrayListlist = new ArrayList();

while ((line = bufferReader.readLine()) != null) {

String[] token = line.split(" "); // create string of tokens

list.add(new Node(token[0], token[1], Integer.parseInt(token[2])));

}

bufferReader.close();

return list.toArray(new Node[list.size()]); // converting list to array

}

public void printNodes(Node[] nodes) {

System.out.println("======================");

for(Node node : nodes) {

System.out.println(node);

}

System.out.println("======================");

}以下是我的Node类

class Node {

String leftchild;

String rightchild;

int cost;

public Node(){

}

public Node(String firstchild, String secondchild, int cost){

this.leftchild = firstchild;

this.rightchild = secondchild;

this.cost = cost;

}

public Node(String firstchild, String secondchild) {

this.leftchild = firstchild;

this.rightchild = secondchild;

}

public ArrayList getChildren(){

ArrayList childNodes = new ArrayList();

if(this.leftchild != null)

{

childNodes.add(leftchild);

}

if(this.rightchild != null) {

childNodes.add(rightchild);

}

return childNodes;

}

public boolean removeChild(Node n){

return false;

}

@Override

public String toString() {

return leftchild +" "+ rightchild;

}

}我没有编译问题,但是当我运行我的代码时,它给了我错误

error reading file 1

不知道为什么。我试图用很多方式改变我的代码,但没有一个能够工作。任何人都可以请为我解决这个问题吗?

谢谢

java 写文件 错误码_JAVA-读取文件错误1(错误)相关推荐

  1. java 看书浏览器官_JAVA读取文件流,设置浏览器下载或直接预览操作

    最近项目需要在浏览器中通过url预览图片.但发现浏览器始终默认下载,而不是预览.研究了一下,发现了问题: // 设置response的header,注意这句,如果开启,默认浏览器会进行下载操作,如果注 ...

  2. java遍历文件和归类_java读取文件的两种方法:java.io和java.lang.ClassLoader

    java读取文件的两种方法:java.io和java.lang.ClassLoader 什么时候使用java.io,什么时候使用java.lang.ClassLoader呢? (注:要是之前读xml文 ...

  3. java读取文件到字符串_Java读取文件到字符串

    java读取文件到字符串 Sometimes while working with files, we need to read the file to String in Java. Today w ...

  4. java 监听写文件的进度_java读取文件显示进度条的实现方法

    实现这个功能比较简单,用到的类有两个:ProgressMonitorInputStream(主要是整个类) 和 ProgressMonitor ,它们在javax.swing中 大体思路,你要首先知道 ...

  5. java写到txt乱码_java读取txt文本发生乱码的解决方法

    java读取txt文本发生乱码的解决方法 发布时间:2020-06-25 17:35:58 来源:亿速云 阅读:90 作者:Leah 本篇文章为大家展示了java读取txt文本发生乱码的解决方法,代码 ...

  6. java读取空格_java 读取文件路径空格和中文的处理

    应用部署时,发生文件读取错误,发现是部署路径中含有空格的文件夹名,然后把应用服务器位置迁移了. 从网上找到如下方案: 1, TestURL().class.getResource("&quo ...

  7. java字节流读取文件_字节流读取文件 java的几种IO流读取文件方式

    java字节流怎么读取数据 字节流读取数据例子如下: import java.io.File;import java.io.FileInputStream;import java.io.FileNot ...

  8. java io 文件路径_如何从Java项目中的相对路径读取文件? java.io.File找不到指定的路径...

    如何从Java项目中的相对路径读取文件? java.io.File找不到指定的路径 我有一个包含2个包的项目: ListStopWords.txt ListStopWords.txt 在包(2)中我有 ...

  9. python打开一个文件-python,一读取文件open()

    在实际操作中,我们经常会读取文件,这个时候python为我们提供了一个open()的方法,供我们读取文件,通过help(open),我们可以获取open的方法 f.close()关闭读取 f.read ...

最新文章

  1. [恢]hdu 2014
  2. 大型商业银行数据库连接池选型策略
  3. VMware vSphere 虚拟化简介
  4. 每日一皮:总觉得这个咖啡杯有点奇怪...
  5. python恶搞表情包-Python自动生成表情包,python在手,从此斗图无敌手
  6. ASP.NET MVC4 传递Model到View
  7. 1.11 抽象(abstract)类
  8. Modbus名词解释
  9. catia齿轮宏程序_Catia宏程序
  10. macbook pro python开发_Mac打造python2 python3开发环境
  11. 坏掉的项链Broken Necklace
  12. OAuth2.0 授权码认证方式使用流程
  13. Volatile(理解)
  14. 熄风的止颤的汤治疗帕金森的优势
  15. Mysql 免安装配置
  16. 如何压缩照片大小,压缩照片大小的方法
  17. 百度有啊 真的还有啊
  18. python并行编程 - GPU篇
  19. 蚂蚁金服区块链切入新场景,支付宝首创公积金黑名单及缴存证明
  20. 2018年浙江省网络安全技能竞赛ctf部分解题思路writeup

热门文章

  1. 数据结构基础-Hash Table详解
  2. 垃圾邮件过滤 php,垃圾邮件过滤功能
  3. 现代神经网络要这么用才能创造智能
  4. 使用masonry布局旋转View的问题
  5. jQuery用于请求服务器的函数
  6. 如何看待2016年安防市场?
  7. Java8读文件仅需一行代码
  8. 继承与 Data Member(2)
  9. 老广: KVM虚拟化学习笔记
  10. STL中用erase()方法遍历删除元素