• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Writing non text file

 
Ranch Hand
Posts: 1325
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all, please Use Code Tags. I've added them for you this time.

Second of all, what is your question? All I see is a piece of code. I have no idea what it means.


After reading that code, let me give you some pointers:
Don't use Hashtable unless you need synchronization - use HashMap instead. And even if you need synchronization, wrapping a HashMap using Collections.synchronizedMap will be better. Hashtable is legacy code that does not allow null keys and values.
Also, declare as an interface (Map) if possible:

This also holds for the file Map: if you don't need static fields to be mutable, make them final. It will prevent you from accidentilly overwriting them.

Don't cast to byte at this point. Although the file only contains bytes, read() returns an int for a reason. Since all byte values are valid return values, read() returns not a value between -128 and 127 but between -1 and 255. All but -1 are valid.
By casting to byte at this point, you will turn the valid return value 255 into an invalid return value of -1, where you later check against. So please keep it as an int until the moment you really need it to be a byte, and that is at the moment you put it into the Map:

Use Map's keySet() method instead. If you need to iterate over it you can retrieve that set's iterator: Furthermore, by using keySet(), you can use the for-each loop:

After these tips I found out there is one major, major flaw in your logic. Hashtable and HashMap do not maintain any known ordering. Therefore, you retrieve the elements in some random order; certainly not ordered by key. (If you do you're very very lucky). Use LinkedHashMap or TreeMap instead.

Or, and this is better, use a List<Byte> (with implementation ArrayList<Byte>) instead. That way you have explicit ordering, and you can use the List's indexing as your index. Although you won't need it:
 
shawn peter
Ranch Hand
Posts: 1325
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i tried to read and write jpg file.but after writing it can't open.it may corrupted.this happens for all non text files.
what is the reason.is there any wrong in this code.please help me

1. package write;
2. import java.util.*;
3. import java.io.*;
4.
5. class Read{
6. private static Hashtable<Integer,Byte> file = new Hashtable<Integer, Byte>();
7. private static String path="g:\\write\\PICTURE.JPG";
8.
9.
10. private void reader()throws IOException
11. {
12. File f;
13. f=new File("g:\\read\\PICTURE.JPG");
14.
15. if(!f.exists()&& f.length()<0)
16. System.out.println("The specified file is not exist");
17.
18. else{
19. long file_size = f.length();
20. System.out.println("Size of the file : " + file_size);
21.
22.
23. FileInputStream finp=new FileInputStream(f);
24.
25. byte b=(byte)finp.read();
26.
27. int i=0;
28. while(b!=-1)
29. {
30.
31. file.put(i,b); //put to hashtable
32.
33.
34. b=(byte)finp.read(); //get next byte
35.
36. i++;
37. }
38. finp.close();
39.
40. }
41.
42. }
43.
44. private void writer()throws IOException
45. {
46. File f=new File(path);
47. FileOutputStream fop=new FileOutputStream(f);
48.
49. if(f.exists()){
50. String str="This data is written through the program";
51.
52. Enumeration<Integer> enu = file.keys();//get all keys
53. while(enu.hasMoreElements())
54. {
55. int key = enu.nextElement();
56. byte b=file.get(key);
57.
58. fop.write(b);
59.
60. System.out.println("The data has been written");
61.
62. }
63. fop.flush();
64. fop.close();
65.
66. }
67. }
68.
69. public static void main(String[] args) {
70. try{
71. Read rd=new Read();
72. rd.reader();
73. rd.writer();
74. }catch(Exception e)
75. {
76. e.printStackTrace();
77. }
78. }
79. }
 
shawn peter
Ranch Hand
Posts: 1325
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
THANK for the answer sir.I learn a lot from your answer.
 
Rob Spoor
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Again, please Use Code Tags.


After reading your other thread, it seems that your problem is simply copying data from one file to another.

This is how it is usually done in one single method:
doing it byte by byte (using read() instead of read(byte[])) is usually slower.
 
Can you really tell me that we aren't dealing with suspicious baked goods? And then there is this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic