I've tried to find help @ Sun's community discussion boards, but no one seems to be able to help me there...
Here are my dilemmas:
I'm using readLine() method of the BufferedReader class to read the file whose name is passed as the argument. Then I'm using the FileReader class to attach my file to the BufferedReader object. It seems like I'm doing it correctly (because it's reading the file properly), however, when it reads each line, it is added to the string object and does not have a line break. I end up having this one *long* line with all the text on it.
See code below:
// Read a text file
public void readFile(String file) {
BufferedReader myInputStream;
try
{
myInputStream = new BufferedReader(new FileReader(file));
try
{
String line;
while ((line=myInputStream.readLine()) !=null) {
text.append(line);
}
}
catch (IOException e) {
System.out.println("End of file. Exception: " +e.getMessage());
return;
}
}
catch (FileNotFoundException e) {
System.out.println("File not found! Exception: " +e.getMessage ());
return;
}
}
--
Then I am using the writeBytes() method of the DataOutputStream class to write to the file whose name is passed as the argument. I'm using the FileOutputStream class to attach my file to the DataOutputStream object. The problem lies when I attempt writing, it only writes the filename (e.g., test.java) and not the text within the file.
see code below:
// Write to file
public void writeFile(String file) {
DataOutputStream myOutputStream;
try
{
myOutputStream = new DataOutputStream(new FileOutputStream (file));
try
{
myOutputStream.writeBytes(file);
}
catch (FileNotFoundException e) {
System.out.println("Unable to write to file. Exception: "
+e.getMessage());
return;
}
}
catch (IOException e) {
System.out.println("End of file. Exception: " +e.getMessage());
return;
}
}
===============
I apologize outright for this long post. If anyone out here has suggestions or can enlighten this Java newbie, it would be much appreciated.
Thanks,
RaDragon
Here are my dilemmas:
I'm using readLine() method of the BufferedReader class to read the file whose name is passed as the argument. Then I'm using the FileReader class to attach my file to the BufferedReader object. It seems like I'm doing it correctly (because it's reading the file properly), however, when it reads each line, it is added to the string object and does not have a line break. I end up having this one *long* line with all the text on it.
See code below:
// Read a text file
public void readFile(String file) {
BufferedReader myInputStream;
try
{
myInputStream = new BufferedReader(new FileReader(file));
try
{
String line;
while ((line=myInputStream.readLine()) !=null) {
text.append(line);
}
}
catch (IOException e) {
System.out.println("End of file. Exception: " +e.getMessage());
return;
}
}
catch (FileNotFoundException e) {
System.out.println("File not found! Exception: " +e.getMessage ());
return;
}
}
--
Then I am using the writeBytes() method of the DataOutputStream class to write to the file whose name is passed as the argument. I'm using the FileOutputStream class to attach my file to the DataOutputStream object. The problem lies when I attempt writing, it only writes the filename (e.g., test.java) and not the text within the file.
see code below:
// Write to file
public void writeFile(String file) {
DataOutputStream myOutputStream;
try
{
myOutputStream = new DataOutputStream(new FileOutputStream (file));
try
{
myOutputStream.writeBytes(file);
}
catch (FileNotFoundException e) {
System.out.println("Unable to write to file. Exception: "
+e.getMessage());
return;
}
}
catch (IOException e) {
System.out.println("End of file. Exception: " +e.getMessage());
return;
}
}
===============
I apologize outright for this long post. If anyone out here has suggestions or can enlighten this Java newbie, it would be much appreciated.
Thanks,
RaDragon