FileInputStream:字节输入流读取
注意:字节输入流,到-1就是文件内容的末尾
//FileInputStream字节输入流读取
public class FileInputStreamDemo5 {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("f:\\test\\ps.txt");
/*fis.read()读数据
by=fis.read()把读取到的数据赋值给变量by
!=1判断by的值是不是等于负1
* */
int by;
while((by=fis.read())!=-1){
System.out.println((char)by);
}
fis.close();
}
}
6、文件复制
注意:其实就是文件读取时,同时再写入文件
//文件复制:其实就是文件读取时,同时再写入文件
public class FileInputStreamDemo6 {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("f:\\test\\ps.txt");
FileOutputStream fos = new FileOutputStream("f:\\test\\ios.txt");
int by;
while((by=fis.read())!=-1){
fos.write(by);
}
fis.close();
fos.close();
}
}
7、读取字节数组
注意:数组的字节长度通常是1024及其倍数
//读取字节数组
public class FileInputStreamDemo7 {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("f:\\test\\ios.txt");
byte[] bytes = new byte[1024];//长度一般设置为1024及其倍数
int len;
while((len=fis.read(bytes))!=-1){
System.out.print(new String(bytes,0,len));
}
fis.close();
}
}
8、复制图片案例
注意:读取字节的同时,同时存入字节
//读取和写入图片
public class FileInputStreamDemo8 {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("f:\\test\\img\\12.png");
FileOutputStream fos = new FileOutputStream("f:\\test\\image\\12.png");
byte[] bytes = new byte[1024];
int len;
while((len=fis.read(bytes))!=-1){
fos.write(bytes,0,len);
}
fis.close();
fos.close();
}
}
9、字节缓冲流
//字节缓冲流
public class FileInputStreamDemo9 {
public static void main(String[] args) throws IOException {
//创建字节缓冲区输出流
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("f:\\test\\ps.txt"));
bos.write("hello\r\n".getBytes());
bos.write("world\r\n".getBytes());
bos.close();
//创建字节缓冲区输入流
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("f:\\test\\ps.txt"));
//第一方式,读取单个字节
int by;
while((by=bis.read())!=-1){
System.out.print((char)by);
}
//第二种方式读取字节数组
byte[] bys = new byte[1024];
int len;
while((len=bis.read(bys))!=-1){
System.out.println(new String(bys,0,len));
}
bis.close();
}
}
10、案例:复制视频
//四种字节流方式读写视频
public class FileInputStreamDemo10 {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("f:\\test\\vadio1\\2.mp4");
FileOutputStream fos = new FileOutputStream("f:\\test\\vadio2\\2.mp4")*/;
//第一种:单个字节流
int by;
while((by=fis.read())!=-1){
fos.write(by);
}
//第二种:单个字节数组
byte[] bytes = new byte[1024];
int len;
while((len=fis.read(bytes))!=-1){
fos.write(bytes,0,len);
}
fis.close();
fos.close();
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("f:\\test\\vadio1\\3.mp4"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("f:\\test\\vadio2\\3.mp4"));
//第三种:字节缓冲区单个字节
int by;
while((by=bis.read())!=-1){
bos.write(by);
}
//第四种:字节缓冲区数组
byte[] bytes = new byte[1024];
int len;
while((len=bis.read(bytes))!=-1){
bos.write(bytes,0,len);
}
bis.close();
bos.close();
}
}
11、字符串编码与解码
编码格式与解码格式需要对应
UTF-8:三个字符表示一个汉字
GBK: 两个字符表示一个汉字
//字符串编码与解码
public class StringDemo1 {
public static void main(String[] args) throws UnsupportedEncodingException {
String s = "中国";
/* 第一种方式编码
byte[] bys1 = s.getBytes();//系统默认编码格式UTF-8
System.out.println(Arrays.toString(bys1));
//第二种方式编码
byte[] bys2 = s.getBytes("UTF-8");//指定UTF-8格式:一个汉字3个字符
System.out.println(Arrays.toString(bys2));
//第三种方式编码
byte[] bys3 = s.getBytes("GBK");//指定GBK格式:一个汉字2个字符
System.out.println(Arrays.toString(bys3));*/
//解码第一种方式:系统默认
byte[] bys1 = s.getBytes();
String s1 = new String(bys1);
System.out.println(s1);
//解码第二种方式:指定utf-8
byte[] bys2 = s.getBytes();//系统默认编码就是utf-8
String s2 = new String(bys2, Charset.forName("utf-8"));
System.out.println(s2);
//解码第三种方式:指定GBK
byte[] bys3 = s.getBytes("gbk");//编码GBK
String s3 = new String(bys3, Charset.forName("gbk"));//解码GBK
System.out.println(s3);
}
}