FileInputStream
FileInputStream循环读取:
public class Test3 {public static void main(String[] args) throws IOException {FileInputStream fis=new FileInputStream("C:\\Users\\小新\\IdeaProjects\\Test\\src\\text");int b;while ((b=fis.read())!=-1){System.out.print((char) b);}//释放资源fis.close();}
}
文件拷贝基本代码(边读边写):
public class Test3 {public static void main(String[] args) throws IOException {//拷贝的路径FileInputStream fis=new FileInputStream("C:\\Users\\小新\\IdeaProjects\\Test\\src\\text");//拷贝到的路径FileOutputStream fos=new FileOutputStream("C:\\Users\\小新\\IdeaProjects\\Test\\src\\copy");//利用while循环边读边写int b;while ((b= fis.read())!=-1){fos.write(b);}fos.close();fis.close();}
}
如果要拷贝大文件,我们可以利用数组,但是数组也会占用内存,所以,我们经量用合适的数组。
public class Test3 {public static void main(String[] args) throws IOException {//拷贝的路径FileInputStream fis=new FileInputStream("C:\\Users\\小新\\IdeaProjects\\Test\\src\\text");//拷贝到的路径FileOutputStream fos=new FileOutputStream("C:\\Users\\小新\\IdeaProjects\\Test\\src\\copy");//利用while循环边读边写int len;byte[] bytes=new byte[1024*1024*5];while ((len=fis.read(bytes))!=-1){fos.write(bytes,0,len);}//释放资源fos.close();fis.close();}
}