Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. public class ReadFileExample {
  2.  
  3. public static void main(String[] args) {
  4.  
  5. File file = new File("Path of the file");
  6. File NewFile = null;
  7. FileInputStream fis = null;
  8. FileOutputStream fop = null;
  9.  
  10. try {
  11. fis = new FileInputStream(file);
  12.  
  13. int content;
  14. while ((content = fis.read()) != -1) { //While you didn't reach EOF
  15. // check if it is an image starter sequence
  16. if(content.equals(STARTER SEQUENCE)) {
  17. //if it is create a new file and start spitting the next bytes there, if not, to the current one
  18. if (NewFile != null) //If i was'previously writing to a file close it
  19. NewFile.close();
  20. NewFile = new File("Path where to save it");
  21. // if file doesnt exists, then create it
  22. if (!NewFile.exists()) {
  23. NewFile.createNewFile();
  24. }
  25. fop = new FileOutputStream(NewFile);
  26. }
  27. if (fop != null) { //Only if you have an open file you can write to it!
  28. // get the content in bytes
  29. byte[] contentInBytes = content.getBytes();
  30.  
  31. fop.write(contentInBytes);
  32. fop.flush();
  33. fop.close();
  34. }
  35.  
  36. }
  37.  
  38. } catch (IOException e) {
  39. e.printStackTrace();
  40. } finally {
  41. try {
  42. if (fis != null)
  43. fis.close();
  44. } catch (IOException ex) {
  45. ex.printStackTrace();
  46. }
  47. }
  48. }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement