版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/cheidou123/article/details/82955489
一、FileInputStream/FileOutputStream
1.FileInputStream 通过字节方式读取文件
先创建一个txt文件
io test
代码:
/***
* @author bincai
* @email [email protected]
*/
public class No8_FileInputStream {
public static void main(String[] args) {
File src = new File("/Users/caibin/IOstudy/caibin.txt");
InputStream is = null;
try {
is = new FileInputStream(src);
//这里的byte一般指定1024*某个数,这里为了效果搞成3
byte[] cache = new byte[3];
int len;
//当读到-1时就到文件末尾了
while ((len = is.read(cache)) != -1) {
String str = new String(cache,0,len);
System.out.println(str);
}
} catch (FileNotFoundException ex) {
} catch (IOException ex) {
} finally {
try {
if (is != null) {
is.close();
}
} catch (IOException ex) {
}
}
}
}
2.FileOutputStream 通过字节方式写出或追加数据到文件
/***
* @author bincai
* @email [email protected]
*/
public class No9_FileOutputStream {
public static void main(String[] args) {
//文件不需要存在,不存在的话就创建
File src = new File("out.txt");
OutputStream os = null;
try {
//这里构造函数有一个是追加的,下面这种默认会覆盖以前的
os = new FileOutputStream(src);
String msg = "caibin is handsome";
byte[] cache = msg.getBytes();
os.write(cache, 0, cache.length);
//输出流flush一下,保证一个好习惯
os.flush();
} catch (FileNotFoundException ex) {
} catch (IOException ex) {
} finally {
try {
if (os != null) {
os.close();
}
} catch (IOException ex) {
}
}
}
}
3. 总结
⑴FileInputStream的src必须存在,FileOutputStream可以不存在,不存在的话创建一个。
⑵FileOutputStream可以追加。
4.实现文件的拷贝
/***
* @author bincai
* @email [email protected]
*/
public class No10_FileCopy {
public static void main(String[] args) {
//待复制文件
File src = new File("/Users/caibin/IOstudy/timg.jpeg");
//复制目标
File dest = new File("timg2.jpeg");
OutputStream os = null;
InputStream is = null;
try {
is = new FileInputStream(src);
os = new FileOutputStream(dest);
//这里的byte一般指定1024*某个数
byte[] cache = new byte[1024];
int len;
//当读到-1时就到文件末尾了e
while ((len = is.read(cache)) != -1) {
//写
os.write(cache, 0, len);
}
os.flush();
} catch (FileNotFoundException ex) {
} catch (IOException ex) {
} finally {
//先打开后关闭
try {
if (os != null) {
os.close();
}
} catch (IOException ex) {
}
try {
if (is != null) {
is.close();
}
} catch (IOException ex) {
}
}
}
}
二、FileReader/FileWriter(仅适合字符文件)
1、FileReader
FileReader最大的好处是不会出现乱码问题!!!前提是文件和项目编码一致!
读取文件
void close() 关闭流并且释放资源
int read() 读取一个字符
int read(char[] buf) 将字符读入数组
首先我们创建一个文件:
io test 北京市海淀区芍药居
代码:
public class No11_Reader {
public static void main(String[] args) {
// 1.创建源
File src = new File("caibin.txt");
// 2.选择流
Reader reader = null;
try {
reader = new FileReader(src);
// 3.操作流,分段读
char[] flush = new char[1024];
int len;
while ((len = reader.read(flush)) != -1) {
String str = new String(flush, 0, len);
System.out.println(str);
}
} catch (FileNotFoundException ex) {
} catch (IOException ex) {
} finally {
//先打开后关闭
try {
if (reader != null) {
reader.close();
}
} catch (IOException ex) {
}
}
}
}
2、FileWriter
写出或追加字符到文件
void close() 关闭流
void flush() 刷新流
void write(char[] buf) 写入文件一个字符数组
void write(string s) 写入文件字符串
append(char c) 追加字符
public class No12_Writer {
public static void main(String[] args) {
File dest = new File("dest.txt");
Writer writer = null;
try {
writer = new FileWriter(dest);
String msg = "handsome 北京市";
writer.write(msg);
writer.flush();
} catch (FileNotFoundException ex) {
} catch (IOException ex) {
} finally {
//先打开后关闭
try {
if (writer != null) {
writer.close();
}
} catch (IOException ex) {
}
}
}
}
三、字节数组流(ByteArrayInputStream/ByteArrayOutputStream)
字节数组流是基于内存的,所以不需要关闭,任何东西都可以转换成字节数组!
1. ByteArrayInputStream
public class No13_ByteArrayInputStream {
public static void main(String[] args) {
//1. 创建源
byte[] src = "shandong".getBytes();
//2. 选择流
InputStream is = null;
try {
is = new ByteArrayInputStream(src);
// 3.操作
int len;
//这里是容易出问题,如果是中文可能会乱码
byte[] flush = new byte[1024];
while((len=is.read(flush))!=-1){
String str = new String(flush,0,len);
System.out.println(str);
}
}catch(IOException ex){
}
//ByteArrayInputStream 不需要关闭
}
}
2. ByteArrayOutputStream
ByteArrayOutputStream并不需要目标文件,里面维护了一个字节数组,可以直接通过os.toByteArray把字节数组拿出来
import java.io.ByteArrayOutputStream;
import java.io.IOException;
/***
* @author bincai
* @email [email protected]
*/
public class No14_ByteArrayOutputStream {
public static void main(String[] args) {
byte[] dest = null;
ByteArrayOutputStream os = null;
try {
os = new ByteArrayOutputStream();
String msg = "caibin";
byte[] datas = msg.getBytes();
os.write(datas);
os.flush();
dest = os.toByteArray();
System.out.println(new String(dest));
} catch (IOException ex) {
}
}
}