idea java加载文件,使用Intellij idea IDE读取文件

I am a long time eclipse user and I have started to play around with IntelliJ Idea IDE.

So from my understanding. A project in IntelliJ is the same as the Eclipse Workspace. As well, a module in IntelliJ is the equivalent of a project in eclipse.

I created a project in IntelliJ, still not sure why there is a src folder in it if it is supposed to be a workspace...

Afterwards, I create a Module in the project. I created a class inside the src of the module. The code I made inside the class is the following:

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

public class MainClass {

public static void main(String[] args) throws FileNotFoundException {

System.out.println("Hello World!");

Scanner input = new Scanner(new File ("test123.txt"));

String answer = input.nextLine();

System.out.println(answer);

}

}

The problem is that I get an error trying to read the file. I tried putting the .txt file inside my src file which is located inside the module and outside the src file but inside the module. But in both cases the file is not found. Yes the code works, I tried it on Eclipse and it works fine. The file name is spelled correctly as well.

Here is a picture of my project/workspace if it is helpful:

QLXqh.png

I apologize if the image is too small, you can find a larger version here: http://oi60.tinypic.com/jg1g60.jpg

解决方案

Just move the file directly to the project folder which calls "Java" (so the main folder) (next to the blue-blurred strip you made :P)

or if it doesn't help, then move the test123.txt file to FirstJavaProgram directory

you can also change filename to one of these:

1) src/test123.txt

2) FirstJavaProgram/src/test123.txt

I am not sure which one will be fine in your case.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值