IO流常用基类:按操作数据单位不同分为:字节流(8bit),字符流(16bit) 。按数据流的流向不同分为:输入流,输出流。
字节流的抽象基类:InputStream ,OutputStream。
字符流的抽象基类:Reader , Writer。
注:由这四个类派生出来的子类名称都是以其父类名作为子类名的后缀。
• 如:InputStream的子类FileInputStream。
• 如:Reader的子类FileReader。
二字符流
1.创建文件
创建流对象,建立数据存放文件
• FileWriter fw = new FileWriter(“Test.txt”);
调用流对象的写入方法,将数据写入流
• fw.write(“text”);
关闭流资源,并将流中的数据清空到文件中。
• fw.close();
FileWriter fw = null; try{ fw = new FileWriter("Test.txt"); fw.write("text"); } catch (IOException e){ System.out.println(e.toString()); } finally{ If(fw!=null) try{ fw.close(); } catch (IOException e){ System.out.println(e.toString()); } }
2.读取文件
建立一个流对象,将已存在的一个文件加载进流。
• FileReader fr = new FileReader(“Test.txt”);
创建一个临时存放数据的数组。
• char[] ch = new char[1024];
调用流对象的读取方法将流中的数据读入到数组中。
• fr.read(ch);
FileReader fr = null; try{ fr = new FileReader("c:\\test.txt"); char[] buf = new char[1024]; int len= 0; while((len=fr.read(buf))!=-1){ System.out.println(new String(buf,0,len)); } } catch (IOException e){ System.out.println("read-Exception :"+e.toString()); } finally{ if(fr!=null){ try{ fr.close(); } catch (IOException e){ System.out.println("close-Exception :"+e.toString()); }
3.字符流缓冲区
缓冲区的出现提高了对数据的读写效率。
对应类
• BufferedWriter
• BufferedReader
缓冲区要结合流才可以使用。
在流的基础上对流的功能进行了增强。
import java.io.*; class BufferedReaderDemo { public static void main(String[] args) throws IOException { //创建一个读取流对象和文件相关联。 FileReader fr = new FileReader("buf.txt"); //为了提高效率。加入缓冲技术。将字符读取流对象作为参数传递给缓冲对象的构造函数。 BufferedReader bufr = new BufferedReader(fr); String line = null; while((line=bufr.readLine())!=null) { System.out.print(line); } bufr.close(); } }
三字节流
基本操作与字符流类相同,但它不仅可以操作字符,还可以操作其他媒体文件。
import java.io.*;
class CopyPic
{
public static void main(String[] args)
{
FileOutputStream fos = null;
FileInputStream fis = null;
try
{
fos = new FileOutputStream("c:\\2.bmp");
fis = new FileInputStream("c:\\1.bmp");
byte[] buf = new byte[1024];
int len = 0;
while((len=fis.read(buf))!=-1)
{
fos.write(buf,0,len);
}
}
catch (IOException e)
{
throw new RuntimeException("复制文件失败");
}
finally
{
try
{
if(fis!=null)
fis.close();
}
catch (IOException e)
{
throw new RuntimeException("读取关闭失败");
}
try
{
if(fos!=null)
fos.close();
}
catch (IOException e)
{
throw new RuntimeException("写入关闭失败");
}
}
}
}
字节流缓冲区:
import java.io.*;
class CopyMp3
{
public static void main(String[] args) throws IOException
{
long start = System.currentTimeMillis();
copy_2();
long end = System.currentTimeMillis();
System.out.println((end-start)+"毫秒");
}
public static void copy_2()throws IOException
{
MyBufferedInputStream bufis = new MyBufferedInputStream(new FileInputStream("c:\\9.mp3"));
BufferedOutputStream bufos = new BufferedOutputStream(new FileOutputStream("c:\\3.mp3"));
int by = 0;
//System.out.println("第一个字节:"+bufis.myRead());
while((by=bufis.myRead())!=-1)
{
bufos.write(by);
}
bufos.close();
bufis.myClose();
}
//通过字节流的缓冲区完成复制。
public static void copy_1()throws IOException
{
BufferedInputStream bufis = new BufferedInputStream(new FileInputStream("c:\\0.mp3"));
BufferedOutputStream bufos = new BufferedOutputStream(new FileOutputStream("c:\\1.mp3"));
int by = 0;
while((by=bufis.read())!=-1)
{
bufos.write(by);
}
bufos.close();
bufis.close();
}
}
四转换流 InputStreamReader,OutputStreamWriter
转换流的由来:1.字符流与字节流之间的桥梁;2.方便了字符流与字节流之间的操。。
转换流的应用: 字节流中的数据都是字符时,转成字符流操作更高效。
class TransStreamDemo
{
public static void main(String[] args) throws IOException
{
System.setIn(new FileInputStream("PersonDemo.java"));
System.setOut(new PrintStream("zzz.txt"));
//键盘的最常见写法。
BufferedReader bufr =
new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(System.out));
String line = null;
while((line=bufr.readLine())!=null)
{
if("over".equals(line))
break;
bufw.write(line.toUpperCase());
bufw.newLine();
bufw.flush();
}
bufr.close();
}
}
五标准输入输出流
System类中的字段:in,out。
它们各代表了系统标准的输入和输出设备。
默认输入设备是键盘,输出设备是显示器。
System.in的类型是InputStream.
System.out的类型是PrintStream,是OutputStream的子类FilterOutputStream 的子类.
六File类
java.io.File类:文件和目录路径名的抽象表示形式,与平台无关。File 能新建、删除、重命名文件和目录,但File 不能访问文件内容本身。如果需要访问文件内容本身,则需要使用输入/输出流。File对象可以作为参数传递给流的构造函数。class FileDemo { public static void main(String[] args) throws IOException { method_5(); } public static void method_5() { File f1 = new File("c:\\Test.java"); File f2 = new File("d:\\hahah.java"); sop("rename:"+f2.renameTo(f1)); } public static void method_4() { File f = new File("file.txt"); sop("path:"+f.getPath()); sop("abspath:"+f.getAbsolutePath()); sop("parent:"+f.getParent());//该方法返回的是绝对路径中的父目录。如果获取的是相对路径,返回null。 //如果相对路径中有上一层目录那么该目录就是返回结果。 } public static void method_3()throws IOException { File f = new File("d:\\java1223\\day20\\file2.txt"); //f.createNewFile(); //f.mkdir(); //记住在判断文件对象是否是文件或者目的时,必须要先判断该文件对象封装的内容是否存在。 //通过exists判断。 sop("dir:"+f.isDirectory()); sop("file:"+f.isFile()); sop(f.isAbsolute()); } public static void method_2() { File f = new File("file.txt"); //sop("exists:"+f.exists()); //sop("execute:"+f.canExecute()); //创建文件夹 File dir = new File("abc\\kkk\\a\\a\\dd\\ee\\qq\\aaa"); sop("mkdir:"+dir.mkdirs()); } public static void method_1()throws IOException { File f = new File("file.txt"); // sop("create:"+f.createNewFile()); //sop("delete:"+f.delete()); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>//创建File对象 public static void consMethod() { //将a.txt封装成file对象。可以将已有的和为出现的文件或者文件夹封装成对象。 File f1 = new File("a.txt"); // File f2 = new File("c:\\abc","b.txt"); File d = new File("c:\\abc"); File f3 = new File(d,"c.txt"); sop("f1:"+f1); sop("f2:"+f2); sop("f3:"+f3); File f4 = new File("c:"+File.separator+"abc"+File.separator+"zzz"+File.separator+"a.txt"); } public static void sop(Object obj) { System.out.println(obj); } }