Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ReadFileExample {
- public static void main(String[] args) {
- File file = new File("Path of the file");
- File NewFile = null;
- FileInputStream fis = null;
- FileOutputStream fop = null;
- try {
- fis = new FileInputStream(file);
- int content;
- while ((content = fis.read()) != -1) {
- // check if it is an image starter sequence
- if(content.equals(STARTER SEQUENCE)) {
- //if it is create a new file and start spitting the next bytes there, if not, to the current one
- NewFile = new File("Path where to save it");
- fop = new FileOutputStream(NewFile);
- // if file doesnt exists, then create it
- if (!file.exists()) {
- file.createNewFile();
- }
- }
- if (fop != null) {
- // get the content in bytes
- byte[] contentInBytes = content.getBytes();
- fop.write(contentInBytes);
- fop.flush();
- fop.close();
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- if (fis != null)
- fis.close();
- } catch (IOException ex) {
- ex.printStackTrace();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement