• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question read and write file

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Tung Xuan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 679
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic