IO流的复习笔记
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流的复习笔记的更多相关文章
- IO流等学习笔记
1.为什么日期的开始是从1970年0101开始记录,计算机的日期记录是现在的时间距1970年的时间,可正可负.? 2.引用类型默认都为null,基本数据类型为0,除基本数据类型外所有的都为引用数据类型 ...
- Java基础复习笔记系列 七 IO操作
Java基础复习笔记系列之 IO操作 我们说的出入,都是站在程序的角度来说的.FileInputStream是读入数据.?????? 1.流是什么东西? 这章的理解的关键是:形象思维.一个管道插入了一 ...
- Android(java)学习笔记167:Java中操作文件的类介绍(File + IO流)
1.File类:对硬盘上的文件和目录进行操作的类. File类是文件和目录路径名抽象表现形式 构造函数: 1) File(String pathname) Creat ...
- Java基础知识强化之IO流笔记17:FileOutputStream构造方法使用
1. 可以参照之前写的笔记: Android(java)学习笔记167:Java中操作文件的类介绍(File + IO流) 2. FileOutputStream(常用的)构造方法: FileOu ...
- Java 学习笔记 IO流与File操作
可能你只想简单的使用,暂时不想了解太多的知识,那么请看这里,了解一下如何读文件,写文件 读文件示例代码 File file = new File("D:\\test\\t.txt" ...
- Java开发笔记(九十一)IO流处理简单的数据压缩
前面介绍的文件I/O,不管是写入文本还是写入对象,文件中的数据基本是原来的模样,用记事本之类的文本编辑软件都能浏览个大概.这么存储数据,要说方便确实方便,只是不够经济划算,原因有二:其一,写入的数据可 ...
- Android(java)学习笔记110:Java中操作文件的类介绍(File + IO流)
1.File类:对硬盘上的文件和目录进行操作的类. File类是文件和目录路径名抽象表现形式 构造函数: 1) File(String pathname) Creat ...
- 20.IO流部分笔记
20.IO流部分笔记 2018/09/06 1.IO流 1.1 创建字节输出流对象,如果没有就自动创建一个 FileOutputStram fos = new FileOutputStram(&qu ...
- JavaSE学习笔记(14)---File类和IO流(字节流和字符流)
JavaSE学习笔记(14)---File类和IO流(字节流和字符流) File类 概述 java.io.File 类是文件和目录路径名的抽象表示,主要用于文件和目录的创建.查找和删除等操作. 构造方 ...
随机推荐
- kali linux之无线渗透
无线技术变化大,难度大,既新鲜刺激,又压力山大.一半协议 一半理论 无线技术特点: 行业发展迅猛 互联网的重要入口 边界模糊 安全实施缺失而且困难 对技术不了解造成配置不当 企业网络私自接入ap破坏 ...
- sqlmap注入之tamper绕过WAF脚本列表
本文作者:i春秋作者——玫瑰 QQ2230353371转载请保留文章出处 使用方法--tamper xxx.py apostrophemask.py用UTF-8全角字符替换单引号字符 apostrop ...
- Android 线刷小白教程
Android 线刷小白教程 再说一遍,绝不使用刷机精灵等软件. 一.概念 安卓系统一般把rom芯片分成7个区,如果再加上内置sd卡这个分区,就是8个: hboot分区----------负责启动. ...
- multiprocess(上)
仔细说来,multiprocess不是一个模块而是python中一个操作.管理进程的包. 之所以叫multi是取自multiple的多功能的意思,在这个包中几乎包含了和进程有关的所有子模块.由于提供的 ...
- 浅析Postgres中的并发控制(Concurrency Control)与事务特性(下)
上文我们讨论了PostgreSQL的MVCC相关的基础知识以及实现机制.关于PostgreSQL中的MVCC,我们只讲了元组可见性的问题,还剩下两个问题没讲.一个是"Lost Update& ...
- leetcode-695-Max Area of Island(BFS)
题目描述: Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land ...
- 图的最短路径---迪杰斯特拉(Dijkstra)算法浅析
什么是最短路径 在网图和非网图中,最短路径的含义是不一样的.对于非网图没有边上的权值,所谓的最短路径,其实就是指两顶点之间经过的边数最少的路径. 对于网图,最短路径就是指两顶点之间经过的边上权值之和最 ...
- 【Quartz】基本原理
1 核心概念 1.1 核心元素 (1)Scheduler 任务调度器,是Quartz框架的核心,负责管理其他组件. (2)Trigger 触发器,用于定义任务调度的时间规则,有SimpleTri ...
- lspci
lspci 是一个用来显示系统中所有PCI总线设备或连接到该总线上的所有设备的工具. 列出所有的PCIE设备: lspci 选项: -v 使得 lspci 以冗余模式显示所有设备的详细信息. -vv ...
- Carte上面的作业1、2天就会丢失的问题
发现Carte上面的作业莫名其妙就会没有,问了客户的维护人员说也没删除. 对象时间也是No Limit,但还是隔1.2天就不见了. 那说明之前配置这里还是无效 <slave_config> ...