1、根据不同的文件内容选择不同的操作类
文本文件选ReaderWriter
图片、视频 inputStreamoutputStream
2、要考虑源文件的编码格式,例如源文件是以GBK编码的,要在解码时用GBK
测试代码:
文本文件内容及格式:
代码:
public class Test1 { public static void main(String[] args) throws IOException { File file = new File("F://1.txt"); FileInputStream inputStream = new FileInputStream(file); byte[] bytes = new byte[1024]; StringBuffer stringBuffer = new StringBuffer(); for (int n;(n=inputStream.read(bytes))!=-1;){ stringBuffer.append(new String(bytes,0,n,"UTF-8")); } System.out.println(stringBuffer.toString()); } }