IO流的种类
字节流:
1.字节输入流(读数据) InputStream(抽象类) 需要使用子类FileInputStream
2.字节输出流(写数据) OutputStream(抽象类) 需要使用子类FileOutputStream
字符流:
1.字符输入流(读数据) Reader(抽象类) 需要使用子类FilerReader
2.字符输出流(写数据) Writer(抽象类) 需要使用子类FileWriter*/
字节输入流
从文件中读取内容方法1
import java.io.*;
public class IODemo2 {
public static void main(String[] args) throws IOException {
//创建一个文件对象
File file = new File("D:\\重要代码Demo\\文件IO对象\\FileDemo1\\file01.txt");
//创建一个指向具体的文件对象的字节输入流对象
InputStream s=new FileInputStream(file); //传入的参数是一个文件对象
//通过一个字节一个字节的读数据
byte onebyte = (byte) s.read();
System.out.println("h的ASCII:"+onebyte); //ASCII码值
System.out.println("104对应的英文单词:"+(char)onebyte);
//继续读取下一个字节
System.out.println("继续读取下一个字节"+(char)s.read());
System.out.println("继续读取下一个字节"+(char)s.read());
System.out.println("继续读取下一个字节"+(char)s.read());
System.out.println("继续读取下一个字节"+(char)s.read());
}
}
//输出结果:
h的ASCII:104
104对应的英文单词:h
继续读取下一个字节e
继续读取下一个字节l
继续读取下一个字节l
继续读取下一个字节o
read() 只能一个一个的读取字节,返回值为int类型,因此可以利用循环来依次读取文件中的内容,读完的表示是s.read()!=-1时
package com.bianyiit.cast;
import java.io.*;
public class IODemo3 {
public static void main(String[] args) throws IOException {
//创建一个文件对象
File file = new File("D:\\重要代码Demo\\文件IO对象\\FileDemo1\\file01.txt");
//创建一个指向具体的文件对象的字节输入流对象
InputStream s=new FileInputStream(file);
//使用循环去读取字节第一种方式
//调用read()读数据的时候,当返回值不等于-1时代表还有数据可以读取
//s.read()代表读到了一个字节,然后抽取出来
int read;
while((read=s.read())!=-1){
System.out.print((char)read);
}
//使用循环去读取字节第二种方式(读一个字节就放到字节数组里面)
//定义一个字节数据
/*byte[] arr=new byte[1024];
//读一个放一个
byte b1 = (byte)s.read();
arr[0]=b1;
byte b2 = (byte)s.read();
arr[1]=b2;
//将字节数组转换成字符串
String s1 = new String(arr, 0, 2);
System.out.println(s1);*/
}
}
简化代码
package com.bianyiit.cast;
import java.io.*;
public class IODemo3 {
public static void main(String[] args) throws IOException {
//创建一个文件对象
File file = new File("D:\\重要代码Demo\\文件IO对象\\FileDemo1\\file01.txt");
//创建一个指向具体的文件对象的字节输入流对象
InputStream s=new FileInputStream(file);
//调用read()读数据的时候,当返回值不等于-1时代表还有数据可以读取
//定义一个字节数组
byte[] arr=new byte[1024];
int read;
int i=0; //定义数组真正读到字节的个数
while ((read=s.read())!=-1){
arr[i]=(byte)read;
i++;
}
//将字节数组转换成字符串
String s2 = new String(arr, 0, i);
System.out.println(s2);
方法2:调用read(byte[] byte)一次读取一个数组长度
package com.bianyiit.cast;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class IODemo4 {
public static void main(String[] args) throws IOException {
//使用字节输入流一次读取一个字节数组
//如果输入了错误的文件,会输出系统找不到指定的文件
FileInputStream is = new FileInputStream(new File("D:\\重要代码Demo\\文件IO对象\\FileDemo1\\file01.txt"));
byte[] arr = new byte[1024];
//byte[] arr = new byte[1024*2];
/*//一次读一个字节数组(一次读1024个字节)
int length = is.read(arr);
String s = new String(arr, 0, length);
System.out.println(s);*/
//如何把文件里面的数据全部读完
int read;
while((read=is.read(arr))!=-1){
String s1 = new String(arr, 0, read);
System.out.println(s1);
}
}
}
字节输出流
package com.bianyiit.cast;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class OutputDemo1 {
public static void main(String[] args) throws IOException {
//使用字节输出流写数据
FileOutputStream os = new FileOutputStream(new File("D:\\重要代码Demo\\文件IO对象\\FileDemo1\\file02.txt"),true);
//使用os字节输出流对象往file02.txt中写数据
String s="I am sunshine boy";
//将字符串转换成字节数组
byte[] bytes = s.getBytes();
//将一个字节数组写入文件中
os.write(bytes);
System.out.println("写入成功!!");
//重复写入数据会覆盖,如果想往文件中追加数据,需要在fileoutputstream中的括号中添加一个参数:true (默认是false)
}
}
文件复制案例
package com.Fuxi;
import java.io.*;
//字节输入输出流
public class IODemo {
public static void main(String[] args) throws IOException {
//创建输入输出流对象
InputStream is=new FileInputStream("E:\\b.txt");
OutputStream os=new FileOutputStream("E:\\a.txt");
//定义一个byte的接收数组
byte[] arr=new byte[1024];
int length;//用来计读取到的字节数
//判断文件是否读完
while((length=is.read(arr))!=-1)
{
//看写入的数据
String s=new String(arr,0,length);
System.out.println(s);
//写入数据
os.write(arr,0,length);
}
}
}
IO流的标准写法
package com.bianyiit.anli;
import java.io.*;
public class Demo1 {
public static void main(String[] args) throws IOException {
//输入输出流的标准代码规范
//申请了两个资源空间
//创建输出流对象
FileInputStream is = null;
FileOutputStream os = null;
try {
is = new FileInputStream(new File("D:\\重要代码Demo\\文件IO对象\\FileDemo1\\preview.jpg"));
os = new FileOutputStream(new File("D:\\重要代码Demo\\文件IO对象\\girl.jpg"));
//读取图片的内容
//一次读一个字节数组
byte[] arr=new byte[1024];
int length;
while((length=is.read(arr))!=-1){
os.write(arr,0,length);
}
} catch (IOException e) {
//e.printStackTrace();
} finally {
//释放资源空间 close()
//假如流对象没有创建成功的情况下就不需要关闭流空间
if(is!=null){
is.close();
}
if(os!=null){
os.close();
}
}
}
}