输出流:数据从程序(内存)到数据源(文件)的路径
如何判断是输入流、输出流?以内存为参照,如果数据流向内存流动,则是输入流;反之,则是输出流。
java流分为两种流1、字节流:可以用于读写二进制文件及任何类型文件byte2、字符流:可以用于读写文本文件,不能操作二进制文件字节流 字符流输入 InputStream Reader输出 OutputStream Writer常用io流--文件对象目的:文件数据源File类介绍(文件流对象中最为重要的File类,对File了解后对子类理解会更加容易)
/** * File类的基本用法 */import java.io.*;//必需加载IO包public class Io01 { public static void main(String[] args) { //创建一个文件对象 File f1=new File("e://aa.txt"); //得到文件的路径 System.out.PRintln("文件路径"+f1.getAbsolutePath()); //得到文件的大小,字节数 System.out.println("文件的大小"+f1.length()); //创建文件夹 File f3=new File("e://ff"); //判断文件夹是否存在 if(f3.isDirectory()){ System.out.println("文件夹存在,不能创建!"); }else{ //创建文件夹 f3.mkdir(); } //创建文件和创建文件夹 File f2=new File("e://ff//hsp.txt"); //判断文件是否存在 if(!f2.exists()){ //可以创建 try { f2.createNewFile();//创建一个新文件 } catch (Exception e) { e.printStackTrace(); } }else{ System.out.println("文件存在,不能创建!"); } //列出一个文件夹下面的所有文件 File f4=new File("e://ff"); //判断文件夹是事存在 if(f4.isDirectory()){ //将文件夹的文件,传给lists数组 File lists[]=f4.listFiles(); //遍历数组 for(int i=0;i<lists.length;i++){ //输出文件夹下所有文件文件名 System.out.println("显示出文件名是"+lists[i].getName()); } } }}-------------------------------------------------FileInputStream的对象把文件读入到内存/** * File类的基本用法 * io流--文件字节流 * FileInputStream类的使用 */import java.io.*;public class Io02 { public static void main(String[] args) { //得到一个文件对象,f指向e:/ff/hsp.txt文件 File f=new File("e://ff//hsp.txt"); FileInputStream fis=null; try { //因为File没有读写的能力,所以需要使用InputStream类 fis=new FileInputStream(f); //定义一个字节数组,相当于缓存 byte []bytes=new byte[1024]; int n=0;//得到实际读取到的字节数 //循环读取 while((n=fis.read(bytes))!=-1){ //把字节转成String String s=new String(bytes,0,n); System.out.println(s); } } catch (Exception e) { e.printStackTrace(); }finally{ //关闭文件流必需放在finally语句块中 try { fis.close(); } catch (Exception e) { e.printStackTrace(); } } }}从键盘接收用户输入内容,并保存到文件中(文件字节输出流,目的:FileOutputStream类)/** * File类的基本用法 * io流--文件字节流 * FileOutputStream类的使用 */import java.io.*;public class Io03 { public static void main(String[] args) { File f=new File("e://ff//ss.txt");//直接覆盖写同一个文件 //字节输出流 FileOutputStream fos=null; if(f.exists()){ System.out.println("文件已存在"); }else{ try { fos=new FileOutputStream(f); String s="hello,world!/r/n"; String s1="中国人"; fos.write(s.getBytes()); fos.write(s1.getBytes()); } catch (Exception e) { e.printStackTrace(); }finally{ try { fos.close(); } catch (Exception e2) { e2.printStackTrace(); } } } }}图片拷贝/** * File类的基本用法 * io流--文件字节流 * 图片拷贝--FileInputStream类与 FileOutputStream类 */import java.io.*;public class Io04 { public static void main(String[] args) { //先将图片读入到内存,再将内存中的图片写入到某个文件 //因为二进制文件只能拿使用字节流来处理 //输入流 FileInputStream fis=null; //输出流 FileOutputStream fos=null; try { fis=new FileInputStream("e://ff//a.jpg"); fos=new FileOutputStream("e://a.jpg"); byte buf[]=new byte[1024]; int n=0;//记录实际读取到的字节数 //循环读取图片 while((n=fis.read(buf))!=-1){ //输出到指定文件 fos.write(buf); } } catch (Exception e) { e.printStackTrace(); }finally{ //一定要关闭打开的文件流 try { fis.close(); fos.close(); } catch (Exception e) { e.printStackTrace(); } } }}(文件字符输入、输出流,目的:FileReader、FileWriter类)读取一个文件并写入到另一个文件中char[]来中转/** * File类的基本用法 * io流--文件字符流,只能用于完全为字符的文件 * TXT文件拷贝--FileReader类与 FileWriter类 */import java.io.*;public class Io05 { public static void main(String[] args) { //文件取出字符流对象(输入流) FileReader fr=null; //写入到文件(输出流) FileWriter fw=null; try { //创建fr对象 fr=new FileReader("e://ff//hsp.txt"); //创建输出对象 fw=new FileWriter("e://hsp.txt"); //创建字符数组 char c[]=new char[1024]; int n=0; //读入到内存 while((n=fr.read(c))!=-1){ //控制台输出TXT文件内容 String s=new String(c,0,n); System.out.println(s); fw.write(c, 0, n); } } catch (Exception e) { e.printStackTrace(); }finally{ try { fr.close(); fw.close(); } catch (Exception e) { e.printStackTrace(); } } }}(文件缓冲字符流,目的:BufferedReader和BufferedWriter类介绍,直接操作String)/** * File类的基本用法 * io流--缓冲字符流 * BufferedReader类与BufferedWriter类 */import java.io.*;public class Io06 { public static void main(String[] args) { BufferedReader br=null; BufferedWriter bw=null; try { //先创建FileReader对象 FileReader fr=new FileReader("e://ff//hsp.txt"); br=new BufferedReader(fr); //创建FileWriter对象 FileWriter fw=new FileWriter("e://hsp1.txt"); bw=new BufferedWriter(fw); //循环读取 String s=""; while((s=br.readLine())!=null){ //输出到磁盘 bw.write(s+"/r/n"); } } catch (Exception e) { e.printStackTrace(); }finally{ try { br.close(); bw.close(); } catch (Exception e) { e.printStackTrace(); } } }}
新闻热点
疑难解答