简体   繁体   中英

Reading from one file using FileInputStream and writing to another file using FileOutputStream

I was trying to read from one file and write the bytes read to another file using the classes specified in the title.I successfully did it but while i was trying to try different things i came across a problem which i do not understand.

Here is the code

import java.io.*;
public class FileInputStreamDemo {

    public static void main(String[] args)
            throws Exception {
        // TODO Auto-generated method stub
        int size;
        InputStream f = new
                FileInputStream("G:/Eclipse Workspace/FileInputStream Demo/src/FileInputStreamDemo.java");

        System.out.println("Total available bytes: " + (size = f.available()));
    /*int n=size/40;
    System.out.println("first " + n + " bytes of file one read() at a time");

    for (int i=0;i<n;i++)
    {
        System.out.print((char) f.read());
    }
    System.out.println("\n Still available: " + f.available());
    System.out.println("reading the next" + n + "with one read(b[])");
    byte b[] = new byte[n]; */
    /*for(int i=0;i<size;i++)
    {
        System.out.print((char) f.read());
    }*/
        OutputStream f1 = new
                FileOutputStream("G:/Eclipse Workspace/FileInputStream Demo/test.txt");
        for (int count = 0; count < size; count++) {
            f1.write(f.read());
        }
        for (int i = 0; i < size; i++) {
            System.out.print(f.read());
        }

        f.close();
        f1.close();
    }
}

The problem that i am talking about is that when i first read from the FileInputStream object f ie f.read() and write it to the f1 ie FileOutputStream object it goes on to do what it is meant to do ,but when i try to read it again it returns -1. why so ?

Use RandomAccessFile and seek(0) method to come back at the beginning.

RandomAccessFile file = new RandomAccessFile(new File("G:/Eclipse Workspace/FileInputStream Demo/src/FileInputStreamDemo.java"), "r");

Here is sample code:

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;

public class FileInputStreamDemo {

    public static void main(String[] args) throws Exception {
        long size;
        File file = new File("D:/Workspace/JavaProject/src/com/test/FileInputStreamDemo.java");
        RandomAccessFile f = new RandomAccessFile(file, "r");

        System.out.println("Total available bytes: " + (size = file.length()));
        OutputStream f1 = new FileOutputStream(new File(
                "D:/Workspace/JavaProject/resources/test.txt"));
        for (int count = 0; count < size; count++) {
            f1.write(f.read());
        }
        f.seek(0);
        for (int i = 0; i < size; i++) {
            System.out.print((char)f.read());
        }

        f.close();
        f1.close();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM