课程总结:
本周主要对javaio进行了学习
1.操作文件的类--File
File类的构造方法:public File(String pathname)----实例化File类的时候,必须设置好路径。
例如File f=new File(“d:”+File.separator+“test”+File.separator+“demo.txt”);
2.RandomAccessFile类
3.字节流与字符流基本操作
字节输出流:OutputStream
OutputStream是整个io包中字节输出流的最大父类
public abstract class OutputStream extends Object implements Closeable,Flushable
此类是一个抽象类,如果想用此类的话,必须通过子类实例化对像。
FileOutputStream子类,此类的构造方法
public FileOutputStream(File file)throws FileNotFoundExceptio
package org.lxh.demo12.byteiodemo;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
public class OutputStreamDemo1{
public static void main(String[]args)throws Exception{
//第1步:使用File类找到一个文件
File f=new File("d:"+File.separator+"test.txt");
//第2步:通过子类实例化父类对象
OutputStream out=null;
out=new FileOutputStream(f);
//第3步:进行写操作
String str="hello wrold";
byte b[]=str.getBytes();
out.write(b);
//第4步:关闭输出流
out.close();
}
}
字符输出流Writer
Writer本身就是一个字符流的输出类,此类定义如下
public abstract class Writer extends Object implements Appendable,Closeable,Flushable
字符输出流:Reader
Reader类时使用字符的方式从文件之中取出数据,Reader类的定义如下
public abstract class Reader extends Object implements Readable,Closeable
浙公网安备 33010602011771号