从eclipse读取文件 [英] Read from file in eclipse

查看:258
本文介绍了从eclipse读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从一个文本文件读取数据到我的java程序。然而,eclipse连续地给我一个源未找到的错误,无论我放在哪里。



我已经在项目目录中添加了一个源文件夹,问题在于它和项目的bin文件,它仍然找不到。



我甚至把它的副本放在我的桌面上,并试图将eclipse指向那里,当它要求我浏览源查找路径。



无论我做什么都找不到该文件。



这里是我的代码,以防万一它们相关:

  System.out.println(System.getProperty(user.dir)); 
文件文件=新文件(file.txt);


扫描仪扫描仪=新扫描仪(文件);

此外,它表示用户目录是项目目录,也有副本。 / p>

我不知道该怎么做。



谢谢,
Alex



在尝试下面的建议并再次刷新之后,我遇到了一大堆错误。

  FileNotFoundException(Throwable)。< init>(String)行:195 
FileNotFoundException(异常)< init>(String)行:不可用
FileNotFoundException(IOException)< init&字符串)行:不可用
FileNotFoundException。< init>(String)行:不可用
URLClassPath $ JarLoader.getJarFile(URL)行:不可用
URLClassPath $ JarLoader.access $ 600 URLClassPath $ JarLoader,URL)行:不可用
URLClassPath $ JarLoader $ 1.run()行:不可用
AccessController.doPrivileged(PrivilegedExceptionAction< T>)行:不可用[native method]
URLClassPath $ JarLoa der.ensureOpen()行:不可用
URLClassPath $ JarLoader。< init>(URL,URLStreamHandler,HashMap)行:不可用
URLClassPath $ 3.run()行:不可用
AccessController.doPrivileged(PrivilegedExceptionAction< T>)行:不可用[native method]
URLClassPath.getLoader(URL)行:不可用
URLClassPath.getLoader(int)行:不可用
URLClassPath .access $ 000(URLClassPath,int)行:不可用
URLClassPath $ 2.next()行:不可用
URLClassPath $ 2.hasMoreElements()行:不可用
ClassLoader $ 2.hasMoreElements()行:不可用
CompoundEnumeration< E> .next()行:不可用
CompoundEnumeration< E> .hasMoreElements()行:不可用
ServiceLoader $ LazyIterator.hasNext()行:not可用
ServiceLoader $ 1.hasNext()行:不可用
LocaleServiceProviderPool $ 1.run()行:不可用
AccessController.doPrivileged(Pri (class< LocaleServiceProvider>)行:不可用
LocaleServiceProviderPool.getPool(Class< LocaleServiceProvider>)行:不可用
NumberFormat.getInstance(Locale,int)行:不可用
NumberFormat.getNumberInstance(Locale)行:不可用
Scanner.useLocale(Locale)行:不可用
扫描程序。 < init>(可读,模式)行:不可用
扫描程序< init>(ReadableByteChannel)行:不可用
扫描程序< init>(文件)行:不可用

代码使用:

 的System.out.println(System.getProperty( user.dir来)); 
文件文件=新文件(System.getProperty(user.dir)+/file.txt);


扫描仪扫描仪=新扫描仪(文件);


解决方案

你尝试刷新(右键 - >刷新)项目文件夹中复制的文件在那里?这将使用Eclipse的内部文件系统SYNC您的文件系统。



当您运行Eclipse项目时,CWD(当前工作目录)是项目的根目录。不是bin的目录。不是src的目录,而是根目录。



另外,如果你在Linux,请记住它的文件系统通常区分大小写。


I'm trying to read from a text file to input data to my java program. However, eclipse continuosly gives me a Source not found error no matter where I put the file.

I've made an additional sources folder in the project directory, the file in question is in both it and the bin file for the project and it still can't find it.

I even put a copy of it on my desktop and tried pointing eclipse there when it asked me to browse for the source lookup path.

No matter what I do it can't find the file.

here's my code in case it's pertinent:

System.out.println(System.getProperty("user.dir"));
    File file = new File("file.txt");


    Scanner scanner = new Scanner(file);

in addition, it says the user directory is the project directory and there is a copy there too.

I have no clue what to do.

Thanks, Alex

after attempting the suggestion below and refreshing again, I was greeted by a host of errors.

FileNotFoundException(Throwable).<init>(String) line: 195   
FileNotFoundException(Exception).<init>(String) line: not available 
FileNotFoundException(IOException).<init>(String) line: not available   
FileNotFoundException.<init>(String) line: not available    
URLClassPath$JarLoader.getJarFile(URL) line: not available  
URLClassPath$JarLoader.access$600(URLClassPath$JarLoader, URL) line: not available  
URLClassPath$JarLoader$1.run() line: not available  
AccessController.doPrivileged(PrivilegedExceptionAction<T>) line: not available [native method] 
URLClassPath$JarLoader.ensureOpen() line: not available 
URLClassPath$JarLoader.<init>(URL, URLStreamHandler, HashMap) line: not available   
URLClassPath$3.run() line: not available    
AccessController.doPrivileged(PrivilegedExceptionAction<T>) line: not available [native method] 
URLClassPath.getLoader(URL) line: not available 
URLClassPath.getLoader(int) line: not available 
URLClassPath.access$000(URLClassPath, int) line: not available  
URLClassPath$2.next() line: not available   
URLClassPath$2.hasMoreElements() line: not available    
ClassLoader$2.hasMoreElements() line: not available 
CompoundEnumeration<E>.next() line: not available   
CompoundEnumeration<E>.hasMoreElements() line: not available    
ServiceLoader$LazyIterator.hasNext() line: not available    
ServiceLoader$1.hasNext() line: not available   
LocaleServiceProviderPool$1.run() line: not available   
AccessController.doPrivileged(PrivilegedExceptionAction<T>) line: not available [native method] 
LocaleServiceProviderPool.<init>(Class<LocaleServiceProvider>) line: not available  
LocaleServiceProviderPool.getPool(Class<LocaleServiceProvider>) line: not available 
NumberFormat.getInstance(Locale, int) line: not available   
NumberFormat.getNumberInstance(Locale) line: not available  
Scanner.useLocale(Locale) line: not available   
Scanner.<init>(Readable, Pattern) line: not available   
Scanner.<init>(ReadableByteChannel) line: not available 
Scanner.<init>(File) line: not available    

code used:

System.out.println(System.getProperty("user.dir"));
    File file = new File(System.getProperty("user.dir") + "/file.txt");


    Scanner scanner = new Scanner(file);

解决方案

Did you try refreshing (right click -> refresh) the project folder after copying the file in there? That will SYNC your file system with Eclipse's internal file system.

When you run Eclipse projects, the CWD (current working directory) is project's root directory. Not bin's directory. Not src's directory, but the root dir.

Also, if you're in Linux, remember that its file systems are usually case sensitive.

这篇关于从eclipse读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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