I/O FileInputStream 字节类型文件输入输出流 (汉字显示乱码)
package IOliu; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class FileInputStreamTest { public static void main(String[] args) { //FileInputStream 字节类型的输入流 FileInputStream fis = null; try { fis = new FileInputStream("D:\\2016.txt"); int i = 0 ; //StringBuffer拼接文件内容 StringBuffer sb = new StringBuffer(); //输入流结尾默认为-1 while((i = fis.read())!= -1){ char ch = (char) i; sb.append(ch); } //读取一个字节 System.out.println(sb.toString()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ //fis.close()异常:空指针异常 try { fis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
输出流
package IOliu; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class FileInputStreamTest { public static void main(String[] args) { //FileInputStream 字节类型的输入流 FileInputStream fis = null; FileOutputStream fos = null; try { fis = new FileInputStream("E:\\小霸王游戏机-珍藏84合1 模拟器+84款经典游戏\\1.gif"); fos = new FileOutputStream("E:\\dogcopy.gif"); //StringBuffer拼接文件内容 //StringBuffer sb; int i = 0; //定义比特数组,用来拼接汉字,解决字节乱码问题 //byte[] bArray; //bArray = new byte[1024]; //sb = new StringBuffer(); //输入流结尾默认为-1 while((i = fis.read())!= -1){ fos.write(i); //char ch = (char) i; //sb.append(bArray); } //读取一个字节 //System.out.println(sb.toString()); //System.out.println(new String(bArray)); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ //fis.close()异常:空指针异常 try { fis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }