Saving as Text File

S

sushant_taneja

Hello All,

I am coding a text editor and want to save a file with the
extension .txt??

How can I acheive it???

My text editor uses the follwoing code for saving the contents of the
document area in the file:

public void saveFile()
{
if(this.isSaved())
{
try
{

String text = docArea.getText();
FileWriter out = new FileWriter(oFile);
BufferedWriter bufferout = new BufferedWriter(out);
bufferout.write(text, 0, text.length());
bufferout.close();
out.close();
}
catch(IOException ioe)
{
System.out.println(ioe.toString());
}
saved = true;
}
else
{
saved = true;
saveFileAs();
}
}

Please help me out.

Regards,
Sushant Taneja
 
J

John B. Matthews

sushant_taneja said:
I am coding a text editor and want to save a file with the
extension .txt??

How can I acheive it???

My text editor uses the follwoing code for saving the contents of the
document area in the file:

public void saveFile()
{
if(this.isSaved())
{
try
{
String text = docArea.getText();
FileWriter out = new FileWriter(oFile);
BufferedWriter bufferout = new BufferedWriter(out);
bufferout.write(text, 0, text.length());

You might try bufferout.flush() at this point in your program.
 
A

Arne Vajhøj

sushant_taneja said:
I am coding a text editor and want to save a file with the
extension .txt??

How can I acheive it???

My text editor uses the follwoing code for saving the contents of the
document area in the file:

public void saveFile()
{
if(this.isSaved())
{
try
{

String text = docArea.getText();
FileWriter out = new FileWriter(oFile);
BufferedWriter bufferout = new BufferedWriter(out);
bufferout.write(text, 0, text.length());
bufferout.close();
out.close();
}
catch(IOException ioe)
{
System.out.println(ioe.toString());
}
saved = true;
}
else
{
saved = true;
saveFileAs();
}
}

What happen when you run that code ?

Arne
 
J

John B. Matthews

Arne Vajhøj said:
John said:
My text editor uses the follwoing code for saving the contents of the
document area in the file:
if(this.isSaved()) { [...]
String text = docArea.getText();
FileWriter out = new FileWriter(oFile);
BufferedWriter bufferout = new BufferedWriter(out);
bufferout.write(text, 0, text.length());

You might try bufferout.flush() at this point in your program.
bufferout.close();
out.close();

Calling flush just before close should not make a difference.

Arne: Good point; it shouldn't matter, and even an empty Dtring will
produce an empty file.

OP: Another thing to check is the value of this.isSaved().
 
M

Mike Schilling

sushant_taneja said:
Hello All,

I am coding a text editor and want to save a file with the
extension .txt??

How can I acheive it???

My text editor uses the follwoing code for saving the contents of
the
document area in the file:

public void saveFile()
{
if(this.isSaved())
{
try
{

String text = docArea.getText();
FileWriter out = new FileWriter(oFile);
BufferedWriter bufferout = new BufferedWriter(out);
bufferout.write(text, 0, text.length());
bufferout.close();
out.close();
}
catch(IOException ioe)
{
System.out.println(ioe.toString());
}
saved = true;
}
else
{
saved = true;
saveFileAs();
}
}

Please help me out.


I'd probably break up the text String into lines and use a PrintWriter
to be sure the line terminators get written correctly.
 
J

John W Kennedy

I'd probably break up the text String into lines and use a PrintWriter
to be sure the line terminators get written correctly.

Or use BufferedWriter with BufferedWriter.newLine();

Another problem: the second close (on the underlying FileWriter) is
redundant. BufferedWriter.close() closes the underlying Writer. In fact,
the underlying writer should not be declared at all -- it should just be
"new BufferedWriter(new FileWriter(...))".

And the short-life variables should be declared final, on general
principles.
 
A

Arne Vajhøj

John said:
And the short-life variables should be declared final, on general
principles.

Not everyone like to sprinkle final keywords all over the code ...

Arne
 
D

Daniel Pitts

Arne said:
Not everyone like to sprinkle final keywords all over the code ...

Arne
Not everyone like to find an accidentally modified local variable either.

"final" lets me assure myself that I actually have initialized it
exactly once, regardless of the flow of my program.

I despise the idiom
public Result doMyFoo() {
Foo foo = null;
if (hasFoo1()) {
foo = getFoo1();
}
if (hasFoo2()) {
foo = getFoo2();
}
return foo.doFoo(); // Possible NPE, oops!
}
vs:
public Result doMyFoo() {
final Foo foo;
if (hasFoo1()) {
foo = getFoo1();
} else if (hasFoo2()) {
foo = getFoo2();
} else {
return noFooResult();
}
return foo.doFoo();
}
 
A

Arne Vajhøj

Daniel said:
Not everyone like to find an accidentally modified local variable either.

I know why it is done.

But to me it is a great solution to a non existing problem.
"final" lets me assure myself that I actually have initialized it
exactly once, regardless of the flow of my program.

I despise the idiom
public Result doMyFoo() {
Foo foo = null;
if (hasFoo1()) {
foo = getFoo1();
}
if (hasFoo2()) {
foo = getFoo2();
}
return foo.doFoo(); // Possible NPE, oops!
}
vs:
public Result doMyFoo() {
final Foo foo;
if (hasFoo1()) {
foo = getFoo1();
} else if (hasFoo2()) {
foo = getFoo2();
} else {
return noFooResult();
}
return foo.doFoo();
}

I really don't like any of those two.

And the first is missing some basic logic. I am
skeptical about whether programmers will be able to apply
final if they are not able to consider the possibility
that both hasFoo1 and hasFoo2 returns false.

Arne
 
D

Daniel Pitts

Arne said:
I know why it is done.

But to me it is a great solution to a non existing problem.


I really don't like any of those two.

And the first is missing some basic logic. I am
skeptical about whether programmers will be able to apply
final if they are not able to consider the possibility
that both hasFoo1 and hasFoo2 returns false.
This is a simplified example. In any case, the possibility the
programmer being a human being and forgetting a line is high over time.
Keep in mind that forgetting is different than not considering.
 
R

Roedy Green

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top