在Java构造函数中导入文本文件

时间:2013-02-15 22:07:23

标签: java

我不确定产生问题的地方。我有两个类,一个叫做testFriends类,另一个叫做GUIapp类。在每个类中,我的目标是导入文件以供使用。在testFriends类中,我在main方法中导入文件,它解析它并且一切正常。在我的GUIapp类中,我尝试做同样的事情,但在构造函数方法中导入它,它告诉我文件不存在。两个文件类都驻留在同一个src文件夹中,我正在使用:

BufferedReader in = new BufferedReader(new FileReader(inputFile));

其中inputFile只是一个字符串“friends.txt”。

以下是两个类:

//This class works

public class temp {

public static void main(String[] args) throws ParseException {
    try {
        System.out.println("hey");
        BufferedReader in = new BufferedReader(new FileReader("friends.txt"));  //create string buffer for reading
        String line = in.readLine(); //reading first line
        System.out.println(line);
    } catch (IOException e) {
        System.out.println("Fail Import.");
    }

}

}


//////////The one below doesn't...

public class GUIapp extends JApplet{
//** PANEL **//
private JPanel outerPanel;

//** Button **//
private JButton button1;


/*
 * Constructor for Getting all the friends set up
 */
public GUIapp() throws ParseException, FileNotFoundException{
    BufferedReader in = new BufferedReader(new FileReader("friends.txt"));//Error Line

}

/*
 * Create Stuff
 */
public void createStuff() {
    outerPanel = new JPanel(); //create outer panel
    button1 = new JButton("Click Me");
    outerPanel.add(button1,BorderLayout.SOUTH);
}


/*
 * Initialize Stuff
 */
public void init(){
    createStuff(); //initialize create stuff
    this.add (outerPanel); 
}


}

任何想法为什么当两者都在同一目录中工作时,一个人可以阅读而另一个人不能阅读?

谢谢,

编辑:下面是我运行GUIapp类时抛出的异常:

java.io.FileNotFoundException: friends.txt (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:120)
at java.io.FileInputStream.<init>(FileInputStream.java:79)
at java.io.FileReader.<init>(FileReader.java:41)
at GUIapp.<init>(GUIapp.java:50)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at java.lang.Class.newInstance0(Class.java:355)
at java.lang.Class.newInstance(Class.java:308)
at sun.applet.AppletPanel.createApplet(AppletPanel.java:807)
at sun.applet.AppletPanel.runLoader(AppletPanel.java:714)
at sun.applet.AppletPanel.run(AppletPanel.java:368)
at java.lang.Thread.run(Thread.java:680)

1 个答案:

答案 0 :(得分:1)

在运行时,您的applet没有您正在寻找的文件。确保文件在运行时存在。

请将此方法添加到您的代码中,以便您可以获取文件

public void readFile(String fileToRead){
  String line;
  URL url = null;
  try{
  url = new URL(getCodeBase(), fileToRead);
  }
  catch(MalformedURLException e){}

  try{
  InputStream in = url.openStream();
  BufferedReader bf = new BufferedReader(new InputStreamReader(in));

  // your business logic here
  }
  catch(IOException e){
  e.printStackTrace();
  }
 }

/ *  *建立所有朋友的构造函数  * /

public GUIapp() throws ParseException, FileNotFoundException{
    readFile("friends.txt")
}

有关详细信息,请查看来自Javase的此图像 enter image description here

要创建使用imgDir下的a.gif图像文件的Image对象,applet可以使用以下代码:

Image image = getImage(getCodeBase(), "imgDir/a.gif");