-3

I am using this method to read all text lines from a text file. But the method is reading just the first line. Any idea please!

And I am using the Java API Method Files.readAllLines()

public static String dateiEinlesen(String path, String name)
{
  Path path = Paths.get(path, name);

  Charset charset = Charset.forName("UTF-8");
  try
  {
    List<String> lines = Files.readAllLines(path, charset);
    for (String line : lines)
    {
      return line;
    }
  }
  catch (IOException e)
  {
  }
  return "File could not found! ";
}
Jozef Chocholacek
  • 2,874
  • 2
  • 20
  • 25
Modo
  • 31
  • 5
  • 3
    Related: _[What's the difference between JavaScript and Java?](https://stackoverflow.com/questions/245062/whats-the-difference-between-javascript-and-java)_ – Luca Kiebel Jul 30 '19 at 10:12
  • thank you for the answer i though also that will be the problem but i could not solve it ! im very new in the Programming langguage ! can you please show me the coorect code? – Modo Jul 30 '19 at 10:23
  • If one of the answers has solved your problem please accept it with a tick. – mwarren Jul 30 '19 at 10:25
  • unfortunately nothing did work! – Modo Jul 30 '19 at 10:40
  • @Modo what did not work? – mwarren Jul 30 '19 at 10:43
  • Well, `return line;` causes method to stop and return current value from `line`. Why do you want to write that method in the first place where since `Files.readAllLines` seems to do exactly what you want? – Pshemo Jul 30 '19 at 10:48
  • it still read just one line ! but im working with Java web aplication and from HTML site i musst download a word File, in the word File it read jsut on line ! but i also trying the Method in normal Java Programm (not web aplicaton and word File )and it has worked perfectly ! all lines has beeen readed ! and i used my first Mehtod – Modo Jul 30 '19 at 10:54

2 Answers2

2

The problem is that you are returning from the for loop which means after the first line is read the method is exited. If you print the line out instead of returning it should print all of the lines in the file.

If your intention is to return all of the lines of the file then just change your code to :-

public static List<String> dateiEinlesen(String path, String name) throws IOException
{
  Path filepath = Paths.get(path, name);

  return Files.readAllLines(filepath, Charset.forName("UTF-8"));
}

It is not usual to catch an exception and do nothing so it is better to let the exception propagate and let the caller handle it.

mwarren
  • 759
  • 3
  • 6
  • 1
    How `return "File could not found! ";` is supposed to compile here when method is declared to return a `List`? – Pshemo Jul 30 '19 at 10:24
  • Now method is missing return value in case exception will be thrown for `Files.readAllLines`. In such case in `catch` block we could return empty list, but that would create confusion because user of that method wouldn't know if lack of values in list was caused by empty file or by some other *problem* (like lack if access privilege, or maybe file didn't exist). To avoid such confusion we could return `null` instead, but this only lets us distinguish between correct value and value caused by problem but without any info about that problem. – Pshemo Jul 30 '19 at 10:43
  • Preferred way would be passing exception (rethrowing it) from method with `public static List dateiEinlesen(String path, String name) throws IOException { return Files.readAllLines(Paths.get(path, name), Charset.forName("UTF-8")); }` to let user catch exception and decide what to do in each situation. But then we probably don't even need that method in the first place since it only wraps one short line of code and method *invocation* still needs to be wrapped in try/catch... – Pshemo Jul 30 '19 at 10:43
0

Try this one, just change the path

 Path path = Paths.get("C:\\Users\\123\\Desktop\\", "write a patch.txt");

            Charset charset = Charset.forName("UTF-8");
            try
            {
                List<String> lines = Files.readAllLines(path, charset);

                for (String line : lines)
                {
                    System.out.println(line);
                }
            }
            catch (IOException e)
            {

            }

        }

What you can do instead of System out, you can store the line String in a string array, and then display everything when you finish reading

alan
  • 48
  • 1
  • 13