下面是我在一本书上看到的一段代码。我把它写进eclipse之后,不能运行,但是要是直接用jdk又可以显示。请问这是什么原因啊?在eclipse上的时候我直接我代码和txt文件放在同一个package里,是不是txt文件放的位置不对?到底要把txt放在哪里呢?
源文件1:Person.java
public class Person {
private String name;
private String memo;
private int age;

public int getAge(){
return age;
}
public void setAge(int age){
this.age = age;
}
public String getMemo(){
return memo;
}
public void setMemo(String memo){
this.memo = memo;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public String toString(){
return "the person 'information is:" + name +"\n" + memo + "\n" + age;
}
}
源文件2:ReadPersons.java
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Vector;public class ReadPersons {
public static Vector getInfo()
{
Vector v = new Vector();
try
{
File f = new File ("info.txt");
FileReader in = new FileReader(f);
BufferedReader br = new BufferedReader(in);
String s =br.readLine();
while(s != null)
{
Person p = new Person();
p.setName(s);
s = br.readLine();
p.setAge(Integer.parseInt(s));
s = br.readLine();
p.setMemo(s);

v.add(p);
s = br.readLine();
}
}catch(IOException e)
{
e.printStackTrace();
}
return v;  }
public static void main(String[] args) {
// TODO Auto-generated method stub
Vector v = ReadPersons.getInfo();
Person p0 = (Person)v.get(2);
System.out.println(p0);
}
}
文本文档:info.txt
rose
21
我是一个教师。
jenny
16
我是新东方的学生,学习感觉挺累,我希望成为程序员。
poly
18
it's just a joke.
robert
20
我是一个机器人,我来自计算机的世界。

解决方案 »

  1.   

    我把它写进eclipse之后,不能运行,但是要是直接用jdk又可以显示?不是很理解其中的意思
      

  2.   

    直接用jdk的意思我想就是通过javac/java去运行lz的问题应该是位置放错了,txt放在你项目的根目录应该就行了
      

  3.   

    我在eclipse中运行显示有错误,但是直接复制到文本文档用javac执行文件又可以运行
      

  4.   

    果然一下子搞定了~我之前把txt直接放在package里去了
      

  5.   

    谢谢各位热心前辈哈~
    我的这个问题搞定过了。
    现在才开始学java,许多东西都不懂,以后肯定经常上来求教