日期:2014-05-20  浏览次数:20687 次

新手求助!那位大神路过麻烦踩一下~
下面是我在一本书上看到的一段代码。我把它写进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
我是一个机器人,我来自计算机的世界。

------解决方案--------------------
我把它写进eclipse之后,不能运行,但是要是直接用jdk又可以显示?

不是很理解其中的意思
------解决方案--------------------
直接用jdk的意思我想就是通过javac/java去运行

lz的问题应该是位置放错了,txt放在你项目的根目录应该就行了

------解决方案--------------------
用jdk把程序打包运行