java -IO流
流是指一连串流动的字符,是以先进先出方式发送信息的通道
Java流的分类
按照流向分: 输入流 VS 输出流
按照单位分: 字节流 VS 字符流
按照角色分: 缓冲流 VS 处理流
流的基类 处理流 缓冲流
InputStream FileInputStream BufferedInputStream
OutputStream FileOutputStream BufferedOutputStream
Reader FileReader BufferedReader
Writer FileWriter BufferedWriter
使用FileInputStream 读文本文件
步骤: 1.引入相关的类 import java.io.IOException; import java.io.FileInputStream; 2.构造文件输入流**FileInputStream** 对象
FileInputStream fis= new FileInputStream(“c:\\test.txt");
3.读取文本文件的数据 fis.available(); fis.read(); 4.关闭文件流对象 fis.close(); @Test public void test01(){ //1.创建File类 File file = new File("HelloWorld.txt"); //2.创建合适的流 FileInputStream fis=null; try { fis = new FileInputStream(file); //3.读操作 //调用FileInputStream 中的read()方法,一个一个字节的去读内容 //直到读取到最后一个内容的时候 返回 -1 int len = fis.read(); while(len!=-1){ //返回值不为-1则循环读取文件中内容并打印 System.out.print((char)len); len=fis.read(); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ //4.关闭流 try { fis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
File类常用方法
/** * boolean exists( ) 是否存在 * boolean delete( ) 删除 * boolean createNewFile( ) 创建一个新的文件 * boolean isFile( ) 是否为文件 * boolean isDirectory( ) 是否为文件夹 * String getPath( ) 获取路径 * String getAbsolutePath( ) 获取绝对路径 * String getName( ) 获取名字 * long length() 获取文件中内容长度 */ @Test public void test01() { // D:\workspace\KH87\workspace\HelloWorld\hello.txt // 创建两个File对象 File f1 = new File("D:\\workspace\\KH87\\workspace\\HelloWorld\\hello.txt"); File f2 = new File("HelloWorld.txt"); System.out.println(f1.isFile()); System.out.println(f1.isDirectory( )); System.out.println(f1.getPath( )); System.out.println(f1.getAbsolutePath( )); System.out.println(f1.getName( )); System.out.println(f1.length()); System.out.println("============="); System.out.println(f2.isFile()); System.out.println(f2.isDirectory( )); System.out.println(f2.getPath( )); System.out.println(f2.getAbsolutePath( )); System.out.println(f2.getName( )); System.out.println(f2.length()); @Test public void test02(){ File file = new File("HelloWorld.txt"); if(file.exists()){ //如果存在则删除 file.delete(); System.out.println("删除成功"); }else { //不存在 创建 try { file.createNewFile(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("文件创建成功"); }
使用FileOutputStream 写文本文件
public class TestOutnput { @Test public void tset01() { File file=new File("word.txt"); FileOutputStream out=null; try { out=new FileOutputStream(file); out.write("Hello".getBytes()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
@Test public void tset02() { File file1=new File("Hello.txt"); File file2=new File("word.txt"); FileInputStream f1=null; FileOutputStream f2=null; try { f1 =new FileInputStream(file1); f2=new FileOutputStream(file2); byte [] aaa=new byte[30]; int read; while((read=f1.read(aaa))!=-1){ f2.write(aaa, 0, read); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { f1.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { f2.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
public class TestReader { @Test public void tset01() { File file1=new File("Hello.txt"); File file2=new File("Hello1.txt"); FileReader f1=null; FileWriter f2=null; try { f1=new FileReader(file1); f2=new FileWriter(file2); char [] abc=new char[30]; int len; while((len=f1.read(abc))!=-1){ f2.write(abc); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { f1.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { f2.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }