aspose file tools*
The moose likes Java in General and the fly likes Writing to a file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Writing to a file" Watch "Writing to a file" New topic
Author

Writing to a file

Justine Hume
Greenhorn

Joined: Jun 07, 2011
Posts: 2


I would like the selection that the user makes to be written to a .txt file, but am unsure how to do this. Can anyone help?

Thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36592
    
  16
Welcome to the Ranch
just read a thread where I made similar suggestions about text files for somebody else. Try this post and the links therein.
Mike Zal
Ranch Hand

Joined: May 04, 2011
Posts: 144

File IO with BufferedOutputStream example


OCJP6, OCWCD5
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

There are tons of examples of this on the internet, but first you should read Sun's tutorial on Reading and Writing to files: http://download.oracle.com/javase/tutorial/essential/io/file.html Then if you have questions after that posted what you tried.

Hunter


"If the facts don't fit the theory, get new facts" --Albert Einstein
Justine Hume
Greenhorn

Joined: Jun 07, 2011
Posts: 2
Sorry guys, I'm kind of new to Java and I have had a look at the links and have no idea how to implement that code with mine
Mike Zal
Ranch Hand

Joined: May 04, 2011
Posts: 144

Justine Hume wrote:Sorry guys, I'm kind of new to Java and I have had a look at the links and have no idea how to implement that code with mine


Step 1) Create some type of FileWriter (You need to specify the file location)
Step 2) wrap it with a BufferFileWriter (Usually done on the same line as step 1)
Step 3) Replace a System.out.println() with <your writer>.write() call
Step 4) <your writer>.close()

You should have new file in the location specified in Step 1.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36592
    
  16
Mike Zal wrote: . . .
Step 2) wrap it with a BufferFileWriter (Usually done on the same line as step 1)
Step 3) Replace a System.out.println() with <your writer>.write() call
Step 4) <your writer>.close() . . .
careful about spellings; it's a BufferedWriter. And writeLine(), surely?

You will also have to wrap the whole thing in a try-finally-catch. Look here, and Rob's reply. Then you substitute the Readers with Writers, and check the API where there is an underlined class name above, to check I have the method names right.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Writing to a file
 
Similar Threads
for if else + Output Problem (array)
Use of Scanner and Text files
Else without if error
Display the prime factors of a number using sets
can not find symble