篇一:java_IO流总结
*************************
*******第19天 IO流*******
*************************
**********************
*******基本内容*******
**********************
字符流: Reader
常用子类:InputStreamReader->(继承)FileReader,BufferedReader
Writer
常用子类:OutputStreamWriter->(继承)FileWriter,BufferedWriter
字节流: OutputStream
常用子类:FileOutputStream,DataOutputStream,ObjectOutputStream,
BufferedOutputStream,ByteArrayOutputStream
InputStream
常用子类:FileInputStream,DataInputStream,ObjectOutputStream,
BufferedInputStream,ByteArrayInputStream
打印流: PrintStream
常用子对象:System.out,System.in,System.err ==============================================================================================
********************************
字节流:OutputStream,InputStream
********************************
一、字节流: FileInputStream,FileInputStream
//文件读写
import java.io.*;
publicclass Text1 {
publicstaticvoid main(String[] args) {
FileInputStreamfis=null;
FileInputStreamfos=null;
try {
fis=newFileInputStream("e:\\temp\\from.zip");
fos=newFileOutputStream("e:\\temp\\to.zip");
byte [] buffer=newbyte[1024];
while (true) {
inttemp = fis.read(buffer,0,buffer.length);
if(temp==-1){
break;
}
fos.write(buffer,0,temp);
}
} catch (Exception e) {
e.printStackTrace();
}
try {//捕获io流关闭异常
fis.close();
fos.close();
} catch (Exception e2) {
}
}
}
}
二、字节缓冲流:
BufferedInputStream,BufferedOutputStream--------------------------------------
//文件读写
publicclass Text2 {
publicstaticvoid main(String[] args) {
String fileName="e:\\老罗视频Android01-09.zip";
String toName="e:\\temp\\老罗视频Android01-09.zip";
FileInputStreamfis=null;
FileOutputStreamfos=null;
BufferedInputStreambis=null;
BufferedOutputStreambos=null;
try {
fis=newFileInputStream(fileName);
fos=newFileOutputStream(toName);
bis=newBufferedInputStream(fis);
bos=newBufferedOutputStream(fos);
long time1=System.currentTimeMillis();//打印时间
while (true) {
byte[] buffer=newbyte[1024];
int temp=bis.read(buffer,0,buffer.length);
if (temp==-1) {
break;
}
bos.write(buffer,0,temp);
}
long time2=System.currentTimeMillis();
System.out.println((time2-time1)/60000);//求时间差
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
bis.close();
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
三、数据流: DataInputStream,DataOutputStream
publicclass Text3 {
publicstaticvoid main(String[] args) throwsIOException {
intx=8;
charch='a';
doublef=5.6;
DataOutputStreamdos=newDataOutputStream(newFileOutputStream("e:\\temp\\dis.txt"));
DataInputStreamdis=newDataInputStream(newFileInputStream("e:\\temp\\dis.txt"));
dos.writeInt(x);
dos.writeChar(ch);
dos.writeDouble(f);
dos.close();
intgetx=dis.readInt();//8
chargetch=dis.readChar();//'a'
doublegetf=dis.readDouble();//5.6
}
}
四、对象数据流:
ObjectInputStream,ObjectDataOutputStream-------------------------------------- //写入对象数据
publicclass Task4 {
publicstaticvoid main(String[] args) throws Exception {
ObjectOutputStreamobs=newObjectOutputStream(newFileOutputStream("e:\\temp\\obs.txt"));
ObjectInputStreamois=newObjectInputStream(newFileInputStream("e:\\temp\\obs.txt"));
obs.writeObject(new Student("杨过",20,99));
obs.writeObject(new Student("小龙女",16,77));
obs.writeObject(new Student("小小龙女",11,77));
obs.writeObject(new Student("小小小龙女",7,77));
while(true){
try {
Student stu=(Student)ois.readObject();
System.out.println(stu);
} catch (Exception e) {
break;
}
class Student implementsSerializable{//实现序列化接口/*Externalizable*/
String name;
intage;
doublescore;
transient String id;//不希望属性被序列化,不会在写入文件时保存
public Student(String name, intage, doublescore) {
this.name = name;
this.age = age;
this.score = score;
}
@Override
public String toString() {
return"Student [name=" + name + ", age=" + age + ", score=" + score + "]"; }
}
/*class Student1 implements Externalizable{//自定义序列化
String name;
int age;
double score;
transient String id;//不希望属性被序列化,不会在写入文件时保存
public Student1() {
}
public Student1(String name, int age, double score) {
this.name = name;
this.age = age;
this.score = score;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", score=" + score + "]"; }
@Override
public void readExternal(ObjectInput in) throws IOException,
ClassNotFoundException {
System.out.println("反序列化");
this.name=(String)in.readObject();
this.age=in.readInt();
this.score=in.readDouble();
}
@Override
public void writeExternal(ObjectOutput out) throws IOException {
System.out.println("序列化");
out.writeInt(age);
out.writeDouble(score);
}
}*/
五、内存流
ByteArrayOutputStream,ByteArrayInputStream-----------------------------------------
publicclass Text5 {
publicstaticvoid main(String[] args) throwsIOException {
ByteArrayInputStreambis=newByteArrayInputStream("6217001820008037164".getBytes());
ByteArrayOutputStreambos=newByteArrayOutputStream();
int m=0;
while(true){
intch=bis.read();//bis.read(byte[],0,len)
if(ch==-1){
break;
}
/*int retch;
if (ch<='z'&&ch>='a') {
retch=Character.toUpperCase((char)ch);
}else{
retch=ch;
}*/
bos.write(ch);//bis.write(byte[],0,len)
m++;
if (m==4) {
bos.write(" ".getBytes());
m=0;
}
}
System.out.println(new String(bos.toByteArray()));
}
}
==============================================================================================
********************************
字符流:Reader,Writer
********************************
一、字符流:
FileReaderFileWriter:--------------------------------------------------------
//文本文件复制
篇二:Java io 流 小结
篇三:java IO文件输入输出流总结
文件输入与输出以流的形式进行,同时根据内容不同分为字节和字符两种
字节流的读取与写入 读取步骤:File→FileInputStream→f.read(bytes)
写入步骤:File→FileOutputStream→f.write(bytes)
演示:读取字节文件即把字节从文件读到内存中 File f=null;//定义文件类的变量,并为空
f=new File(“”);//给变量赋值,表明指向某个文件
因为File没有读写的能力,所以需要用个InputStream
Try{
FileInputStream fis=null;//定义文件字节输入流变量
fis=new FileInputStream(f);//给其赋值,指向目标文件
Byte [] bytes=new byte[1024]; //定义数组,把字节流的字节读到数组中,相当于缓存 Int n=0;//记录读取的字节数
While(n=fis.read(bytes)==-1){//以1024字节大小为单位每次读取字节,从fis字节流中,并放到bytes字节组中,读取字节个数为零即读到末尾,则返回数值-1.
String s=new String(bytes,0,n);// 把字节转换成字符串,0代表:转换开始的下标;
n代表:转换字节的长度。
System.out.println(s);//把字符串打印到显示器上
}
}
演示:写入字节文件即把字节从内存中写道文件里
File f=null;
f=new File(“d:\\ss.txt”);
FileOutputSteam fos=null;
Try{
Fos=new FileOutputStrream(f);
String s=” ”;//定义空字符串
Byte []bytes=new byte[1024];//定义字节数组,存储转换来的字符
Fos.write(s.getBytes());//把String转化为byte
}
举例:图片的拷贝即从一个磁盘拷贝到另一个磁盘;或从一个电脑到另一个电脑
思路:先把图片读到内存,在写入到某个文件里,因为图片是二进制文件,则只能用字节流 File f1=new File(“c:\\a.jpg”);
FileInputStream fis=null;
Fis=new FileInputStream(f1) ;
File f2=new File(“d:\\a.jpg”);
FileOutputStream fos=null;
Fos=new FileOutputStream(f2);
Byte buf[]=new byte[1024];
Int n=0;//记录实际读到的字节数
While((n=fis.read(buf))!==-1){//以buf的大小为单位读取fis文件里的字节,读到内存buf字节组中。N代表一次读的字节的个数,当没有读到字节时,n的返回数值是-1.
Fos.write(buf);
}
字符的读取和写入 文件字符流:FileReader→f.read(c);
FileReader→BufferedReader→f.readLine();
File→FileReader→BufferedReader→reader.readLine();
同理:FileWeader→f.write(c);
FileWeader→BufferedWriter→f.write();
File→FileWeader→BufferedWriter→reader.write();
文件读取字符流对象 FileReader fr=null;//写入到文件
FileWriter fw=null; Try{
Fr=new FileReader(“c:\\test.txt”);//创建输出对象
Fw=new FileWriter(“d:\\vvv.txt”);
Int n=0;//记录实际读取的字符数
Char c[]=new char[1024];//定义字符数组,来存放从文件读取的字符
While((n=fr.read(c))!==-1){
String s=new String(c,0,n);//把字符数组转换为字符串
System.out.println(s);
System.out.println(c);
Fw.write(c);//将字符写入到d盘指定的文件
}
}
第二种:
BuffereReader br=null;
bufferedWriter bw=null;
Try{
//先创建FileReader对象,然后再升级为bufferedReader
//先通过FileReader找到文件,再转成bufferedReader
FileReader fr=new FileReader(“c:\\test.txt”);
Br=new bufferedReader(fr);
//创建FileWriter对象
FileWriter fw=new FileWriter(“d:”);
Bw=new bufferedWriter(fw);
//循环读取文件:
String s=””;
While((s=br.readLine())!==null){
System.out.println(s);
//输出到磁盘
Bw.write(s+”\r\n”);
}
}
第三种情况: //字符流把文件的字符以行的形式读到内存中并显示
File file=null;
FileReader fr=null;
BufferedReader reader=null;
File=new File(“C:\\Users\\Administrator\\ mysql_tmoss.TXT”); Fr=new FileReader(file);
Reader=new Bufferedreader (fr);
String s=null;
s=reader.readLine()//从输入流中读取一行字符,并赋给字符串s
while((s=reader.readLine())!==null){
System.out.println(s);
}
//把上述字符流写到另一个文件或把内存的字符流写到某文件中则 File file1=null;
FileReader fw=null;
BufferedReader writer=null;
File1=new File(“C:\\Users\\Administrator\\ mysql.TXT”);//如果文件存在
则会覆盖,如果文件不存在则重建一
个 Fw=new FileReader(file1);
Writer=new BufferedReader(fw);
Writer.write(s);
从键盘输入字符
// System.in:标准”输出流。此流已打开并准备接受输出数据
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
//从控制台读取一行数据,默认形式为字符串。
String a1=br.readLine();
//转换形式:
Float num=Float.parseFloat(a1);
备注:
FileInputStream:从文件系统中的某个文件中获取输入字节。
FileReader:从文件系统中的某个文件中获取输入字符。
FileInputStream(File file):通过打开一个到实际文件的连接来创建一个FileInputStream,该文件通过文件系统中的File对象file来指定。
FileInputStream(String name):通过打开一个到实际文件的连接来创建一个FileInputStream,该文件通过文件系统中的路径名name 指定
Read():从此输入流中读取一个数据字节
Read(byte[] b):从此输入流中将最多b.length个字节的数据读入一个字节数组中。
Read(byte[] b,int off ,int len):从此输入流中将最多len 个字节的数据读入一个字节数据中。 File 定义一个文件类型的变量,用来指定磁盘中的文件:
File f = new File("C://programfiles/java/java/file.txt");
创建一个文件:f.createMenFile
创建文件夹:File f=new File(“d:\\ff”);
f.mkdir();
如何列出文件夹下有多少个文件:File f=new File(“d:\\ff”);
If(f.isDirectory()){
File lists[]=f.listFiles();
For(int i=0;i<=lists.length;i++){
System.out.println(“文件名”+lists[i].getName());
}
}
记事本读写文件功能实现:
Java中构造函数:
作用:实例化对象
注意:每一个类中都必须有一个构造函数,可以自己定义;如果没有定义,则系统会提供一个默
认的构造函数.
要求:1,构造函数名必须和类名相同,
构造函数只能通过new关键字调用,普通函数需要通过类的实例调用,如果是static方法,则直接通过类名即可调用
构造函数没有返回类型,普通函数必须有返回类类型,不返回用void表示
构造函数是一个特殊的函数,是无返回类型修饰即没有任何返回类型,和类名一模一
样,参数随意。
对象是用相应的构造函数创建出来的,是通过执行相应构造函数里的内容进行的先通过new分配对象内存,然后调用构造函数,执行构造函数
构造这个类的对象的时候就必须执行构造函数·要有构造函数才能构造一个对象··也就
是说·一个构造函数的执行和对象的构造是同步的·或者说·构造函数执行了之后才能构造对象
构造方法 只能是public 类名(){}
一个类中可以定义带参数的构造方法,也可以定义不带参数的构造方法以,这样的定
义叫做重载
有的时候调用这个类不必要用到构造函数。
只有你需要用到这个类的对象的时候才会用到它。
1,在Java程序中,其原始程序的文件名不能随意命名,必须和public类名称一样,因此在一个独立的原始程序里,只能有一个public类,却可以有许多non-public类。若是在一个Java程序中没有一个类是public,那么该Java程序的文件名就可以随意命名了。
2, Java程序是由一个或一个以上的类组合而成,程序起始的主体也是被包含在类之中。这个起始的地方称为main(),
public static void main(String args[]) // main() method,主程序开始 {
…
}
如前一节所述,main() method之前必须加上public static void 这三个标识符。public代表main()公有的method;static表示main()在没有创建类对象的情况下,仍然可以被运行;void则表示main()方法没有返回值。Main后的括号()中的参数String args[]表示运行该程序时所需要的参数,这是固定的用法,如果现在不了解也没有关系,在以后的章节中会一一介绍。
Java中的包、类、方法、参数和变量的名字,可由任意顺序的大小写字母、数字、下划线(_)和美元符号($)组成,但标识符不能以数字开头,不能是Java中的保留关键字。
System.out是指标准输出,通常与计算机的接口设备有关,如打印机、显示器等。其后所连接的println,是由print与line所组成的,意义是将后面括号中的内容打印在标准输出设备显示器上。左、右括号之间的内容,即是欲打印到显示器中的参数,参数可以是字符、字符串、数值、常量或是表达式,参数与参数之间以括号作为间隔。
println()中的变量共有三个,以加号连接这些将被打印的数据。在此,加号是“合并”的意思,并非作为算术运算符号的用途。
当表达式中有类型不匹配时,有下列的处理方法:(1)占用较少字节的数据类型会转换成占用较多字节的数据类型。(2)有short和int类型,则用int类型。(3)字符类型会转换成short类型。(4)int类型转换成float类型。(5)若一个操作数的类型为double,则其它的操作数也会转换成double类型。(6)布尔类型不能转换至其他的类型。
File类是IO包中唯一代表磁盘文件本身的对象,File类定义了一些与平台无关的方法来操作文件,通过调用File类提供的各种方法,能够完成创建,删除和重命名文件,判断文件的读写权限
以及是否存在,设置和查询文件的最近修改时间等操作Java能正确处理约定路径分隔符,如果在windows版本的java下用斜线,路径处理依然正确,记住,如果使用windows使用反斜线的约定,就需要在字符串内使用它的转义序列。Java约定是用unix和url风格的斜线来作为路径的分隔符
下面的构造方法可以用来生成File对象
File(String directoryPath)这里,directoryPath是文件的路径
File 定义了很多获取File对象标准属性的方法。例如:getName()用于返回文件名 getParent()返回父目录名,exists()方法在文件存在的情况下返回true,反之返回false。
然而File类是不对称的,意思是虽然存在可以验证一个简单文件对象属性的很多方法,但是没有相应的方法来改变这些属性。