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 类是文件和目录路径名的抽象表示,主要用于文件和目录的创建.查找和删除等操作. 构造方 ...
随机推荐
- 微信浏览器禁止app下载链接的两种处理方法
最近替朋友放一个微信下载链接,通过二维码扫描下载. 通过扫描二维码下载APP已成为一个非常方便的方式,微信也成为扫描二维码重要的工具,但是扫描后微信浏览器会对APK和appStore的链接进行屏蔽,导 ...
- HTML防止input回车提交表单
原链接:https://blog.csdn.net/ligang2585116/article/details/44699567 自动提交情况说明: 1.默认情况下,单个输入框,无论按钮的type=& ...
- “全栈2019”Java第一百零二章:哪些作用域可以声明局部内部类?
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...
- Mybatis 动态Sql语句《常用》
MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其他类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句有多么痛苦.拼接的时候要确保不能忘了必要的空格,还要注意省掉 ...
- centos绑定https
1.百度云申请免费ssl证书 一年一申请 2. https://www.wosign .com /faq/faq-apache-https.htm 3.注意ssl.conf里面各个证书的顺序 证书路径 ...
- day 10 课后作业
# -*- coding: utf-8 -*-# @Time : 2019/1/2 16:35# @Author : Endless-cloud# @Site : # @File : 课后作业.py# ...
- SpringCloud学习笔记(一)——基础
什么是微服务架构 简单地说,微服务是系统架构上的一种设计风格,它的主旨是将一个原本独立的系统拆分成多个小型服务,这些小型服务都在各自独立的进程中运行,服务之间通过基于HTTP的RESTful API进 ...
- golang使用etcd实现分布式锁
package main import ( "context" "fmt" "time" "go.etcd.io/etcd/cli ...
- python学习,day3:函数式编程,带参数
# coding=utf-8 # Author: RyAn Bi def test(x,y,z): print(x) print(y) print(z) test(y=2,z =3,x=1) #形参与 ...
- CountDownLatch的简单实现
1. @Data public abstract class BaseLatch { private int limit; protected int running; BaseLatch(int l ...