Eclipse中的相​​对文件路径如何工作? [英] How do relative file paths work in Eclipse?

查看:119
本文介绍了Eclipse中的相​​对文件路径如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我2009年的新年决议是学习Java。我最近收购了Java for Dummies,并通过使用Eclipse重新编写本书中的演示代码。无论如何,使用相对路径的书中的每个示例似乎都没有读取它应该读取的.txt文件。



以下是示例代码: p>

  import java.util.Scanner; 
import java.io.File;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.GridLayout;

class TeamFrame extends JFrame {

public TeamFrame()throws IOException {
PlayerPlus player;
扫描仪myScanner =新扫描仪(新文件(Hankees.txt));

for(int num = 1; num <= 9; num ++){
player = new PlayerPlus(myScanner.nextLine(),myScanner.nextDouble());
myScanner.nextLine();
addPlayerInfo(player);
}

add(new JLabel());
add(new JLabel(------));
add(new JLabel(Team Batting Aberage:));
add(new JLabel(PlayerPlus.findTeamAverageString()));
setTitle(The Hankees);
setLayout(new GridLayout(11,2));
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setVisible(true);
}

void addPlayerInfo(PlayerPlus player){
add(new JLabel(player.getName()));
add(new JLabel(player.getAverageString()));
}
}

你可以在下面的屏幕截图中看到我有包含此文件。



图像不再可用



另外,我已经验证当我构建一个Hankees.txt的副本的应用程序被放置在具有编译的.class文件的bin文件夹中。



最后,如果我将第12行更改为以下并将Hankees.txt放在我的C:\驱动器的根目录下编译并运行正常。

 扫描仪myScanner =新的扫描仪(新文件(C:\\Hankees.txt)); 

所以基本上我的问题是我做错了什么?或者是Eclipse以某种方式负责?



感谢任何和所有的帮助!

解决方案

您需要src / Hankees.txt



您的文件位于文件夹不计入工作目录。\



或者,您可以将文件移动到项目的根目录,只需使用 Hankees.txt


So my 2009 new years resolution is to learn Java. I recently acquired "Java for Dummies" and have been following along with the demo code in the book by re-writing it using Eclipse. Anyway, every example in the book that uses a relative path does not seem to read the .txt file it's supposed to read from.

Here is the sample code:

import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.GridLayout;

class TeamFrame extends JFrame {

    public TeamFrame() throws IOException {
        PlayerPlus player;
        Scanner myScanner = new Scanner(new File("Hankees.txt"));

        for (int num = 1; num <= 9; num++) {
            player = new PlayerPlus(myScanner.nextLine(), myScanner.nextDouble());
            myScanner.nextLine();
            addPlayerInfo(player);
        }      

        add(new JLabel());
        add(new JLabel("   ------"));
        add(new JLabel("Team Batting Aberage:"));
        add(new JLabel(PlayerPlus.findTeamAverageString()));
        setTitle("The Hankees");
        setLayout(new GridLayout(11,2));
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }

    void addPlayerInfo(PlayerPlus player) {
        add(new JLabel(player.getName()));
        add(new JLabel(player.getAverageString()));
    }
}

And you can see in the below screen shot I have included this file.

image no longer available

Also, I have verified that when I build the application that a copy of Hankees.txt is placed in the bin folder with the compiled .class files.

Lastly, if I change line 12 to the following and place Hankees.txt in the root of my C:\ drive the program compiles and runs fine.

Scanner myScanner = new Scanner(new File("C:\\Hankees.txt"));

So basically, my question is what am I doing wrong? Or is Eclipse responsible for this in some way?

Thanks for any and all help!

解决方案

You need "src/Hankees.txt"

Your file is in the source folder which is not counted as the working directory.\

Or you can move the file up to the root directory of your project and just use "Hankees.txt"

这篇关于Eclipse中的相​​对文件路径如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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