Java核心类库-Io流笔记详解(代码实现)

IO

Java.io.File
1.在指定路径下创建新的文件
File file = new File(“D://1.txt”);
Boolean flag = file.createNewFile();
System.out.println(flag?“创建成功”:“创建失败”);

2.在指定路径下创建新的文件夹
File dir = new File(“D://dir”);
boolean flag1 = dir.mkdir();
System.out.println(flag1?“创建成功”:“创建失败”);

3.在指定文件夹生成新的文件的两种不同构造方法
File a = new File(dir,“a.txt”);
a.createNewFile();
File b = new File(“D://dir”,“b.txt”);
b.createNewFile();

文件遍历案例
在这里插入图片描述

IO流概述
在这里插入图片描述

Java.io.FileOutputStream
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream(“D://a.txt”);
//1.write(int b) 将指定的字节写入此文件输出流。
fos.write(66);

//2.write(byte[] b, int off, int len) 将从偏移量 off开始的指定字节数组中的 len字节写入此文件输出流。 
byte[] bytes = {65,66,67,68,69};
fos.write(bytes,2,2);
fos.close();
System.out.println("成功写出");

}

java.io.InputStream
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream(“D://a.txt”);
while (true){
byte b = (byte) fis.read();
if (b==-1){
break;
}
System.out.println(b);
}

byte[] bytes = new byte[10];
int nums = fis.read(bytes);//10
System.out.println(new String(bytes,0,nums));
int nums2 = fis.read(bytes);//10
System.out.println(new String(bytes,0,nums2));
int nums3 = fis.read(bytes);//6
System.out.println(new String(bytes,0,nums3));
fis.close();

}

文件加密和解密工具
public static void main(String[] args) throws IOException {
System.out.println(“请输入文件的全路径:”);
Scanner input = new Scanner(System.in);
String fileName = input.nextLine();
//原文件:shuangseqiu.png
File oldFile = new File(fileName);
//新文件:mi-shuangseqiu.png
File newFile = new File(oldFile.getParentFile(),“mi-”+oldFile.getName());

FileInputStream fis = new FileInputStream(oldFile);
FileOutputStream fos = new FileOutputStream(newFile);
while (true){
    int b = fis.read();
    if (b==-1)
        break;
    //任何数据^相同的数字两次 结果就是其本身
    fos.write(b^10);
}
System.out.println("加密或解密完成!");

}

字符输出
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter(“D://b.txt”);
fw.write(‘b’);
//append:追加,返回当前对象
fw.append(“锄禾日当午”).append(“汗滴禾下土”).append(“谁知盘中餐”);
fw.close();
}

字符读取
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader(“b.txt”);
while (true){
int len = fr.read();
if (len == -1)
break;
System.out.println((char)len);
}
char[] chars = new char[100];
int len = fr.read(chars);
System.out.println(new String(chars,0,len));
}

字节流转化字符流
public static void main(String[] args) throws IOException {
//转换流
//字节流 ‘装饰’ 为字符流 使用了装饰者设计模式
FileInputStream fis = new FileInputStream(“D://b.txt”);
//将字节输入流转换为字符输入流
//参数1.要转换的字节流
//参数2.指定编码名称
InputStreamReader isr = new InputStreamReader(fis,“UTF-8”);
while (true){
int len = isr.read();
if (len==-1)
break;
System.out.println((char)len);
}

FileOutputStream fos = new FileOutputStream("D://b.txt");
OutputStreamWriter osw = new OutputStreamWriter(fos);
osw.write("刘叶飘最帅");
osw.flush();
osw.close();

}

Print与BufferedReader
public static void main(String[] args) throws IOException {
//字符输出(打印流)
//1、
PrintStream ps = new PrintStream(“D://c.txt”);
ps.println(“彭于晏很帅”);
//2、
PrintWriter pw = new PrintWriter(“D://c.txt”);
pw.println(“彭于晏真的很帅”);
pw.flush();
ps.close();

//缓存读取流,将字符输入流转换带有缓存可以一次读取一行的缓存字符读取流
FileReader fr = new FileReader("D://c.txt");
BufferedReader br = new BufferedReader(fr);
String text = br.readLine();
System.out.println(text);

}

Properties
public static void main(String[] args) throws IOException {
//Properties类与propertis文件
Properties ppt = new Properties();
ppt.put(“name”,“西游记”);
ppt.put(“info”,“唐僧师徒四人去西天取经的故事”);
FileWriter fw = new FileWriter(“D://book.properties”);
ppt.store(fw,“小说图书”);
fw.close();

Properties ppt = new Properties();
FileReader fr = new FileReader("D:book.properties");
ppt.load(fr);
System.out.println(ppt.getProperty("name"));
System.out.println(ppt.getProperty("info"));

}

以上内容是我在学习IO流理整理出来的,希望对正在学习java的你们能够有所帮助。如果内容不当或代码有错误,一定要联系我,我会加以改正。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值