Hello, since i started to learn java, i had a question which was "how to read from .txt file". I looked over a lot of tutorials, but i learned to to write in a txt file... Now i'm having a lot of troubles with this thing. I know it's very hard and yes, i don't want dirrectly the code. Just some hints, or some help when i'm getting errors. Thanks for your time
I made a bit, it's like PHP: public void readFromCmds(){ Path file = "test.txt"; try (InputStream in = Files.newInputStream(file); BufferedReader reader = new BufferedReader(new InputStreamReader(in))) { String line = null; while ((line = reader.readLine()) != null) { System.out.println(line); } } catch (IOException x) { System.err.println(x); } } but i'm getting an error from PHP: Path file = "test.txt"; What is it telling me Code: Type mismatch: cannot convert from String to Path
You need to create Path object. http://docs.oracle.com/javase/tutorial/essential/io/pathOps.html The thing above may work as well. Depends on what does newInputStream accept as argument.
Ok i did it like PHP: public void readFromCmds(){ Path file = Paths.get("test.txt"); try (InputStream in = Files.newInputStream(file); BufferedReader reader = new BufferedReader(new InputStreamReader(in))) { String line = null; while ((line = reader.readLine()) != null) { System.out.println(line); } } catch (IOException x) { System.err.println(x); } }