What I'm trying to do is read a set of names from one file and print it in the following format to another file:
Number <no> Hello <name>, Welcome
where <no> is a number generated by a for loop and <name> is the name read from a file. However my output is:
Number 1 Hello Hugo
Huffman
DickeryWelcomeNumber 1 Hello
Thorpe
SebastianeryWelcome
The following is the code I used :
import java.io.*;
import java.lang.*;
public class ReadWriteFile {
public static void main(String[] args) {
try
{
java.io.FileReader fileReader = new java.io.FileReader(
"textfile.txt");
java.io.FileWriter fileWriter = new java.io.FileWriter("out.txt");
FileWriter fstream = new FileWriter("out.txt");
BufferedWriter out = new BufferedWriter(fstream);
char[] buffer = new char[20];
int byteCount = 0;
for (int i = 1; i < 10; i++) {
while ((byteCount = fileReader.read(buffer)) >= 0)
{
fileWriter.write("Number "+i+" Hello ");
fileWriter.write(buffer);
fileWriter.append("Welcome");
}
}
fileWriter.flush();
fileWriter.close();
fileReader.close();
System.out.println("Wrinting complete!");
}
catch (Exception e)
{
System.out.println(e.toString());
}
}
}
First off admittedly, the code isn't entirely mine. I got it from here and such there are quite a few things I don't understand such as what is the purpose of the buffer character array and why is it used? the use of fileWriter.append was my idea and apparently I don't seem to understand it's usage :p
anyways any help on this is greatly appreciated (even if it a refinement or a complete redo of the code) :)
Question
rustix
What I'm trying to do is read a set of names from one file and print it in the following format to another file:
Number <no> Hello <name>, Welcome
where <no> is a number generated by a for loop and <name> is the name read from a file. However my output is:
The following is the code I used :
import java.io.*; import java.lang.*; public class ReadWriteFile { public static void main(String[] args) { try { java.io.FileReader fileReader = new java.io.FileReader( "textfile.txt"); java.io.FileWriter fileWriter = new java.io.FileWriter("out.txt"); FileWriter fstream = new FileWriter("out.txt"); BufferedWriter out = new BufferedWriter(fstream); char[] buffer = new char[20]; int byteCount = 0; for (int i = 1; i < 10; i++) { while ((byteCount = fileReader.read(buffer)) >= 0) { fileWriter.write("Number "+i+" Hello "); fileWriter.write(buffer); fileWriter.append("Welcome"); } } fileWriter.flush(); fileWriter.close(); fileReader.close(); System.out.println("Wrinting complete!"); } catch (Exception e) { System.out.println(e.toString()); } } }First off admittedly, the code isn't entirely mine. I got it from here and such there are quite a few things I don't understand such as what is the purpose of the buffer character array and why is it used? the use of fileWriter.append was my idea and apparently I don't seem to understand it's usage :p
anyways any help on this is greatly appreciated (even if it a refinement or a complete redo of the code) :)
Link to comment
Share on other sites
0 answers to this question
Recommended Posts