GBK
编码和解码
IO流分类
IO流的分类
IO流的体系
字节流
FileInputStream(文件字节输入流)
文件复制
public class copyfile {
public static void main(String[] args) throws IOException {
//1.定义一个字节流输出流,指定读取那个文件
FileInputStream fis = new FileInputStream("D:/a.txt");
//定义一个字节输入流,指定写到什么位置
FileOutputStream fos = new FileOutputStream("D:/bbb");
//读
byte[] bytes = new byte[1024];//定义一个字节数组,存储每次读到的内容
int len;//定义一个变量len,存储读到的长度
while ((len = fis.read(bytes)) != -1) {
fos.write(bytes, 0, len);
}
//释放资源
fis.close();
fos.close();
}
}
字节流-加密&解密
try-with-resource
字符流
FileReader(文件字符输入流)
public class fileTest {
public static void main(String[] args) throws IOException {
//创建字符输入流对象
FileReader fr = new FileReader("day01/a.txt");
//读 -58 -124 -123
//read():一次读一个字符,返回值就是这个字符对应的字节
int read =fr.read();
System.out.println((char)read);
//释放资源
fr.close();
}
}
public class fileTest {
public static void main(String[] args) throws IOException {
//创建字符输入流对象
FileReader fr = new FileReader("day01/a.txt");
char[] chars=new char[1024];
//读 -58 -124 -123
//read():一次读一个字符,返回值就是这个字符对应的字节
/* int read;
while ((read= fr.read())!=-1)
{
System.out.println((char)read);
}*/
int len;//用来存储每次读到的字符长度
while ((len=fr.read(chars))!=-1)
{
//将字符数组中的内容拼接成一个字符串。读到多少个字符,就拼接几个字符
String s =new String(chars,0,len);
System.out.println(s);
}
//释放资源
fr.close();
}
}
FileWriter(文件字符输出流)
public class fileWriter {
public static void main(String[] args) throws IOException {
//创建一个字符输出流,指定向那个文件中写数据
//注意:
//1.如果文件不存在,会自动地帮我们创建
//2.创建对象时,打开文件,清空文件
//3.如果想要追加的方式写数据,使用两个参数的构造
FileWriter fw = new FileWriter("day01/a.txt",true);
//写
fw.write("我爱刘亦菲");//直接写一个字符串
fw.write("\r\n");//换行
fw.write(97);//如果给一个字节,写数据展示的是该字节对应的字符
String s="我爱中国......";
fw.write(s,0,4);//直接写一个字符串的一部分
char [] chars={'我','爱','你'};
fw.write(chars);//直接写一个字符数组
fw.write(chars,0,2);//直接写一个字符数组
//释放资源
fw.close();
}
}
字节缓冲流
字节缓冲流的作用
BufferedWriter(字符缓冲输出流)
转换流(了解下面 )
打印流 PrintStream/PrintWriter
序列化流
IO框架
总结
1.基本字节流-文件复制
文件上传、下载
文件复制
加密、解密