1.一个一个字节的读
package com.sxt.io;
/*
*操作步骤
* 1.创建流
* 2.选择流
* 3.操作
* 4.释放资源
*/
import java.io.*;
public class IOText01 {
public static void main(String[] args) {
//1.创建源
File src =new File("abc.txt");
//2.选择流
InputStream is=null;
try {
is =new FileInputStream(src);
//3.操作(读取)
int temp;
while((temp=is.read())!=-1){
System.out.println((char)temp);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(null!=is){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
2.分段读取
package com.sxt.io;
import java.io.*;
/*四个步骤:分段读取
*1.创建源
* 2.选择流
* 3.操作(分段读取)
*4.释放资源
*/
public class IOText02 {
public static void main(String[] args) {
//1.创建源
File src=new File("abc.txt");
//2.选择流
InputStream is=null;
try {
is =new FileInputStream(src);
//3.操作(分段读取)
byte[] flush =new byte[3];//缓冲容器
int length =-1;
while((length=is.read(flush))!=-1){
String str =new String(flush,0,flush.length,"utf8");
System.out.println(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
hel
lo
sxt
i
am
goi
ng
to
sxt
3.写出内容
package com.sxt.io;
/*
*文件字节输出流
* 1.创建源
* 2.选择流
* 3.操作(写出内容)
* 4.释放资源
*/
import java.io.*;
public class IoText03 {
public static void main(String[] args) {
//1.创建源
File dest =new File("dext.txt");
//2.选择流
OutputStream os =null;
try {
os =new FileOutputStream(dest);
//3.操作(写出内容)
String str ="wang li gao";
byte[] datas=str.getBytes();
os.write(datas,0,datas.length);
os.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(null!=os){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
问题总结:文件可以不存在,可自行创建
4.文件拷贝
package com.sxt.io;
import java.io.*;
/*
*文件字节输入、输出流 拷贝
* 1.创建源
* 2.选择流
* 3.操作
* 4.释放资源
*/
public class Copy {
public static void main(String[] args) {
copy("src/com/sxt/io/IOText01.java","abc.txt");
}
public static void copy(String srcPath,String destPath){
//1.创建源
File src =new File(srcPath);
File dest =new File(destPath);
//2.选择流
InputStream is =null;
OutputStream os =null;
try {
is =new FileInputStream(src);
os =new FileOutputStream(dest);
//3.操作
byte[] flush =new byte[3];
int len=-1;
while((len=is.read(flush))!=-1){
os.write(flush,0,flush.length);
}
os.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(null!=os){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(null!=is){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
问题总结:
1、若write 方法中不指定起始位置和长度时,结果可能会出错,建议加上起始位置和长度
2、关闭流时先打开的后关闭:即先关闭输出流,后关闭输入流
5.字符输入流
package com.sxt.io;
import java.io.*;
public class IOText04 {
public static void main(String[] args) {
//1.创建源
File src = new File("abc.txt");
//2.选择流
FileReader reader = null;
try {
reader = new FileReader(src);
//3.操作(分段读取)
char[] flush = new char[1024];
int len = -1;
while ((len = reader.read(flush)) != -1) {
//字符数组-->字符串
String str = new String(flush, 0, flush.length);
System.out.println(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
6.字符输出流
package com.sxt.io;
import java.io.*;
public class IOText05 {
public static void main(String[] args) {
//1.创建源
File dest =new File("dext.txt");
//2.选择源
FileWriter writer =null;
try {
writer =new FileWriter(dest);
//3.操作(写出内容)
//写法一
/* String str ="wang li gao\n尚学堂";
char[] datas=str.toCharArray();//字符串-->字符数组
writer.write(datas,0,datas.length);
writer.write("add");
writer.flush();
*/
//写法二
/* String msg ="IO is so easy\r\n尚学堂欢迎你";
writer.write(msg);
writer.flush();
*/
//写法三
writer.append("IO is so easy\r\n").append("尚学堂欢迎你");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(null!=writer){
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
以上源头文件都是File
-------------------------------------------------------
以下源头文件是内存
7.字节数组输入流
package com.sxt.io;
/*四个步骤:字节数组输入流
*1.创建源:字节数组 不要太大
* 2.选择流
* 3.操作
* 4.释放资源:可以不用处理
*
*/
import java.io.*;
public class IOText06 {
public static void main(String[] args) {
//1.创建源
byte[] src ="talk is cheap show me the code".getBytes();
//2.选择流
InputStream is = null;
try {
is = new ByteArrayInputStream(src);
//3.操作(分段读取)
byte[] flush = new byte[5];
int len = -1;
while ((len = is.read(flush)) != -1) {
//字符数组-->字符串
String str = new String(flush, 0, flush.length);
System.out.println(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
8.字节数组输出流
package com.sxt.io;
/*
*字节数组输出流ByteArrayOutputStream
* 1.创建源:内部维护
* 2.选择流:不关联源
* 3.操作:(写出内容)
* 4.释放资源:可以不用
*
* 获取数据:toByteArray()
*/
import java.io.*;
public class IOText07 {
public static void main(String[] args) {
//1.创建源
byte[] dest =null;
//2.选择流
ByteArrayOutputStream baos =null;
try {
baos =new ByteArrayOutputStream();
//3.操作(写出内容)
String str ="show me the code";
byte[] datas=str.getBytes();
baos.write(datas,0,datas.length);
baos.flush();
//获取数据
dest =baos.toByteArray();
System.out.println(dest.length+"-->"+new String (dest,0,dest.length));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(null!=baos){
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
问题总结
1.创建源:内部维护
2.选择流:不关联源
3.操作:(写出内容)
4.释放资源:可以不用
获取数据:toByteArray()
9.综合--对接流
package com.sxt.io;
/*
*1.图片读取到字节数组中
* 2.字节数组写出到文件
*/
import java.io.*;
public class IOtext08 {
public static void main(String[] args) {
//文件转成字节数组
byte[] datas=fileToByteArray("abc.txt");
System.out.println(datas.length);
byteArrayToFile( datas,"dext.txt");
}
/*
*图片读取到字节数组
*1)图片到程序 FileInputStream
* 2)程序到字节数组 ByteArrayOutputStream
*/
public static byte[] fileToByteArray(String filePath) {
//1.创建源与目的地
File src = new File(filePath);
byte[] dest = null;
//2.选择流
InputStream is = null;
ByteArrayOutputStream baos = null;
try {
is = new FileInputStream(src);
baos = new ByteArrayOutputStream();
//3.操作(分段读取)
byte[] flush = new byte[1024 * 10];
int len = -1;
while ((len = is.read(flush)) != -1) {
baos.write(flush, 0, len);//写入到字节数组中
}
baos.flush();
return baos.toByteArray();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
//字节数组写出到图片
/*1)字节数组到程序ByteArrayInputStream
2)程序到文件FileOutputStream
*/
public static void byteArrayToFile(byte[] src,String filePath){
//1.创建源
File dest =new File(filePath);
//2.选择流
InputStream is = null;
OutputStream os =null;
try {
is = new ByteArrayInputStream(src);
os =new FileOutputStream(dest);
//3.操作(分段读取)
byte[] flush = new byte[5];
int len = -1;
while ((len = is.read(flush)) != -1) {
//字符数组-->字符串
String str = new String(flush, 0, len);
byte[] datas=str.getBytes();
os.write(datas,0,datas.length);
}
os.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(null!=os){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
10.封装拷贝和释放资源
package com.sxt.io;
/*
*1.封装拷贝
* 2.封装释放资源
*/
import java.io.*;
public class FileUtils {
public static void main(String[] args) {
//文件到文件
try {
InputStream is =new FileInputStream("abc.txt");
OutputStream os =new FileOutputStream("abc-copy.txt");
copy(is,os);
} catch (IOException e) {
e.printStackTrace();
}
//文件到字节数组
byte[] datas=null;
try {
InputStream is =new FileInputStream("abc.txt");
ByteArrayOutputStream os =new ByteArrayOutputStream();
copy(is,os);
datas =os.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}
//字节数组到文件
try {
InputStream is =new ByteArrayInputStream(datas);
FileOutputStream os =new FileOutputStream("copy.txt");
copy(is,os);
} catch (IOException e) {
e.printStackTrace();
}
}
//对接输入输出流
public static void copy(InputStream is,OutputStream os) {
try {
//3.操作
byte[] flush = new byte[3];
int len = -1;
while ((len = is.read(flush)) != -1) {
os.write(flush, 0, len);
}
os.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
close(is, os);
}
}
//释放资源
public static void close(InputStream is,OutputStream os){
if(null!=is){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(null!=os){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void close(Closeable... ios){
for(Closeable io: ios){
if(null!=io){
try {
io.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
释放资源2.0: try...with...resource
package com.sxt.io;
/*
*try..with...resourse
*/
import java.io.*;
public class FileUtils2 {
public static void main(String[] args) {
//文件到文件
try {
InputStream is =new FileInputStream("abc.txt");
OutputStream os =new FileOutputStream("abc-copy.txt");
copy(is,os);
} catch (IOException e) {
e.printStackTrace();
}
//文件到字节数组
byte[] datas=null;
try {
InputStream is =new FileInputStream("abc.txt");
ByteArrayOutputStream os =new ByteArrayOutputStream();
copy(is,os);
datas =os.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}
//字节数组到文件
try {
InputStream is =new ByteArrayInputStream(datas);
FileOutputStream os =new FileOutputStream("copy.txt");
copy(is,os);
} catch (IOException e) {
e.printStackTrace();
}
}
//对接输入输出流
public static void copy(InputStream is,OutputStream os) {
try {
//3.操作
byte[] flush = new byte[1024];
int len = -1;
while ((len = is.read(flush)) != -1) {
os.write(flush, 0, len);
}
os.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}