java文件操作详解

    xiaoxiao2021-03-25  90

    //文件以流的形式来操作:输入、输出流(出入内存) //1.字节流:可读二进制文件及任何类型文件;(InputStream,OutputStream) //2.字符流:可读文本文件,不可读二进制(Reader,Writer)

    //File类的基本用法 package com.test8; import java.io.*;

    public class FileIO {

    public static void main(String[] args) { // TODO Auto-generated method stub File file=new File("d:\\aa.txt"); File file2=new File("d:/aa.txt"); //2种形式等价 System.out.println("文件路径 "+file.getAbsolutePath()+"文件大小: "+file2.length()); File f=new File("d:\\hsp.txt"); if(!f.exists()){ try { f.createNewFile(); //创建文件 } catch (IOException e) { e.printStackTrace(); } } //列出一个文件下的所有文件夹 File f2=new File("D:\\树"); if(f2.isDirectory()){ File lists[]=f2.listFiles(); for(int i=0;i<lists.length;i++) System.out.println("文件:\n"+lists[i].getName()); } File f3=new File("d:\\aa.txt"); FileInputStream fis=null; //因为File没有读写能力,所以需要InputStream流 try { fis=new FileInputStream(f3); byte []bytes=new byte[1024];//定义一个字节数组,相当于缓存 int n=0;//得到实际读取到的字节数 while((n=fis.read(bytes))!=-1){ String s=new String(bytes,0,n);//把字节转为String类型 System.out.println(s); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ //关闭文件流,必须放这,因为finally一定执行 try { fis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } File f4=new File("D:\\aaa.txt"); FileOutputStream fos=null; try { fos=new FileOutputStream(f4); String s="你好啊\r\n";//回车换行 String s2="你也好啊"; //byte []bytes=new byte[1024]; //把String转为bytes【】 fos.write(s.getBytes()); fos.write(s2.getBytes()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //图片copy 1.先把图片读入到内存 2.写入到某个文件 //因为是二进制的,只能用字节流 File f5=new File("c\\a.jpg"); FileInputStream fis2=null; FileOutputStream fos2=null; try { fis2=new FileInputStream(f5); fos2=new FileOutputStream("d:\\a.jpg"); byte buf[]=new byte[1024]; //byte为一位 int n=0; while((n=fis2.read(buf))!=-1){ //循环读取,读入到buf fos2.write(buf); //输出到指定文件 ,从buf中读出 } } catch (Exception e) { e.printStackTrace(); }finally{ try{ fis2.close(); fos2.close(); }catch(Exception e){ e.printStackTrace(); } } //字符流案例 copy文件 FileReader fr=null; //输入流 FileWriter fw=null; //输出流,对应OutputStream try { fr=new FileReader("c:\\test.txt"); fw=new FileWriter("d:\\zzz.txt"); //读入内存 char c[]=new char[1024]; //字符数组,2个字节 int n=0; //记录实际读取到的字符数 while((n=fr.read(c))!=-1){

    // String s=new String(c,0,n); // System.out.println(c); //输出读取到的c fw.write(c); //把c输出到fw }

    } catch (Exception e) { e.printStackTrace(); }finally{ try { fr.close(); fw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //演示缓冲字符流 提高效率 一次读取更多内容 BufferedReader 直接操作string BufferedReader br=null; BufferedWriter bw=null; //先创建FileReader对象 FileReader fr3=null; FileWriter fw3=null; try { fr3 = new FileReader("c:\\a.txt"); br=new BufferedReader(fr3); fw3=new FileWriter("d:\\d.txt"); bw=new BufferedWriter(fw3); //循环读取 String s=""; while((s=br.readLine())!=null){ // System.out.println(s); //输出到磁盘 bw.write(s+"\r\n"); //其本身不完成换行,自己加回车换行 } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { br.close(); bw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }

    } }

    转载请注明原文地址: https://ju.6miu.com/read-23431.html

    最新回复(0)