0

Printing every line from a file?

Heyya! I have this class: The append-method works wonderfully. However, when I try to print each line, things get really buggy. Say I appended some sentences in the test.txt: SentenceA SentenceB SentenceC How will I have to change the getting()-method so it is going to print each line? import java.util.*; import java.io.*; import java.lang.*; public class Test { private Formatter form; private Scanner sc; public void append(String text) { try(FileWriter file = new FileWriter("test.txt", true); BufferedWriter bw = new BufferedWriter(file); PrintWriter out = new PrintWriter(bw)) { out.println(text); } catch (IOException e) { e.printStackTrace(); } } public void getting() { try { File f = new File("test.txt"); FileReader fr = new FileReader(f); BufferedReader br = new BufferedReader(fr); while ((br.readLine())!=null) { System.out.println(br.readLine()); } br.close(); } catch(Exception e) { e.printStackTrace(); } } }

11th Feb 2019, 10:59 PM
Gino ^^
Gino ^^ - avatar
1 Odpowiedź
0
https://www.tutorialspoint.com/java/io/bufferedreader_readline.htm In this example they use a String fileLine = "null" --> while (fileLine = br.readLine()) != null) --> System.out.println (fileLine);
12th Feb 2019, 12:42 AM
Denise Roßberg
Denise Roßberg - avatar