流的概念
说白了就一句话---------------->对文件的读取和写入的操作。
输入流:
以内存为主体,数据到内存的过程叫做读操作(内存在读read)称为输入流(有东西要进来了input)
输出流:
以内存为主体,数据到文件的过程叫做写操作(内存在写东西write)称为输出流(有东西要出去了output)
-------------------------------------------------------------------------------------------------------------------------------------------------------------
字符流---需要对除了英文之外的文字操作的流
字节流---对以字节单位存储的数据文件操作的流
话不多说多敲代码
-----FileInputStream,FileOutputStream-----
字节流:
public class DemoFileInputStream {
public static void main(String[] args) {
//声明一个流
FileInputStream in = null;
FileOutputStream out = null;
//初始化流要捕获异常
try {
in = new FileInputStream("src/cp.jpg");//相对路径项目下的文件
out = new FileOutputStream("cp1.jpg");
byte[] buf = new byte[1024 * 4];
int len = -1;
while ((len = in.read(buf))!=-1) {//读操作
out.write(buf,0,len);//写操作
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (in!=null) in.close();
if (out!=null) out.close();
System.out.println("复制成功");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
-----FileReader,FileWriter-----
字符流(文件里面写了好多文字)
public class DemoFileReader {
public static void main(String[] args) throws Exception {//不建议直接抛出异常(为了偷懒)上文的抛异常的方式比较正规请大家按照上文的格式写...........
FileReader read = new FileReader("src/给女朋友的检讨.txt");
FileWriter write = new FileWriter("检讨.txt");
try {
char[] c = new char[1024 * 4];
int data = -1;
while ((data = read.read(c)) != -1) {
write.write(c,0,data);
write.flush();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (read != null) read.close();
if (write != null) write.close();
System.out.println("复制成功");
}
}
}
如果遇到编码与你的idea设置的编码格式不一样的话你得使用转化流InputStreamReader,OutputStreamWriter
-----InputStreamReader,OutputStreamWriter-----
小案例:实现字符文件的复制指定编码方式
public class DemoZhuanHuaLiu {
public static void main(String[] args) throws Exception{
FileInputStream in = new FileInputStream("检讨.txt");
InputStreamReader read = new InputStreamReader(in,"Utf-8");
FileOutputStream out = new FileOutputStream("F:\\liutest\\检讨.txt");
OutputStreamWriter write = new OutputStreamWriter(out, "GBK");
try {
char[] c = new char[1024 * 4];
int len = 0;
while ((len = read.read(c)) != -1) {
System.out.println(new String(c,0,len));
write.write(c, 0, len);
write.flush();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
write.close();
read.close();
System.out.println("转码完成");
}
}
}
高效输入输出缓冲流(这个流效率更高,方法更实用,同样分有字符流和字节流)
-----BufferedInputStream,BufferedOutputStream-----
字节流
public class DemoBufferedStream {
public static void main(String[] args) throws Exception{
//BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("hello.txt",true));
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("hello.txt",true));
BufferedInputStream in = new BufferedInputStream(new FileInputStream("hello.txt"));
try {
for (int i = 0; i < 1000; i++) {
out.write("hello world,hello java\r\n".getBytes());
out.flush();
}
byte[] buf = new byte[1024];
int len = -1;
while ((len = in.read(buf)) != -1) {
System.out.print(new String(buf, 0, len));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
in.close();
out.close();
}
}
}
-----BufferedRead,BufferedWriter-----
字符流
public class DemoBufferReader {
public static void main(String[] args) throws Exception{
BufferedWriter write = new BufferedWriter( new FileWriter("程序员的自我修养.txt"));
BufferedReader read = new BufferedReader(new FileReader("程序员的自我修养.txt"));
try {
for (int i = 0; i < 100; i++) {
write.write("好好学JAVA,天天找对象");
write.newLine();
write.flush();
}
String line = "";
while ((line = read.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
read.close();
write.close();
}
}
}
对象流(用来对对象的操作的流)
public class Student implements Serializable {//实现这个接口进行序列化
private static final long serialVersionUID = -49244194852343058L;
private String sNo;
private String name;
private int grade;
public void show(){
System.out.println("我是" + name);
}
@Override
public String toString() {
return "Student{" +
"sNo='" + sNo + '\'' +
", name='" + name + '\'' +
", grade=" + grade +
'}';
}
public static long getSerialVersionUID() {
return serialVersionUID;
}
public String getsNo() {
return sNo;
}
public void setsNo(String sNo) {
this.sNo = sNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
public Student(String sNo, String name, int grade) {
this.sNo = sNo;
this.name = name;
this.grade = grade;
}
}
public class Demo1 {
public static void main(String[] args) throws Exception{
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("student.txt"));
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("student.txt"));
try {
Student s = new Student("s112", "小样", 18);
Student s1 = new Student("s122", "大样", 28);
ArrayList<Student> list = new ArrayList<>();
list.add(s);
list.add(s1);
oos.writeObject(list);
oos.flush();
Object o = ois.readObject();
ArrayList<Student> list1 = (ArrayList<Student>) o;
list1.forEach(n->n.show());
} catch (Exception e) {
e.printStackTrace();
} finally {
oos.close();
ois.close();
}
}
}
内存流
public class Test {
public static void main(String[] args) throws Exception{
//给定一个大写的字符串将其转化成小写
String s = "HELLO";
ByteArrayInputStream bis = new ByteArrayInputStream(s.getBytes());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int data = -1;
while ((data = bis.read()) != -1) {
char c = (char) data;
bos.write(Character.toLowerCase(c));
}
String string = bos.toString();
System.out.println(string);
bis.close();
bos.close();
}
}
打印流
public class DemoPrintStream {
public static void main(String[] args) throws Exception{
PrintWriter pw = new PrintWriter("test.txt");
PrintStream ps = new PrintStream("test.txt");
ps.print("你好啊");
ps.write(97);//a
ps.print(97);//97
/* pw.write(98);//b
pw.write(98);//b
pw.print(98);//98
pw.print(98);*///98 同时使用会覆盖
ps.flush();
ps.close();
pw.flush();
pw.close();
}
}
---------------------------------------------------------
public class Demo2 {
public static void main(String[] args) throws Exception{
PrintStream pw = new PrintStream(new FileOutputStream("xuhaohao.txt"));//PrintWrite不可以
System.setOut(pw);//重定向
System.out.println("世界你好啊");
pw.close();
}
}
RandomAccessFile类
public class RandomAccessFileDemo {
public static void main(String[] args) throws Exception{
RandomAccessFile file = new RandomAccessFile("file.txt","rw");//设置为可读和写操作
//写入数据
file.writeUTF("你好,中国");
file.writeBoolean(true);//占一个字节
file.writeDouble(3.14159);//占八个字节
file.writeInt(12346);
file.seek(0);//把文件指针设置到文件的0号位置(读之前一定要置零)
System.out.println("----------读文件----------");
System.out.println(file.readUTF());
System.out.println(file.readBoolean());
file.skipBytes(8);//跳过八个字节读取
System.out.println(file.readInt());
//实现文件的复制
RandomAccessFile file2 = new RandomAccessFile("file2.txt","rw");
byte[] bytes = new byte[(int) file.length()];
file.seek(0);//记得要置零
file.readFully(bytes);
file2.write(bytes);
System.out.println("复制成功");
}
}