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 类是文件和目录路径名的抽象表示,主要用于文件和目录的创建.查找和删除等操作. 构造方 ...
随机推荐
- H - Graphics(dfs)
H - Graphics Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%lld & %llu Submi ...
- 深入了解java虚拟机(JVM) 第十二章 类加载器
一.什么是类加载器 类加载器是一个用来加载类文件的类,Java源代码通过javac编译器编译成类文件,然后JVM来执行类文件中的字节码来执行程序.需要注意的是,只有被同一个类加载器加载的类才可能会相等 ...
- 【OCP-12c】CUUG 071题库考试原题及答案解析(19)
1.choose the best answerWhat is the primary difference between the relational database (RDB) andobje ...
- BZOJ 1801--中国象棋(DP)
1801: [Ahoi2009]chess 中国象棋 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1995 Solved: 1160[Submit] ...
- [CSS3] 各种角度的三角形绘制
#triangle-up { width:; height:; border-left: 50px solid transparent; border-right: 50px solid transp ...
- 由button标签在 IE 8.0 下的异常表现引发的一场血案
写在最前的最后:整篇文章絮絮叨叨说了半天,我得出一个最佳实践:和button标签say goodbay,用 a 标签模拟之. 首先看一个在chrome 下的简单demo 这样的布局在组件开发中再常见不 ...
- Java_多线程2_线程池
线程池(pool): 线程池的作用: 1.节省资源,减少线程的数量和创建销毁线程的开销2.合理的管理线程的分配 线程池的创建: 1.newCachedThreadPool //优点:很灵活,弹性的线程 ...
- js 封装trim()方法,去掉空格
<script> //定义一个对象 - 名字是$ var $$ = function() {}; //原型 $$.prototype = { $id:function(id) { retu ...
- Python flask Reason: image not found libmysqlclient.21.dylib
Python flask Reason: image not found libmysqlclient.21.dylib 折腾了半个下午,在这里找到了答案,在此记录一下,以免后人躺坑 错误提示: Im ...
- windows 下 pyinstaller distutils not included with latest virtualenv (16.4.0)
起因 因为windows下python3.7.2内置venv虚拟环境下pyinstaller错误问题,切换virtualenv但是发现最新版本又有其他问题 - -! ModuleNotFoundErr ...