Results 1 to 6 of 6
Thread: how to handle files?
- 06-09-2009, 04:42 PM #1
Member
- Join Date
- Feb 2009
- Posts
- 29
- Rep Power
- 0
how to handle files?
hi,
i dealt with ".txt" files in java.
can some one please tell me how to deal with binary files(such as image or audio or a office document etc.) or at least refer some good tutorial. i searched the net, all they says about streaming.what the streaming is all about?
i am waiting for a brief and resourceful tutorial.
thanx
- 06-09-2009, 06:47 PM #2
Depends what the file is and what you want to do with it. Image? Audio? Office document?
Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 06-10-2009, 02:44 AM #3
Member
- Join Date
- Feb 2009
- Posts
- 29
- Rep Power
- 0
thanx for your reply.
actually i intend to upload a file from a client machine to server, and i have worked with ".txt" files(using the readLine() method to read from the source file and write it down in another .txt file in the server).
but the same can't be applied to a image file or a audio file(i tried to do but it is not useful). so my question is what is the idea behind general file handling?
- 06-10-2009, 03:25 AM #4
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
Well, you could read them as ImageIcon's or the audio equivalent (Sound API is not my strong point) and send/read those using Object streams, but I'm not sure how you would go about writing them to the server.
If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
- 06-10-2009, 05:26 AM #5
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
For general binary files, the easiest approach is to use FileInputStream and FileOutputStream.
For example, the following will copy from one file to another:
Java Code:FileInputStream in = new FileInputStream(new File("inFile.bin")); FileOutputStream out = new FileOutputStream(new File("outFile.bin")); byte[] bytes = new byte[65536]; try { while(true) { int numRead = in.read(bytes); if(numRead == -1) break; out.write(bytes, 0, numRead); } } catch (IOException ioEx) { ... do something } finally { try { in.close(); } catch (Throwable t) {} try { out.close(); } catch (Throwable t) {} }
- 06-10-2009, 08:39 PM #6
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
Similar Threads
-
How to handle socket Exception
By mayank0512 in forum NetworkingReplies: 14Last Post: 12-21-2010, 11:31 PM -
handle FileNotFoundException
By minifish in forum New To JavaReplies: 13Last Post: 11-08-2008, 02:01 AM -
Handle exception manually
By John_28 in forum New To JavaReplies: 2Last Post: 06-05-2008, 11:26 AM -
Better way to handle exceptions
By javaplus in forum Advanced JavaReplies: 2Last Post: 01-16-2008, 06:47 PM -
how to handle exceptions
By paty in forum Advanced JavaReplies: 2Last Post: 08-05-2007, 04:17 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks