I have Come across so many programmes of how to read a text file using Scanner
in Java. Following is some dummy code of Reading a text file in Java using Scanner
:
public static void main(String[] args) {
File file = new File("10_Random");
try {
Scanner sc = new Scanner(file);
while (sc.hasNextLine()) {
int i = sc.nextInt();
System.out.println(i);
}
sc.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
}
But, please anyone help me in "Writing" some text (i.e. String or Integer type text) inside a .txt
file using Scanner
in java. I don't know how to write that code.
Scanner
can't be used for writing purposes, only reading. I like to use a BufferedWriter
to write to text files.
BufferedWriter out = new BufferedWriter(new FileWriter(file));
out.write("Write the string to text file");
out.newLine();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With