There is a utility for comparing the files "test.txt" and "test1.txt". The command line is working properly. To run, enter "java -cp. CompFiles test.txt test1.txt"

(text files are pre-placed in one directory and everything works ok)

But how to start the program from Eclipse, at the same time to immediately specify the file names? When replacing FileInputStream (args [0]) with FileInputStream ("test.txt"), it still does not see the file in the directory.

import java.io.*; public class CompFiles { public static void main(String[] args) { int i = 0, j = 0; if (args.length != 2) { //убедиться, что имена файлов передаются программе System.out.println("Использование: Compfiles файл1 файл2"); return; } // Сравнить файлы try ( FileInputStream f1 = new FileInputStream(args[0]); FileInputStream f2 = new FileInputStream(args[1])) { //проверка содержимого файлов do { i = f1.read(); j = f2.read(); if(i !=j)break; } while (i != -1 && j != -1); if (i !=j) System.out.println("Содержимое отличачется"); else System.out.println("Содержимое совпадает"); } catch (IOException exc) { System.out.println("Ошибка ввода-вывода " + exc); } } } 
  • 2
    specify the full paths for the files or set the working directory at startup - Alex Chermenin

3 answers 3

Use getResourceAsStream to get the stream in such cases, in the future it will help to avoid problems with the work from the jar.

  FileInputStream f1 = CompFiles .getResourceAsStream("/test.txt"); 
  • And how does your code correspond to the question? It's about getting files not from jar resources, but from the working directory. - ezhov_da

Everything turned out to be much simpler than I thought. When called from the command line, everything works. But to set the values ​​right away, you need to remove the if check statement or the return statement:

 if (args.length != 2) { //убедиться, что имена файлов передаются программе System.out.println("Использование: Compfiles файл1 файл2"); return; } 

The utility saw that the command line values ​​were zero and went to the beginning through return.

To specify the arguments directly use:

 FileInputStream f1 = new FileInputStream("test.txt"); FileInputStream f2 = new FileInputStream("testdata.txt") 

    In each development environment, in the project settings, you can specify command line arguments and a working folder.
    In Eclipse, this is the place in the Debug and Run menu in the Debug Configurations and Run Configurations sub-item.

    • Thanks It works. But is it possible to determine the arguments in advance in the program? - Victor Presnyakov
    • Did you open Debug Configurations? There is a tab Arguments. - ezhov_da