· 9 years ago · Nov 04, 2016, 01:26 PM
1// read from file
2
3// read bytes
4 BufferedInputStream in = new BufferedInputStream(new FileInputStream("input.txt"));
5 while (in.available()!=0){
6 in.read();
7 }
8
9// read characters (line by line)
10
11 BufferedReader in = new BufferedReader(new FileReader("input.txt"));
12 String temp;
13 while ((temp = in.readLine())!=null){
14 // temp variable hold the current line
15 }
16///////////////////////////////////////////////////////
17
18// write on file
19
20// write bytes
21
22 BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("output.txt"));
23 out.write(int_val);
24
25// write lines
26
27 BufferedWriter out = new BufferedWriter(new FileWriter("output.txt"));
28 out.write(str);
29
30
31///////////////////////////////////////////////////////
32
33// Read and write arabic characters
34
35 InputStreamReader in = new InputStreamReader(new FileInputStream("in.txt"),"utf-8");
36
37 OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("out.txt"), "utf-8");
38
39///////////////////////////////////////////////////////
40
41// Compress files
42
43// Compress only 1 file :
44
45// There is 2 methods
46
47// 1- Using ZipOutputStream
48
49 BufferedInputStream in = new BufferedInputStream(new FileInputStream("sample.txt"));
50 ZipOutputStream out = new ZipOutputStream(new FileOutputStream("compressed.zip"));
51 ZipEntry e = new ZipEntry("sample.txt");
52 out.putNextEntry(e);
53 while (in.available()!=0){
54 out.write(in.read());
55 }
56 out.closeEntry();
57 out.close();
58
59// 2- Using GZIPOutputStream
60
61 BufferedInputStream in = new BufferedInputStream(new FileInputStream("sample.txt"));
62 GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream("compressed.gz"));
63 while (in.available()!=0){
64 out.write(in.read());
65 }
66 out.close();
67
68
69// Extract only 1 file :
70
71// There is 2 methods
72
73// 1- Using ZipInputStream
74
75 ZipInputStream in = new ZipInputStream(new FileInputStream("compressed.zip"));
76 BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(in.getNextEntry().getName()));
77 while (in.available()!=0){
78 out.write(in.read());
79 }
80 out.close();
81
82// 2- Using GZIPInputStream
83
84 GZIPInputStream in = new GZIPInputStream(new FileInputStream("compressed.gz"));
85 BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("sample.txt"));
86 while (in.available()!=0){
87 out.write(in.read());
88 }
89 out.close();
90
91// WE CAN'T USE GZIPInputStream OR GZIPOutputStream TO COMPRESS / EXTRACT MULTIPLE FILES
92
93// Compress multiple files :
94
95 String [] files ={"file1.txt","file2.txt"};
96 BufferedInputStream in = null;
97 ZipOutputStream out = new ZipOutputStream(new FileOutputStream("compressed.zip"));
98 ZipEntry e = null;
99 for(int i=0;i<files.length;i++){
100 in = new BufferedInputStream(new FileInputStream(files[i]));
101 e = new ZipEntry(files[i]);
102 out.putNextEntry(e);
103 while(in.available()!=0){
104 out.write(in.read());
105 }
106 out.closeEntry();
107 in.close();
108 }
109 out.close();
110
111// Extract multiple files
112 BufferedOutputStream out = null;
113 ZipInputStream in = new ZipInputStream(new FileInputStream("compressed.zip"));
114 ZipEntry e = in.getNextEntry();
115 while(e!=null){
116 out = new BufferedOutputStream(new FileOutputStream(e.getName()));
117 while(in.available()!=0){
118 out.write(in.read());
119 }
120 out.close();
121 e = in.getNextEntry();
122 }
123 in.close();
124
125///////////////////////////////////////////////////////
126
127// Encryption
128 BufferedInputStream in= new BufferedInputStream(new FileInputStream("input.txt"));
129 FileOutputStream out = new FileOutputStream("Encrypted.txt");;
130 String keyStr ="87654321";
131
132 DESKeySpec k = new DESKeySpec(keyStr.getBytes());
133 SecretKeyFactory skey = SecretKeyFactory.getInstance("DES");
134 SecretKey key = skey.generateSecret(k);
135
136 Cipher c =Cipher.getInstance("DES");;
137 c.init(Cipher.ENCRYPT_MODE, key);
138 CipherInputStream cin = new CipherInputStream(in, c);
139 while(true){
140 int b=cin.read();
141 if(b==-1)
142 break;
143 out.write(b);
144 }
145
146// Decryption
147 BufferedInputStream in= new BufferedInputStream(new FileInputStream("Encrypted.txt"));
148 FileOutputStream out = new FileOutputStream("output.txt");;
149 String keyStr ="87654321";
150
151 DESKeySpec k= new DESKeySpec(keyStr.getBytes());
152 SecretKeyFactory skey=SecretKeyFactory.getInstance("DES");
153 SecretKey key=skey.generateSecret(k);
154
155 Cipher c =Cipher.getInstance("DES");
156 c.init(Cipher.DECRYPT_MODE, key);
157 CipherInputStream cin = new CipherInputStream(in, c);
158 while(true){
159 int b=cin.read();
160 if(b==-1)
161 break;
162 out.write(b);
163 }
164
165// MessageDigest
166
167 BufferedInputStream in = new BufferedInputStream(new FileInputStream("input.txt"));
168 MessageDigest md= MessageDigest.getInstance("md5");
169 DigestInputStream din =new DigestInputStream(in, md);
170 while(din.available()!=0) din.read();
171 byte[] hash= din.getMessageDigest().digest();
172 for(int i=0;i<hash.length;++i){
173 System.out.print(hash[i]>=0?Integer.toHexString(hash[i]):Integer.toHexString(hash[i]+256));
174 }