Working with a file in Eclipse

I have an instruction to run the program on the command line, for example:

java SetTest < alice30.txt 

I wonder how to do this in Eclipse. I tried putting this in Run Configuration as follows:

enter image description here

Another thing I don’t know is where to put this file (alice30.txt). Is this in the root of the project or in the src folder where the source files are?

I know these are beginner questions, but I am stuck and need help.

EDIT : As @Kane suggested, I transferred the file and opened the stream. Instead:

 Scanner in = new Scanner(System.in); 

Now I use:

 Scanner in = new Scanner(new File("alice30.txt")); 
+4
source share
4 answers

The eclipse root directory is the base directory of the project (i.e., not the src / directory, directly under the project.)

Generally, a good style has a “resources” folder for txt, graphics, etc.

Instead of trying to transfer the stream, you can just pass the file name and open the stream itself.

The reason that you do in Eclipse does not work, because your command line / shell / dos / bash / whatever handles creating the input stream from the file for you. Eclipse does not. So, from the command line: < alice.txt means "run this program without arguments and create a stream for system.in", while in Eclipse means "run this program with two arguments" <'and' alice.txt '

+2
source

You can pass the full path to the file in arguments (for example, c: /.../ alice30.txt))

+3
source

you need to do this: Add: import java.io.IOException; import java.nio.file.Paths;

then: replace "Scanner in = new scanner (System.in);" to "Scanner in = new scanner (Paths.get (" alice30.txt ")) ;, and you also need to do this:" public static void main (String args []) throws an IOException "

+1
source

With the information from this link / page and several attempts, I will figure out how to pass arguments and files using the local route in eclipse Run -> Run Configurations .., although this is not recommended, as Kane said.

In my case: I need to do "$ java someClass tinyW.txt <tinyT.txt" (This is an example from Robert Sedgwick's book of Algorithms)

In my case, "tinyW.txt" is an argument, so in the eclipse environment you can set in Run -> Run Configurations -> Arguments -> Program arguments: / local address / tinyW.txt. For my Ubuntu: / home / **** / tinyW.txt enter image description here "<tinyT.txt" is a file that connects to the main arguments, so you can set the route and file in "Run → RUn Configurations → Common", click on "Input File", use the "File System" icon and select a file from the local calculations. See Figure. So, in the input file: /local_address/tinyT.txt. My business: / home / *** / tinyT.txt. Hope it also works for you. enter image description here

0
source

Source: https://habr.com/ru/post/1380952/


All Articles