Guest User

Untitled

a guest
Sep 18th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. Printing a single and specific method to a file in Java
  2. // save object to file
  3. ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("/tmp/file")));
  4. oos.writeObject(myArray); // where myArray is String[]
  5.  
  6. // load object from file
  7. ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("/tmp/file")));
  8. String[] read = (String[]) ois.readObject();
  9.  
  10. import java.io.File;
  11. import java.io.FileInputStream;
  12. import java.io.FileNotFoundException;
  13. import java.io.FileOutputStream;
  14. import java.io.IOException;
  15. import java.io.ObjectInputStream;
  16. import java.io.ObjectOutputStream;
  17. import java.util.Arrays;
  18.  
  19. public class TestSerialization {
  20.  
  21. public static void main(final String[] array) throws FileNotFoundException, IOException, ClassNotFoundException {
  22. // save object to file
  23. ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("/tmp/file")));
  24. oos.writeObject(array); // where myArray is String[]
  25.  
  26. // load object from file
  27. ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("/tmp/file")));
  28. String[] read = (String[]) ois.readObject();
  29.  
  30. System.out.println(Arrays.toString(read));
  31. }
  32.  
  33. }
  34.  
  35. // create a new file with specified file name
  36. FileWriter fw = new FileWriter("myFile.log");
  37.  
  38. // create the IO strem on that file
  39. BufferedWriter bw = new BufferedWriter(fw);
  40.  
  41. // write a string into the IO stream
  42. bw.out("my log entry");
  43.  
  44. // don't forget to close the stream!
  45. bw.close();
Add Comment
Please, Sign In to add comment