Engineering Full Stack Apps with Java and JavaScript
Need to copy file using FileInputStream and FileOutputStream
FileInputStream is a low level byte stream, whose constructor takes the name of the file as an argument. FileOutputStream is also another low level byte stream, whose constructor takes the name of the file as an argument.
import java.io.*;
public class FileCopier {
public static void main(String[] args) throws IOException,
InterruptedException {
FileInputStream fin = new FileInputStream("myFile.txt");
FileOutputStream fout = new FileOutputStream("myFileOut.txt", true);
int b = fin.read();
while (b != -1) {
fout.write((char) b);
b = fin.read();
}
fout.close();
fin.close();
}
}
Here we read contents of myFile.txt byte by byte, cast it into character and write it into another file. The read method returns -1 when the end of file is reached.