将此自定义readDataFile函数拉入Eclipse以将.dat文件数据打印到控制台 [英] Pulling this custom readDataFile function into Eclipse to print .dat file data to console

查看:143
本文介绍了将此自定义readDataFile函数拉入Eclipse以将.dat文件数据打印到控制台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标:从.dat文件中获取数据并将其打印到Eclipse中的控制台中

Goal: Get the data from a .dat file and print it to the console in Eclipse

资源 >: fpfret.java PointF.java dicolour.dat

我已经解决了所有问题,并且只有一些控制台错误,这是我的代码和问题是:如何添加 getCodeBase() 方法?

I have resolved all my issues and have just a few console errors, here's my code and my question is: How do I add the getCodeBase() method?

package frp3;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.EOFException;
import java.net.URL;
import java.util.Vector;

public class FileRead {
  public static void main(String[] args) { //getDocumentBase
    System.out.println(readDataFile(getCodeBase() + "dichromatic.dat", 300, 750));
  }
  private static String getCodeBase() {
// TODO Auto-generated method stub
return null;
  }
@SuppressWarnings("unchecked")
private static PointF[] readDataFile(String filename, int min, int max) {
    @SuppressWarnings("rawtypes")
    Vector v = new Vector();
  try {
        DataInputStream dis = new DataInputStream(new BufferedInputStream((new URL(filename)).openStream()));
    float f0, f1;
    while (true) {
          try {
            f0 = dis.readFloat();
            f1 = dis.readFloat();

            if (min < 0 || max < 0 || (f0 >= min && f0 <= max)) {
              v.addElement(new PointF(f0, f1));
            }
           }
           catch (EOFException eof) {
             break;
           }
        }
    }
    catch (Exception e) {
    e.printStackTrace();
    }
    PointF[] array = new PointF[v.size()];
    for (int i = 0; i < v.size(); i++) {
      array[i] = (PointF) v.elementAt(i);
    }

 return array; 
 }
}

这是我的控制台错误:

java.net.MalformedURLException: no protocol: nulldichromatic.dat
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at frp3.FileRead.readDataFile(FileRead.java:27)
at frp3.FileRead.main(FileRead.java:12)
[Lfrp3.PointF;@29be513c

这是我在Eclipse中的项目视图:

Here's my Project View in Eclipse:

推荐答案

好的。实际上,这比我最初通过时想的要复杂。基本上,readDataFile希望dichromous.dat文件是Internet上可用的资源。在readDataFile中查看以下行:

Alright. This is actually more complex then I thought at first pass. Basically, readDataFile expects the dichromatic.dat file to be a resource available on the Internet. Look at the following line from readDataFile:

DataInputStream dis = new DataInputStream(new BufferedInputStream((new URL(filename)).openStream()));

基本上,无论传入的文件名都用作URL。对于您的用例,您的文件托管在本地文件系统上,我建议进行一些更改。

Basically, whatever filename gets passed in, is used as a URL. For your use-case, where your file is hosted on your local filesystem, I recommend a few changes.

首先,将上述DataInputStream声明行替换为:

First, replace the above DataInputStream declaration line with:

DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream(filename)));

第二,将getCodeBase替换为:

Second, replace getCodeBase with:

private static String getCodeBase() {
    return "";
}

我只是用空字符串替换了null。由于 dicolour.dat位于项目的根目录中,因此使用空字符串(表示项目根目录)作为getCodeBase()的结果就足够了,因为该函数的结果会被预先添加为 dichromous。 dat,然后以文件名的形式传递给readDataFile。

I've simply replace null with an empty string. Since "dichromatic.dat" is in the root of your project, it should be sufficient to use an empty string, indicating project root, as the result for getCodeBase(), as the result of that function gets pre-pended to "dichromatic.dat" before being passed to readDataFile as filename.

如果将dichromate.dat放在其他位置,修改该空字符串作为通向文件的路径。

If you put dichromatic.dat in a different place, just modify that empty string to be the "path" that leads to the file.

希望这会有所帮助。

忘了提及-尽管Eclipse应该为您妥善处理此问题,但一定要更新导入列表以包括 import java.io.FileInputStream

Forgot to mention -- be sure to update your imports list to include import java.io.FileInputStream -- although Eclipse should handle this gracefully for you.

这篇关于将此自定义readDataFile函数拉入Eclipse以将.dat文件数据打印到控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆