1 36 37 43 44 45 50 51 import java.awt.*; 52 import java.awt.event.*; 53 import java.io.*; 54 import java.lang.*; 55 import java.applet.*; 56 import javax.swing.*; 57 import javax.jnlp.*; 58 59 public class randomFile 60 { 61 FileOpenService fos; 62 JNLPRandomAccessFile raf; 63 FileContents fc; 64 static int count=0; 65 JTextArea textArea; 66 java.awt.Container contentPane; 67 JFrame t; 68 69 public randomFile() 70 { 71 t = new JFrame("JNLPRandomAccessFile API demo"); 72 contentPane = t.getContentPane(); 73 contentPane.setLayout( new FlowLayout() ); 74 contentPane.setBackground(Color.white); 75 textArea = new JTextArea(); 76 contentPane.add(textArea); 77 textArea.setText("Demonstrating usage of random access file. \n"); 78 textArea.append("It behaves like a large array of bytes stored in the file system \n"); 79 textArea.append("Pick a file to open on the local system \n"); 80 t.addWindowListener(new WindowAdapter(){ 81 public void windowClosing(WindowEvent e) 82 { 83 destroy(); 84 t.setVisible(false); 85 System.exit(0); 86 } 87 88 }); 89 t.pack(); 90 t.setSize(500,400); 91 t.setVisible(true); 92 93 94 96 try { 97 fos = (FileOpenService)ServiceManager.lookup("javax.jnlp.FileOpenService"); 98 } catch (UnavailableServiceException e) { 99 fos = null; 100 } 101 102 if (fos != null) { 103 try { 104 fc = fos.openFileDialog(null, null); 106 107 if (fc != null) { 109 long grantedLength = fc.getLength(); 110 if (grantedLength + 1024 > fc.getMaxLength()) { 111 grantedLength = fc.setMaxLength(grantedLength + 1024); 114 } 115 116 raf = fc.getRandomAccessFile("rw"); 118 textArea.append("File selected : "); 119 textArea.append(fc.getName()); 120 textArea.append("\n"); 121 textArea.append("Created a random access file in read/write mode.... \n"); 122 try 123 { 124 Thread.sleep(2000); 125 } catch (InterruptedException e){ 126 } 129 130 } 131 } catch (Exception e) { 132 e.printStackTrace(); 133 } 134 } 135 } 136 137 138 public void write() 139 { 140 141 String msg="JAVA WEB START CAN STUN YOU.. "; 142 143 if (fos != null && fc != null) { 144 try { 145 textArea.append("Seek to the begining of the file for write... \n"); 146 147 try 148 { 149 Thread.sleep(2000); 150 } catch (InterruptedException e){ 151 } 154 155 raf.seek(0); 157 158 162 textArea.append("Writing a string to the file using UTF-8 encoding... \n"); 163 164 try 165 { 166 Thread.sleep(2000); 167 } catch (InterruptedException e){ 168 } 171 172 raf.writeUTF("CONGRATS !!. WRITE & READ PERFORMED SUCCESSFULLY "); 173 raf.seek(raf.length()-1); 174 175 textArea.append("Seek to end of the file for next write... \n"); 176 try 177 { 178 Thread.sleep(2000); 179 } catch (InterruptedException e){ 180 } 183 184 190 191 textArea.append("Writing another string as a sequence of bytes \n"); 192 try 193 { 194 Thread.sleep(2000); 195 } catch (InterruptedException e){ 196 } 199 200 raf.writeBytes("\n" + msg + count++); 201 } catch (Exception e) { 202 e.printStackTrace(); 203 } 204 } 205 206 } 207 208 public void destroy() { 209 if (fos != null && fc != null) { 210 try { 211 raf.close(); 213 } catch (Exception e) { 214 e.printStackTrace(); 215 } 216 } 217 } 218 219 public void read() 220 { 221 if (fos != null && fc != null) { 222 try 223 { 224 225 226 textArea.append("Now reading whatever was written.. \n"); 227 textArea.append("Reading one line at a time from the begining of the File.. \n"); 228 229 try 230 { 231 Thread.sleep(2000); 232 } catch (InterruptedException e){ 233 } 236 237 238 243 textArea.append("\n"); 244 raf.seek(0); 245 String line = raf.readUTF(); 246 while (line != null) 247 { 248 textArea.append(line); 249 textArea.append("\n"); 250 line = raf.readLine(); 251 252 } 253 254 textArea.append("\n"); 255 try 256 { 257 Thread.sleep(2000); 258 } catch (InterruptedException e){ 259 } 262 263 textArea.append("Thanks for choosing Java Web Start.. \n"); 264 265 } catch (Exception e) { 266 e.printStackTrace(); 267 } 268 } 269 } 270 271 public static void main(String args[]) 272 { 273 274 randomFile randomfile = new randomFile(); 275 randomfile.write(); 276 randomfile.read(); 277 278 } 279 } 280 | Popular Tags |