黑马程序员——Java基础--IO流(1)

来源:互联网 发布:excel数据标签插件 编辑:程序博客网 时间:2024/05/02 00:51
------- android培训、java培训、期待与您交流! ----------

一  File类

 File类是文件和目录路径名的抽象表示形式。常用的方法如下:

 构造方法:

 public File(String pathname)

public File(String parent,String child)

public File(File parent,String child)

import java.io.*;public class FileDemo1 {public static void main(String[] args) {method_2();}/* *   传递File类型父路径,String类型子路径 */public static void method_2(){File parent = new File("E:");//创建File对象,传递File父路径,字符串子路径/File file = new File(parent,"eclipse");System.out.println(file);}/* *   传递字符串父路径,字符串的子路径 */ public static void method_1(){ //创建File对象,传递字符串父路径,字符串子路径 File file = new File("E:","eclipse"); System.out.println(file); }/* *    传递字符串的路径 */public static void method(){//创建File类对象,传递字符串路径File file = new File("C:\\ a.txt");System.out.println(file);}}

创建方法:

public boolean createNewFile()创建文件

public boolean mkdir()创建单级文件夹

public boolean mkdirs()创建多级或单级文件夹

import java.io.*;public class FileDemo2 {public static void main(String[] args)throws IOException {method_1();}/* * File类创建目录(文件夹) */ public static void method_1(){ File file = new File("c:\\abc"); boolean b = file.mkdirs(); System.out.println(b); }/* * File类创建文件 */public static void method()throws IOException{File file = new File("c:\\a.txt");//调用方法createNewFileboolean b = file.createNewFile();System.out.println(b);}}


重命名功能:

public boolean renameTo(File dest) 数据源对象调用方法,传递数据目的对象。

获取功能:

public String getName()获取路径名字

public File[] listFiles()获取目录下的文件和文件夹

import java.io.*;public class FileDemo1 {public static void main(String[] args) {method_2();method_1();method();}/* *  获取File构造方法中封装的路径下的文件和子目录 */public static void method_2(){File file = new File("e:\\eclipse");//调用方法listFiles()封装的路径下文件和子目录File[] files = file.listFiles();for(File f : files){System.out.println(f);}}/* *   获取File构造方法中封装的路径下的文件和子目录 */public static void method_1(){File file = new File("e:\\Eclipse");//调用方法list()获取,封装的路径下文件和子目录String[] str = file.list();for(String s : str){System.out.println(s);}}public static void method(){//类名调用静态方法listRoots获取系统根File file =new File("e:\\Eclipse");String name=file.getName();System.out.println(name);}}

二  IO流

IO流用来处理设备之间的数据传输。

IO流分类:

 按照数据流向:输入流(读入数据);输出流(写入数据)。

 按照数据类型:字节流;字符流。

字节流的抽象基类:

InputStream,OutputStream。

字符流的抽象基类:

Reader, Writer。

FileOutputStream:

构造方法:

FileOutputStream(File file)

FileOutputStream(String name)

常用成员方法:

public void write(int b)

public void write(byte[] b)

public void write(byte[] b,int off,int len)

FileInputStream:

构造方法:

FileInputStream(File file)

FileInputStream(String name)

常用成员方法:

public int read()一次读取一个字节

public int read(byte[] b)一次读取一个字节数组

字节缓冲输出流

BufferedOutputStream

字节缓冲输入流

BufferedInputStream

import java.io.*;public class Copy {public static void main(String[] args) {copy_4();}/* * 定义方法,实现第四种复制,缓冲区读写字节数组 */public static void copy_4() {// 创建2个缓冲区对象BufferedInputStream bis = null;BufferedOutputStream bos = null;try {// 匿名对象传递参数bis = new BufferedInputStream(new FileInputStream("c:\\m.exe"));bos = new BufferedOutputStream(new FileOutputStream("d:\\m.exe"));// 读写自己诶数组byte[] bytes = new byte[1024*100];int len = 0;while ((len = bis.read(bytes)) != -1) {bos.write(bytes, 0, len);}} catch (IOException ex) {ex.printStackTrace();throw new RuntimeException("复制失败");} finally {try {if (bos != null)bos.close();} catch (IOException ex) {throw new RuntimeException("资源关闭失败");} finally {try {if (bis != null)bis.close();} catch (IOException ex) {throw new RuntimeException("资源关闭失败");}}}}/* * 定义方法,实现第三种复制,缓冲区读写单个字节 */public static void copy_3() {// 创建2个缓冲区对象BufferedInputStream bis = null;BufferedOutputStream bos = null;try {// 匿名对象传递参数bis = new BufferedInputStream(new FileInputStream("c:\\src.zip"));bos = new BufferedOutputStream(new FileOutputStream("d:\\src.zip"));// 读取1个字节,写1个字节int len = 0;while ((len = bis.read()) != -1) {bos.write(len);}} catch (IOException ex) {ex.printStackTrace();throw new RuntimeException("复制失败");} finally {try {if (bos != null)bos.close();} catch (IOException ex) {throw new RuntimeException("资源关闭失败");} finally {try {if (bis != null)bis.close();} catch (IOException ex) {throw new RuntimeException("资源关闭失败");}}}}/* * 定义方法,实现第二种复制,读写字节数组 */public static void copy_2() {FileInputStream fis = null;FileOutputStream fos = null;try {fis = new FileInputStream("c:\\m.exe");fos = new FileOutputStream("d:\\m.exe");int len = 0;// 定义字节数组 1KBbyte[] bytes = new byte[1024*100];while ((len = fis.read(bytes)) != -1) {fos.write(bytes, 0, len);}} catch (IOException ex) {ex.printStackTrace();throw new RuntimeException("复制失败");} finally {try {if (fos != null)fos.close();} catch (IOException ex) {throw new RuntimeException("资源关闭失败");} finally {try {if (fis != null)fis.close();} catch (IOException ex) {throw new RuntimeException("资源关闭失败");}}}}/* * 定义方法,实现第一种复制,读写单个字节 */public static void copy_1() {FileInputStream fis = null;FileOutputStream fos = null;try {fis = new FileInputStream("c:\\src.zip");fos = new FileOutputStream("d:\\src.zip");int len = 0;while ((len = fis.read()) != -1) {fos.write(len);}} catch (IOException ex) {ex.printStackTrace();throw new RuntimeException("复制失败");} finally {try {if (fos != null)fos.close();} catch (IOException ex) {throw new RuntimeException("资源关闭失败");} finally {try {if (fis != null)fis.close();} catch (IOException ex) {throw new RuntimeException("资源关闭失败");}}}}}


0 0
原创粉丝点击