Y. Daniel Liang, “Introduction to Java Programming”, 10th Edition, Pearson, 2015
ID: 3662991 • Letter: Y
Question
Y. Daniel Liang, “Introduction to Java Programming”, 10th Edition, Pearson, 2015 ISBN: 978-0-13-376131-3
USING Java Programming
Write a program that will write the contents of a text file backwards one character at a time. Your program should first ask for the text file name to be processed – and after verifying that the file exists, it should read the file into a StringBuilder buffer. Save the new file using the old filename appended with “Ver2-“ at the front. So for the file “MyStuff.txt” the file would be saved as Ver2-MyStuff.txt”. You can use any text file to test your work
Explanation / Answer
class filewr
{
public static void main(String args[])
{
String str="file.txt";
BufferedReader br = new BufferedReader(new FileReader(str));
try {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
sb.append(System.lineSeparator());
line = br.readLine();
}
String everything = sb.toString();
} finally {
br.close();
}
String str1="Ver2-"+str;
File file = new File(str1);
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
writer.write(sb.toString());
} finally {
if (writer != null) writer.close();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.