目录
io流
字节流/字符流
字节流可以用于所有文件,字符流只能用于简单文本的文件(即可以被记事本打开且无乱码的文件)
字节流又分为字节输入流和字节输出流.
简单io流
字节输出流可以如下构造
FileOutputStream fos=new FileOutputStream(file,true);
第一个参数为文件或者路径,第二个参数为续写开关,默认为否,输出时会清空文件再输入,反之则续写.
file处可以写路径,也可以写文件若写文件需在前面添加一段代码
File file=new File("kiana.txt");//"kiana.txt"为文件路径
用法如下
fos.write();
参数可以有多种形式,如
int形式会以阿思康码的形式解码,推荐使用长度为1024的数组.可以将字符串灌进数组之后输出.
输出完毕后关流.
fos.close();
字节输入流如下构造
FileInputStream fis=new FileInputStream(file); byte[]txt=new byte[1024]; int b; while((b= fis.read(txt))!=-1) { System.out.print(new String (txt ,0,b)); } fis.close();
首行为构造,第二行构造数组方便传递,第三行定义整型,数组若装不满可以通过b来计数,且还充当是否录入数据的成分.
字符输出流如下构造
FileWriter fw =new FileWriter(file,true); String str2="Kiana,出击!\r\n"; fw.write(str2); fw.close();
与字节同理不再赘述
字符输入流如下构造
FileReader fr= new FileReader(file); char []chars=new char[1024]; int c; while((c=fr.read(chars))!=-1){ System.out.println(new String(chars,0,c)); } fr.close();
注意:字符流使用char[ ]数组,字节流使用byte[ ]数组
拷贝
拷贝单个文件
import java.io.*; public class Filemove1 { public static void main(String[] args) throws IOException { File file=new File("D:\\java学习资料\\windy"); File filedemo=new File("D:\\java学习资料\\noel"); File[]files=file.listFiles(); for (File i:files) { FileOutputStream fos=new FileOutputStream(new File(filedemo,i.getName())); FileInputStream fis=new FileInputStream(i); byte[]arr=new byte[1024]; int len; while ((len=fis.read(arr))!=-1){ fos.write(arr,0,len); } fos.close(); fis.close(); } } }
拷贝多级文件
import java.io.*; public class Filemove2 { public static void main(String[] args) throws IOException { File files1=new File("D:\\java学习资料\\windy"); File files2=new File("D:\\java学习资料\\noel"); filemove(files1,files2); } public static void filemove(File ed,File ing) throws IOException { ing.mkdirs(); File []filed=ed.listFiles(); for (File i:filed) { if(i.isFile()){ FileOutputStream fos=new FileOutputStream(new File(ing,i.getName())); FileInputStream fis=new FileInputStream(i); byte []arr=new byte[1024]; int len; while ((len=fis.read(arr))!=-1){ fos.write(arr,0,len); } fis.close(); fos.close(); } else { filemove(i,new File(ing,i.getName())); } } } } 多级文件利用递归思想
高级io流
缓冲流
以下为缓冲流的拷贝代码,边读边写.
import java.io.*; public class BufferedFileOutPUT { public static void main(String[] args) throws IOException { BufferedInputStream bi=new BufferedInputStream(new FileInputStream("hello.txt")); BufferedOutputStream bo=new BufferedOutputStream(new FileOutputStream("byebye.txt")); int len; byte[]arr=new byte[1024]; while((len=bi.read(arr))!=-1){ bo.write(arr,0,len); } bi.close(); bo.close(); } }
字符缓冲流同理,不再赘述
序列化流
以下为学生类代码
import java.io.Serializable; public class student implements Serializable { private static final long serialVersionUID = 01; private String name; private int age; private transient int uid; public student(String name, int age, int uid) { this.name = name; this.age = age; this.uid = uid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "student{" + "name='" + name + '\'' + ", age=" + age + ", uid=" + uid + '}'; } }
以下为序列化输出流代码
import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; public class xuliehua { public static void main(String[] args) throws IOException { student stu=new student("刘谌",17,12345612); ObjectOutputStream ob=new ObjectOutputStream(new FileOutputStream("hello.txt")); ob.writeObject(stu); ob.close(); } }
以下为序列化输入流代码
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.ObjectInputStream; import java.sql.SQLOutput; import java.util.ArrayList; public class xuliehua2 { public static void main(String[] args) throws IOException, ClassNotFoundException { ObjectInputStream oi=new ObjectInputStream(new FileInputStream("hello.txt")); Object s=oi.readObject(); System.out.println(s); oi.close(); } }
打印流
打印流只有字符输出流和字节输出流拥有,代码解释如下
import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.PrintStream; import java.io.PrintWriter; public class print { public static void main(String[] args) throws FileNotFoundException { PrintWriter pw=new PrintWriter(new FileOutputStream("byebye.txt"),true); pw.println("hello"); pw.close(); PrintStream ps=new PrintStream(new FileOutputStream("byebye.txt",true),true); ps.println("bye-bye"); ps.close(); } }
构造方法为
方法为