IO字节流和缓冲流

IO字节流的读取和写入

读取

 import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException; public class Test {
public static void main(String[] args) {
try (FileInputStream fis = new FileInputStream("java.txt")
) {
byte[] bytes = new byte[40];
int temp;
while ((temp = fis.read(bytes)) != -1) {
System.out.println(new String(bytes, 0, temp));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

写入

 import java.io.*;

 public class Test {
public static void main(String[] args) {
try (FileOutputStream fos = new FileOutputStream("file" + File.separator + "1024.txt", true) //写入时不清除原有数据
) {
fos.write("Hello".getBytes());
fos.write("\n".getBytes()); //换行
fos.write("Wrold!".getBytes());
fos.flush(); //刷新
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

IO字节流的文件拷贝

 import java.io.*;

 public class Test {
public static void main(String[] args) {
try (
FileOutputStream fos = new FileOutputStream("file" + File.separator + "new.txt");
FileInputStream fis = new FileInputStream("java.txt")
) {
int temp;
byte[] bytes = new byte[50];
while ((temp = fis.read(bytes)) != -1) {
fos.write(bytes);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

IO缓冲流的读取和写入

 import java.io.*;

 /**
* IO缓冲字节流的读取
*/
public class Test {
public static void main(String[] args) {
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream("java.txt"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("file" + File.separator + "new.txt"));
) {
int temp;
while ((temp = bis.read()) != -1) {
bos.write(temp);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

IO字符流和缓冲流

IO字符流的文件拷贝

 import java.io.*;

 /**
* IO字符流的文件拷贝
*/
public class Test {
public static void main(String[] args) {
try (FileReader fr = new FileReader("java.txt");
FileWriter fw = new FileWriter("file" + File.separator + "new.txt")
) {
int temp;
while ((temp = fr.read()) != -1) {
fw.write(temp);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

IO缓冲字符流的文件拷贝

 import java.io.*;

 /**
* IO缓冲字符流的文件拷贝
*/
public class Test {
public static void main(String[] args) {
try (
BufferedReader br = new BufferedReader(new FileReader("java.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("file" + File.separator + "new.txt"))
) {
String msg;
while ((msg = br.readLine()) != null) {
bw.write(msg);
bw.newLine(); //换行
bw.flush(); //刷新
} } catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

练习:图片简单的加密和解密

加密

 import java.io.*;

 /**
* 图片的简单加密
* 通过异或的方式
*/
public class Test {
public static void main(String[] args) {
try (
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("file" + File.separator + "123.jpg"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("file" + File.separator + "new123.jpg"))
) {
int temp;
while ((temp = bis.read()) != -1) {
bos.write(temp ^ 88);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

解密

 import java.io.*;

 /**
* 图片的简单解密
* 通过异或的方式
*/
public class Test {
public static void main(String[] args) {
try (
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("file" + File.separator + "code.jpg"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("file" + File.separator + "decode.jpg"))
) {
int temp;
while ((temp = bis.read()) != -1) {
bos.write(temp ^ 88);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

IO流的复习笔记的更多相关文章

  1. IO流等学习笔记

    1.为什么日期的开始是从1970年0101开始记录,计算机的日期记录是现在的时间距1970年的时间,可正可负.? 2.引用类型默认都为null,基本数据类型为0,除基本数据类型外所有的都为引用数据类型 ...

  2. Java基础复习笔记系列 七 IO操作

    Java基础复习笔记系列之 IO操作 我们说的出入,都是站在程序的角度来说的.FileInputStream是读入数据.?????? 1.流是什么东西? 这章的理解的关键是:形象思维.一个管道插入了一 ...

  3. Android(java)学习笔记167:Java中操作文件的类介绍(File + IO流)

    1.File类:对硬盘上的文件和目录进行操作的类.    File类是文件和目录路径名抽象表现形式  构造函数:        1) File(String pathname)       Creat ...

  4. Java基础知识强化之IO流笔记17:FileOutputStream构造方法使用

    1. 可以参照之前写的笔记:   Android(java)学习笔记167:Java中操作文件的类介绍(File + IO流) 2. FileOutputStream(常用的)构造方法: FileOu ...

  5. Java 学习笔记 IO流与File操作

    可能你只想简单的使用,暂时不想了解太多的知识,那么请看这里,了解一下如何读文件,写文件 读文件示例代码 File file = new File("D:\\test\\t.txt" ...

  6. Java开发笔记(九十一)IO流处理简单的数据压缩

    前面介绍的文件I/O,不管是写入文本还是写入对象,文件中的数据基本是原来的模样,用记事本之类的文本编辑软件都能浏览个大概.这么存储数据,要说方便确实方便,只是不够经济划算,原因有二:其一,写入的数据可 ...

  7. Android(java)学习笔记110:Java中操作文件的类介绍(File + IO流)

    1.File类:对硬盘上的文件和目录进行操作的类.    File类是文件和目录路径名抽象表现形式  构造函数:        1) File(String pathname)       Creat ...

  8. 20.IO流部分笔记

    20.IO流部分笔记 2018/09/06 1.IO流  1.1 创建字节输出流对象,如果没有就自动创建一个 FileOutputStram fos = new FileOutputStram(&qu ...

  9. JavaSE学习笔记(14)---File类和IO流(字节流和字符流)

    JavaSE学习笔记(14)---File类和IO流(字节流和字符流) File类 概述 java.io.File 类是文件和目录路径名的抽象表示,主要用于文件和目录的创建.查找和删除等操作. 构造方 ...

随机推荐

  1. 【OCP-12c】2019年CUUG OCP 071考试题库(75题)

    75.Which statements are correct regarding indexes? (Choose all that apply.) A. A non-deferrable PRIM ...

  2. OC中NSClassFromString()与NSStringFromClass()的用法及应用场景

    1.NSClassFromString()利用一个字符串创建一个类,我是在标签控制器中 UITabBarController中创建它的子控制器中使用的 - (void)viewDidLoad { [s ...

  3. leecode刷题(19)-- 最长公共前缀

    leecode刷题(19)-- 最长公共前缀 最长公共前缀 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: [&quo ...

  4. Linux命令行测试网速speedtest.net

    Linux命令行测试网速speedtest.net 当发现上网速度变慢时,人们通常会先首先测试自己的电脑到网络服务提供商(通常被称为"最后一公里")的网络连接速度.在可用于测试宽带 ...

  5. react onclick传递参数

    最近在做react项目的时候,被一个小问题绊了一脚,记录一下 onClick 传入参数 onClick={e=>{this.Mallclose(e,index)} onClick={this.M ...

  6. jpetStore 学习总结(2)

    在写jpetstore时,最难理解的应该是数据库还有每个表之间的关系了,我在这里对数据库简单的介绍. 以下是数据库的所有表:    account表是个人信息表,里面包括用户的名字,邮箱,地址,哪个城 ...

  7. 本地搭建sass运行环境

    1.安装node.js 安装文件为msi文件,可到node.js官网下载安装包,下载路径为:https://nodejs.org/en/download/ 安装路径为默认路径,安装完成之后配置环境变量 ...

  8. 关于MatlabGUI清除WorkSpace的用法

    近日在调试Matlba GUI程序时,因为不想退出程序后手动Clear All来清理,又需要在过程中对WorkSpace进行清理,否则会引用之前的结果导致错误,找了很多资料,国内的论坛什么的都说用Cl ...

  9. Mac 10.12安装抓包工具Charles

    说明:青花瓷,Filddler之后就是这个最好用.收费软件. 下载: (链接:https://pan.baidu.com/s/1kV1Robl 密码: 3g6u)

  10. [转]分布式锁-RedisLockRegistry源码分析

    前言 官网的英文介绍大概如下: Starting with version 4.0, the RedisLockRegistry is available. Certain components (f ...