aspose file tools*
The moose likes Beginning Java and the fly likes Question read and write file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question read and write file" Watch "Question read and write file" New topic
Author

Question read and write file

Tung Xuan
Greenhorn

Joined: Apr 16, 2012
Posts: 9
anyone can give me a solution on how to read and create files (images, rar, pdf, doc, etc.) I just create txt file

thanks a lot
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14139
    
  18

The code in your example is not going to work properly with JPG files.

There are two kinds of I/O classes in Java: there are streams (InputStream and OutputStream) and readers/writers. Streams are for dealing with binary data. Readers and writers are for dealing with text files. In your example, you're using a BufferedReader and a FileWriter, which are both classes designed to be used for reading and writing text files, to copy a JPG file, which is not a text file. You're also trying to store the binary data in a String, which is not designed for storing arbitrary binary data.

If you want to be able to copy arbitrary files (binary or text), use streams, not readers/writers. If you're using Java 7, then copying a file is very simple:

If you want to be able to load image files so that you can for example display them in a GUI, then Java's ImageIO API is useful for that. Note that it doesn't support all image file formats, just a few basic ones (including JPG and PNG).

Another remark: Never catch exceptions and ignore them, as you're doing in lines 16 and 17 of your code. When an exception occurs, you'll not see anything and you won't know that something went wrong. At least print an error message when you catch an exception. For example:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Tung Xuan
Greenhorn

Joined: Apr 16, 2012
Posts: 9
thanks for your reply, I will noted in catch exceptions, you can tell me how to transfer files (txt, jpg, rar, etc.) using UDP protocol,
My idea at the client read the file conversion to binary data into DatagramPacket and send to the server,
the server receive DatagramPacket and create a new file, but I do not know how to convert a file into binary data and vice versa.
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
Tung Xuan wrote:but I do not know how to convert a file into binary data and vice versa.

All files contain binary data. You don't need to convert them.
File content only becomes text or a spreadsheet or a pdf file or any other format when they are opened by a program that interprets the binary data in that way.
As long as you use the Stream classes that Jesper refers to to handle your file content you should have no problems.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Tung Xuan wrote:thanks for your reply, I will noted in catch exceptions, you can tell me how to transfer files (txt, jpg, rar, etc.) using UDP protocol,


Why UDP? UDP is particularly unsuited for file transfers. You either have to implement sequencing, error handling, and retransmission yourself (all stuff that TCP gives you), or accept the fact that your file may end up with pieces missing or in the wrong order.
 
Consider Paul's rocket mass heater.
 
subject: Question read and write file