Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 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) {
  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. NewFile = new File("Path where to save it");
  19. fop = new FileOutputStream(NewFile);
  20. // if file doesnt exists, then create it
  21. if (!file.exists()) {
  22. file.createNewFile();
  23. }
  24. }
  25. if (fop != null) {
  26. // get the content in bytes
  27. byte[] contentInBytes = content.getBytes();
  28.  
  29. fop.write(contentInBytes);
  30. fop.flush();
  31. fop.close();
  32. }
  33.  
  34. }
  35.  
  36. } catch (IOException e) {
  37. e.printStackTrace();
  38. } finally {
  39. try {
  40. if (fis != null)
  41. fis.close();
  42. } catch (IOException ex) {
  43. ex.printStackTrace();
  44. }
  45. }
  46. }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement