Guest User

Untitled

a guest
May 17th, 2012
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.76 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.StringTokenizer;
  3. import java.util.logging.Level;
  4. import java.util.logging.Logger;
  5.  
  6. public class main {
  7.     public static void main(String[] args) {
  8.         // The file we want to save.
  9.         File f = new File("build.xml");
  10.         try {
  11.             // Make it into a byte array first
  12.             FileInputStream fis = new FileInputStream(f);
  13.             ByteArrayOutputStream bos = new ByteArrayOutputStream();
  14.             byte[] buf = new byte[1024];
  15.             try {
  16.                 for(int readNum; (readNum = fis.read(buf)) != -1;) {
  17.                     bos.write(buf, 0, readNum);
  18.                     System.out.println("read " + readNum + " bytes,");
  19.                 }
  20.                 StringBuilder s = new StringBuilder();
  21.                 // Now we simulate making it into a String, for easier storage
  22.                 // in a database.
  23.                 for(byte b : buf) {
  24.                     // for debugging
  25.                     s.append(b).append(",");
  26.                     System.out.print(b +",");
  27.                 }
  28.                 // Now we want to retrieve the file from the database as a string
  29.                 File someFile = new File("build2.xml");
  30.                 FileOutputStream fos = new FileOutputStream(someFile);
  31.                 // We count how many bytes there are in this string.
  32.                 // One byte per Token.
  33.                 StringTokenizer st = new StringTokenizer(s.toString(),",");
  34.                 buf = new byte[st.countTokens()];
  35.                 int i = 0;
  36.                 StringBuilder t = new StringBuilder();
  37.                 // Now we parse out all Bytes from the string, and put them into
  38.                 // the prepared byte array.
  39.                 while(st.hasMoreTokens()) {
  40.                     byte b = Byte.parseByte(st.nextToken());
  41.                     System.out.print(b + ",");
  42.                     buf[i] = b;
  43.                     i++;
  44.                     // for debugging
  45.                     t.append(b).append(",");
  46.                 }
  47.                 // Here I print true if both strings are exactly the same
  48.                 // which they should be, which means that the bytes are intact
  49.                 // before and after conversion.
  50.                 System.out.println("\n" +(t.toString().equals(s.toString()) ? true : false));
  51.                 // Here we would make the physical file on the machine.
  52.                 fos.write(buf);
  53.                 fos.flush();
  54.                 fos.close();
  55.             } catch (IOException ex) {
  56.                 Logger.getLogger(main.class.getName()).log(Level.SEVERE, null, ex);
  57.             }
  58.         } catch (FileNotFoundException ex) {
  59.             Logger.getLogger(main.class.getName()).log(Level.SEVERE, null, ex);
  60.         }
  61.        
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment