无法使用java applet读入文本文件

时间:2012-06-18 06:48:52

标签: java netbeans text-files java-io japplet

我在使用JApplet和Java Web Start时尝试读取文本文件时遇到问题,基本上我有一个文件,其中包含我想要阅读的用户数据。文本文件存储在网络服务器。这是代码:

company.readCompanyFile("file:///C:/Users/Chris/Desktop/ArFile Clients/" + companyName + "/"
                    + companyName + ".cmp");

和readCompanyFile函数是:

public void readCompanyFile(String cname)
{
    try
    {
        BufferedReader br = new BufferedReader(new FileReader(cname));

        name = br.readLine();
        quota = Float.parseFloat(br.readLine());

        String s;
        while ((s = br.readLine()) != null)
        {
            String[] parts = s.split(":");

            metadataFields.add(parts);
        }

        br.close();
    }
    catch(IOException e)
    {
        System.out.println("File does not exist, or has invalid format");
    }
}

尝试通过java web start启动时会抛出以下错误

BadFieldException[ The field <extension>href has an invalid value: C:\Users\Chris\Desktop\ArFile Clients,C:\Users\Chris\Desktop\ArFile Clients]
    at com.sun.javaws.jnl.XMLUtils.getAttributeURL(Unknown Source)
    at com.sun.javaws.jnl.XMLUtils.getRequiredURL(Unknown Source)
    at com.sun.javaws.jnl.XMLFormat.handleResourceElement(Unknown Source)
    at com.sun.javaws.jnl.XMLFormat.access$800(Unknown Source)
    at com.sun.javaws.jnl.XMLFormat$2$1.visitElement(Unknown Source)
    at com.sun.javaws.jnl.XMLUtils.visitChildrenElements(Unknown Source)
    at com.sun.javaws.jnl.XMLFormat$2.visitElement(Unknown Source)
    at com.sun.javaws.jnl.XMLUtils.visitElements(Unknown Source)
    at com.sun.javaws.jnl.XMLFormat.buildResourcesDesc(Unknown Source)
    at com.sun.javaws.jnl.XMLFormat.parse(Unknown Source)
    at com.sun.javaws.jnl.LaunchDescFactory.buildDescriptor(Unknown Source)
    at com.sun.javaws.jnl.LaunchDescFactory.buildDescriptor(Unknown Source)
    at com.sun.javaws.jnl.LaunchDescFactory.buildDescriptorFromCache(Unknown Source)
    at com.sun.javaws.jnl.LaunchDescFactory.buildDescriptorFromCache(Unknown Source)
    at sun.plugin2.applet.JNLP2Manager.initialize(Unknown Source)
    at sun.plugin2.main.client.PluginMain.initManager(Unknown Source)
    at sun.plugin2.main.client.PluginMain.access$200(Unknown Source)
    at sun.plugin2.main.client.PluginMain$2.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Error while initializing manager: BadFieldException[ The field <extension>href has an invalid value: C:\Users\Chris\Desktop\ArFile Clients,C:\Users\Chris\Desktop\ArFile Clients], bail out

1 个答案:

答案 0 :(得分:3)

  

文本文件存储在Web服务器上。

...而applet在客户端计算机上运行。怎么读取文件?

您必须通过网络访问它,例如通过HTTP(将其放在服务器上的小程序旁边)。

此外,该堆栈跟踪似乎与您的代码无关。看起来Webstart无法解析部署描述符(可能还因为引用服务器磁盘上的文件)。