Write and read to .txt file JAVA | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Write and read to .txt file JAVA

I have a text file which has several columns of text. I guess I need to import the text as an array but can someone point me in the right direction? Also, how do I write to file separating strings with a tab size space? Thank you

31st Dec 2017, 7:56 PM
Duncan Skilton
Duncan Skilton - avatar
3 Answers
+ 1
Read: You could use this: Scanner scanner = new Scanner(new File (HeresThePath); while (scanner.hasNextLine(){ System.out.println(scanner.nextLine(); } scanner.close ();// this is very important! Write: BufferedWriter writer = new BufferedReader(new FileReader (PATH)); writer.write ("A Line"); writer.newLine (); writer.write ("Another Line"); writer.close (); // this is very important!
31st Dec 2017, 10:31 PM
Jonas Schröter
Jonas Schröter - avatar
+ 1
For tab space, use "\t" between your Strings!
31st Dec 2017, 10:33 PM
Jonas Schröter
Jonas Schröter - avatar
+ 1
That’s great, thank you. I’ll give it a try later today 👍🏿
2nd Jan 2018, 7:58 AM
Duncan Skilton
Duncan Skilton - avatar