This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Creating a text file using FileWriter  RSS feed

 
sudheer raj
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have created a text file using FileWriter.
I can see it properly when i open it with MS-Word,but when i try to open it with notepad or wordpad.
It is not displayed properly.
To print this file, which docflavor should i use?
Whether it is docflavor.INPUT_STREAM.TEXT_PLAIN or wat???
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sudheer raj:
I have created a text file using FileWriter.
I can see it properly when i open it with MS-Word,but when i try to open it with notepad or wordpad.
It is not displayed properly.


Strange, if this is a text file it should ideally open properly in notepad and not in MS-Word(as it is not a ms word style document.)
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's impossible to understand what exactly you mean by "not displayed properly."

To get better help sooner, post a SSCCE that clearly demonstrates the problem.
 
Rob Spoor
Sheriff
Posts: 21090
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nitesh Kant:


Strange, if this is a text file it should ideally open properly in notepad and not in MS-Word(as it is not a ms word style document.)

Actually, MS Word can also handle plain text files. So the problem is in how the file is written.

Sudheer, can you show us how you create the FileWriter and what you write to it?
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rob Prime:

Actually, MS Word can also handle plain text files.

I see.
 
sudheer raj
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
str="xyz\nabcd\n";
char buff[]=new char[str.length()];
str.getChars(0,str.length(),buff,0);
try{
File f1=new File("/logs","temp.txt");

FileWriter fo=new FileWriter(f1);


fo.write(buff);
fo.close();
}catch(IOException ioe){

}

This way i created the file.
 
Rob Spoor
Sheriff
Posts: 21090
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strange, that code works here just fine.
Well, with the exception that Notepad doesn't recognize the line break, but that is because Windows requires \r\n, not just \n. In fact, use System.getProperty("line.separator") instead; that way it'll work with all operating systems.
 
sudheer raj
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so instead of \n if i use \r\n then will it open properly in the notepad?
one more thing now i want to print this file
while specifying the docflavor which one should i use
whether it is docflavor.INPUT_STREAM.TEXT_PLAIN or wat??
 
sudheer raj
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you RobPrime.
Now its getting displayed properly
And even its getting printed from the program

Thanks a lot
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!