Suppose a text file has following lines Twinkle, twinkle, little star, How I won
ID: 3570745 • Letter: S
Question
Suppose a text file has following lines
Twinkle, twinkle, little star,
How I wonder what you are.
Up above the world so high,
Like a diamond in the sky.
write a program that will read this file and add "******" after everyline
so the file should be like this
Twinkle, twinkle, little star,
*****
How I wonder what you are.
*****
Up above the world so high,
*****
Like a diamond in the sky.
*****
actually i need a method for this not the whole program and if possible using has next not the buffer one
Explanation / Answer
import java.io.*;
import java.io.*;
import java.io.IOException;
class twinkle{
public static void main(String[] args) {
BufferedReader br = null;
BufferedWriter wr = null;
try {
String sCurrentLine;
br = new BufferedReader(new FileReader("Twinkle.txt"));
wr = new BufferedWriter(new FileWriter("Twinkle2.txt"));
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
wr.write(sCurrentLine);
wr.write(" ****** ");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
wr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.