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. }