0
package Aula12;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class Ex1 {
        public static void main(String[] args) {
            try {
                Scanner input = new Scanner(new FileReader("words.txt"));
                while (input.hasNext()) {
                    String word = input.next();
                    System.out.println(word);
                }
    
                ArrayList<String> texto = new ArrayList<>();
                ArrayList<String> verificador = new ArrayList<>();
    
                while (input.hasNext()) {
                    String word = input.next();
                    texto.add(word);
                }
    
                int palavras = texto.size();
                System.out.println("Número total de palavras: " + palavras);
    
                for (String i : texto) {
                    if (verificador.contains(i)) {
                        continue;
                    } else {
                        verificador.add(i);
                    }
                }
                int palavrasdiff = verificador.size();
                System.out.println("Número de palavras diferentes: " + palavrasdiff);
    
                input.close();

            } catch (FileNotFoundException e) {
                System.out.println("File not found: " + e.getMessage());
            } catch (IOException e) {
                System.out.println("Error reading file: " + e.getMessage());
            }
        }
    }


The file "words.txt" is in the src folder inside my Java project but the system can´t find it.

I expected the code to find the file, read it and count how many words it has and how many different words it has too but the system is unable to find the refered file.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • 3
    "The file "words.txt" is in the src folder inside my Java project but the system can´t find it." Presumably that's *not* the working directory of the process though. You've provided a relative filename - that's going to be relative to the working directory of the process... – Jon Skeet May 16 '23 at 11:22
  • 2
    [How to get the current working directory in Java?](https://stackoverflow.com/q/4871051) – 001 May 16 '23 at 11:23
  • Put the file into the project root, not into the src folder. Your IDE typically sets the project root as the working directory. – f1sh May 16 '23 at 11:30
  • I puted the file into the project root and it worked. Thank you! – Vasco Vouzela May 16 '23 at 11:44
  • Read about [the working directory](https://en.wikipedia.org/wiki/Working_directory) in computer systems. This is not actually a Java concept, it’s a near-universal concept in operating systems. It was a common concept before Java even existed. You need to understand how filenames work before writing code that works with files. – VGR May 16 '23 at 17:04

1 Answers1

0

You can first print the directory of the currently running program, and then move words.txt over there.

System.out.println(System.getProperty("user.dir"));
yang lee
  • 16
  • 1