IO流:用来传输
第一步:造文件,以及熟悉基础的文件类方法
public class Demo1 {
public static void main(String[] args) throws IOException {
//创建一个文件
File file = new File("D:\\kxbb99\\IO\\c.txt");
boolean newFile = file.createNewFile();
//若没有绝对地址,将会创建到项目里
File file7 = new File("d.txt");
boolean newFile1 = file7.createNewFile();
//创建一个文件夹
File file1 = new File("D:\\kxbb99\\IO\\kxbb");
boolean mkdir = file1.mkdir();
//创建一组文件夹套文件夹
File file2 = new File("D:\\kxbb99\\IO\\wqq\\zbk");
boolean mkdirs = file2.mkdirs();
//删除一个文件夹或者文件
File file3 = new File("b.txt");
boolean delete = file3.delete();
//判断是否为文件
File file4 = new File("D:\\kxbb99\\IO\\b.txt");
System.out.println(file4.isFile());
//判断是否为文件夹
File file5 = new File("D:\\kxbb99\\IO\\kxbb");
System.out.println(file5.isDirectory());
//判断是否存在
File file6 = new File("D:\\kxbb99\\IO\\kxbb");
System.out.println(file6.exists());
//获取绝对路径
File file8 = new File("w.txt");
System.out.println(file8.getAbsolutePath());
//获取相对路径
File file9 = new File("D:\\kxbb99\\IO\\c.txt");
System.out.println(file9.getPath());
//获取名称
File file10 = new File("D:\\kxbb99\\IO\\c.txt");
System.out.println(file10.getName());
//获取文件内容长度字节数,若不存在此文件则返回0L
File file11 = new File("D:\\kxbb99\\IO\\c.txt");
System.out.println(file11.length());
//获取一个指定目录下的所有文件和文件夹的File数组
File file12 = new File("D:\\kxbb99\\IO");
File[] files = file12.listFiles();
for (File file13 : files) {
System.out.println(file13);
}
//获取一个指定目录下的所有文件和文件夹的名称数组《String》
String[] list = file12.list();
for (String s : list) {
System.out.println(s);
}
//判断是否有后缀为.txt形式的文件如果有则输出他的绝对路径
for (File file13 : files) {
String name = file13.getName();
if (name.endsWith(".txt")){
System.out.println(file13.getAbsolutePath());
}
}
}
}
IO流进行传输
字符适合txt文件
字节适合视频,图片等文件
1,操作数据单位: | 字节流,字符流 |
2,数据的流向: | 输入流,输出流 |
3,流的角色: | 节点流,处理流 |
常用的IO流如下
抽象基类 | 节点流(文件流) | 缓冲流(处理流的一种) |
InputStream | FileInputStream:字节 | BufferedInputStream |
OutputStream | FileOutputStream:字节 | BufferedOutputStream |
Reader | FileReader:字符流读 | BufferedReader |
Writer | FileWriter:字符流写 | BufferedWriter |
有异常用try catch捕获(ctrl+alt+t)
FileInputStream:字节读取流
public class Demo3 {
public static void main(String[] args) throws Exception{
//造文件
File file = new File("d.txt");
//造流
FileInputStream fis = new FileInputStream(file);
//读数据
byte[] bytes = new byte[1024];
int len=0;
while ((len = fis.read(bytes))!=-1){
String str = new String(bytes,0,len);
System.out.println(str);
}
//记得关闭流,否则内存泄露
fis.close();
}
}
注意
保存文件时编码的区别:
utf-8:英文字符占用一个字节,中文字符占用三个字节。
GBK:英文字符占用一个字节,中文字符占用两个字节。
ANSI:不同的国家和地区采用不同的编码,例如在大陆的本地码,就是GBK编码。
Unocode:每个字符都是占用两个字节!
FileOutputStream:字节输入流
public class Demo4 {
public static void main(String[] args){//不用抛异常直接用try catch
FileInputStream fis = null;
FileOutputStream fos = null;
try {
//1.造流
fis = new FileInputStream("d.txt");
fos = new FileOutputStream("b.txt");
byte[] by = new byte[1024];
int len = 0;
//读取数据
while ((len=fis.read(by))!=-1){
//保存数据
fos.write(by,0,len);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
//关闭流 先用的后关,后用的先关
if (fos!=null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis!=null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
IO流复制字节流:(可对该功能进行包装)(关流有顺序)
public class Demo5 {
public static void main(String[] args) {
long KAI = System.currentTimeMillis();
String a = "D:\\kxbb99\\IO\\c.txt";
String b = "D:\\kxbb99\\IO\\zd.txt";
copy(a,b);
long jie = System.currentTimeMillis();
System.out.println(jie-KAI);
}
public static void copy(String a,String b) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(a);
fos = new FileOutputStream(b);
byte[] by = new byte[1024];
int len = 0;
while ((len=fis.read(by))!=-1){
fos.write(by,0,len);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
//关闭流 先用的后关,后用的先关
if (fos!=null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis!=null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
FileWriter:字符输入流
public class Demo1 {
public static void main(String[] args) throws Exception{
FileWriter fw = new FileWriter("D:\\kxbb99\\IO\\kxbb.txt",true);
//上一行的true表示输入叠加,反之默认为false表示覆盖输入
fw.write("wqqlsp");
fw.write("kxbb99");
fw.close();
}
}
FileReader:字符流读取流
public class Demo2 {
public static void main(String[] args) throws Exception{
FileReader fr = new FileReader("D:\\kxbb99\\IO\\kxbb.txt");
int len = 0;
char[] chars = new char[1024];
while ((len=fr.read(chars))!=-1){
String s = new String(chars,0,len);
System.out.println(s);
}
fr.close();
}
}
字符流文件拷贝案例
public class Demo3 {
public static void main(String[] args) throws Exception{
FileReader fr = new FileReader("D:\\kxbb99\\IO\\kxbb.txt");
FileWriter fw = new FileWriter("D:\\kxbb99\\IO\\kxbb99.txt",true);
char[] chars = new char[1024];
int len = 0;
while ((len= fr.read(chars))!=-1){
fw.write(chars,0,len);
}
fw.close();
fr.close();
}
}
缓冲流:更快(默认8192字节)
IO流根据功能花划分,可以分为:节点流和包装流(处理流)。
节点流: 可以从或向一个特定的地方(节点)读写数据,例如:FileOutputStream、FileInputStream、FileWriter和FileReader等。
包装流:对一个已存在的流的连接和封装,通过所封装的流的功能调用实现数据读写,例如:缓冲流、转换流和合并流等。
BufferedOutputStream:包装字节输入流
注意flush方法的使用
public class Demo4 {
//缓冲流
public static void main(String[] args) throws Exception{
//造流,内部是字节流外部包装一个缓冲流
BufferedOutputStream bos =
new BufferedOutputStream(new FileOutputStream("D:\\kxbb99\\IO\\kxbb.txt"));
//写数据
bos.write("我的妈".getBytes());
//刷新缓冲区
bos.flush();
//关流
bos.close();
}
}
BufferedInputStream:包装字节输出流
public class Demo5 {
public static void main(String[] args) throws Exception {
FileInputStream fis = new FileInputStream("D:\\kxbb99\\IO\\kxbb.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
int len;
byte[] by = new byte[1024];
while ((len=bis.read(by))!=-1){
System.out.println(new String(by,0,len));
}
bis.close();
}
}
字节包装流复制
public class Demo6 {
public static void main(String[] args) throws Exception{
BufferedInputStream bis =
new BufferedInputStream(new FileInputStream("D:\\kxbb99\\IO\\kxbb.txt"));
BufferedOutputStream bos =
new BufferedOutputStream(new FileOutputStream("D:\\kxbb99\\IO\\kxdd.txt"));
int len;
byte[] by = new byte[1024];
while ((len=bis.read(by))!=-1){
bos.write(by,0,len);
}
bos.close();
bis.close();
}
}
BufferedReader:包装字符读取流
注意readLine方法的使用
public class Demo8 {
public static void main(String[] args) throws Exception{
BufferedReader br =
new BufferedReader(new FileReader("D:\\kxbb99\\IO\\kxbb01.txt"));
String line = null;
while ((line=br.readLine())!=null){
System.out.println(line);
}
br.close();
}
}
BufferedWriter:包装字符输入流
注意newLine方法
public class Demo7 {
public static void main(String[] args) throws Exception{
BufferedWriter bw =
new BufferedWriter(new FileWriter("D:\\kxbb99\\IO\\kxbb01.txt"));
for (int i = 0; i <10; i++) {
bw.write("123");
bw.newLine();
bw.flush();
}
bw.close();
}
}
包装字符流复制
public class Demo9 {
public static void main(String[] args){
BufferedReader br = null;
BufferedWriter bw = null;
try {
br = new BufferedReader(new FileReader("D:\\kxbb99\\IO\\kxbb01.txt"));
bw = new BufferedWriter(new FileWriter("D:\\kxbb99\\IO\\kxbb02.txt"));
String a = null;
while ((a=br.readLine())!=null){
bw.write(a);
bw.newLine();
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if (bw!=null){
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (br!=null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
图片复制加密/解密《by[i] = (byte)(by[i]^5)异或运算》
public class Demo10 {
public static void main(String[] args) throws Exception{
FileInputStream fis = new FileInputStream("D:\\kxbb99\\Java\\Java作业包\\e.png");
FileOutputStream fos = new FileOutputStream("D:\\kxbb99\\Java\\Java作业包\\f.png");
int len;
byte[] by = new byte[4];
while ((len=fis.read(by))!=-1){
for (int i = 0; i <len; i++) {
by[i] = (byte)(by[i]^5);
}
fos.write(by,0,len);
}
fos.close();
fis.close();
}
}
练习难题
public class Demo {
//练习:获取文本上字符出现的次数,把数据写入文件
/**
* 思路:
* 1,遍历文本每一个字符
* 2,字符出现的次数存在map中
* 3,把map文件中的数据写入文件
*/
public static void main(String[] args){
FileReader fr = null;
BufferedWriter bw = null;
try {
fr = new FileReader(new File("day12\\aa.txt"));
//1,创建hashmap
Map<Character,Integer> map = new HashMap<>();
//2,遍历每一个字符出现的次数
int count = 0;
while ((count = fr.read()) != -1){
//int 还原char
char c = (char)count;
//判断char是否在map中第一次出现
if (map.get(c) == null){
map.put(c,1);
}else {
map.put(c,map.get(c)+1);
}
}
//3,把map中数据存到文件夹中
//3.1 创建Writer
bw = new BufferedWriter(new FileWriter("day12\\aab.txt"));
//3.2 遍历map 再写入数据
Set<Map.Entry<Character, Integer>> entries = map.entrySet();
for (Map.Entry<Character, Integer> entry : entries) {
switch (entry.getKey()){
case ' ':
bw.write("空格="+entry.getValue());
break;
case '\t': // \t:表示tab 键字符
bw.write("tab键="+entry.getValue());
break;
case '\r': // \r:表示回车
bw.write("回车="+entry.getValue());
break;
case '\n': // \n:表示换行
bw.write("换行="+entry.getValue());
break;
default:
bw.write(entry.getKey()+"="+entry.getValue());
break;
}
bw.newLine();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//4,关闭流
if (bw !=null){
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fr !=null){
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
2.7、转换流(属于字符流)《我觉得没啥用》
java API提供了两个转换流:
转换流在字节流和字符流之间的转换
InputStreamReader:将InputStream转换为Reader
OutputStreamWriter:将Writer转换为OutputStream
很多时候我们使用转换流来处理文件乱码问题,实现编码和解码的功能。
public class Test2 {
public static void main(String[] args) throws Exception {
//1,造File
File file1 = new File("day12\\aa.txt");
File file2 = new File("day12\\aa_ccc.txt");
//2.1,造流
FileInputStream fis = new FileInputStream(file1);
FileOutputStream fos = new FileOutputStream(file2);
//2.2 转换流
InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
OutputStreamWriter osw = new OutputStreamWriter(fos,"GBK");
//3,复制 读写操作
char[] ch = new char[20];
int len;
while ((len = isr.read(ch)) != -1){
osw.write(ch,0,len);
}
//4,关流
osw.close();
isr.close();
}
}