zoukankan      html  css  js  c++  java
  • 最近在研究IO

    import java.io.File;
    import java.io.IOException;
    
    public class Demo11_1 {
    
        public static void main(String[] args) {
            //创建一个文件对象
            //File f = new File("d:\123.txt");
            
            //得到文件的路劲
            //System.out.println("文件路径:"+f.getAbsolutePath());
            //得到文件的大小
            //System.out.println("文件大小"+f.length());
            //System.out.println("可读"+f.canRead());
            
            //创建文件和创建文件夹
            //File f = new File("d:\zy.txt");
            //if(!f.exists()){
                //可以创建
            //    try {
            //        f.createNewFile();
                
            //    } catch (IOException e) {
                    // TODO Auto-generated catch block
            //        e.printStackTrace();
            //    }
            //}else{
            //    System.out.println("已存在");
            //}
            
            //File f = new File("d:\ff\zy.txt");
            //if(f.isDirectory()){
            //    System.out.println("文件夹存在");
            //}else{
            //    //创建
            //    f.mkdir();
            //}
            
            //列出一个文件夹下面的所有文件
            //File f = new File("d:\ff");
            //if(f.isDirectory()){
            //    File lists[] = f.listFiles();
            //    for(int i=0;i<lists.length;i++){
            //        System.out.println("文件名:"+lists[i].getName());
            //    }
            //}
            
            //测试在文件加下创建文件,并判断文件夹是否存在
            File f = new File("d:\zy");
            if(f.exists()){
                System.out.println("文件夹存在");
                
            }else{
                f.mkdir();
            }
            File fn = new File("d:\zy\zy.txt");
            if(fn.exists()){
                System.out.println("文件已存在");
            }else{
                System.out.println("文件不存在,创建文件");
                try {
                    fn.createNewFile();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    
    /**
     * 演示FileInputStream类的使用
     * @author zy
     *
     */
    public class Demo11_2 {
        public static void main(String[] args) throws IOException {
            
            File f = new File("d:\123.txt"); 
            FileInputStream fis=null;
            try {
                fis = new FileInputStream(f);
                //定义一个字节数组
                byte[] bytes = new byte[1024];
                int n=0;
                //读取结束返回-1
                while((n=fis.read(bytes))!=-1){
                    //把字节转换成string
                    String s = new String(bytes,0,n);
                    System.out.println(s);
                }
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally{
                //关闭文件流
                fis.close();
                
            }
            
        }
    
        
        
    }
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    /**
     * 演示FileOutputStream的使用
     * @author zy
     *
     */
    public class Demo11_3 {
    
        public static void main(String[] args) {
            
            File f = new File("d:\test.txt");
            //判断文件是否存在
            if(!f.exists()){
                try {
                    f.createNewFile();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            //字节输出
            FileOutputStream fos = null;
            try {
                fos = new FileOutputStream(f);
                
                String s = "小灰灰
    ";
                String sa = "喜洋洋";
                //定义数组
                //byte [] bytes=new byte[1024];
                //把string转换成bytes数组
                
                fos.write(s.getBytes());
                fos.write(sa.getBytes());
            } catch (Exception e) {
                // TODO: handle exception
            }finally{
                try {
                    fos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    
    public class Demo12_4 {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    
            //先把图片读入到内存--》写入到某个文件
            //因为是二进制文件,因此只能用字节流完成
            //输入流
            //File f1=new File("E:\1.jpg");
            FileInputStream fis=null;
            //输出流
            FileOutputStream fos=null;
            try {
                fis=new FileInputStream("e:\1.jpg");
                fos=new FileOutputStream("f:\1.jpg");
                byte buf[]=new byte[512];
                int n=0;//记录实际读取到的字节数
                //循环读取
                while((n=fis.read(buf))!=-1){
                    //输出到指定文件
                    fos.write(buf);
                }
                
            } catch (Exception e) {
                e.printStackTrace();
                // TODO: handle exception
            }finally{
                //关闭打开的文件流
                try {
                    fis.close();
                    fos.close();
                } catch (Exception e2) {
                    // TODO: handle exception
                }
            }
        }
    
    }
    import java.io.FileReader;
    import java.io.FileWriter;
    
    public class Demo12_5 {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    
            //文件取出字符流对象(输入流)
            FileReader fr=null;
            //写入到文件(输出流)
            FileWriter fw=null;
            
            try {
                //创建fr对象
                fr=new FileReader("e:\test.txt");
                //创建输出对象
                fw=new FileWriter("f:\test.txt");
                int n=0;//记录实际读取的字符数
                //读入到内存
                char c[]=new char[1024];
                while((n=fr.read(c))!=-1){
                    //String s=new String(c,0,n);//避免乱码
                    //System.out.println(s);
                    fw.write(c);
                }
            } catch (Exception e) {
                // TODO: handle exception
            }finally{
                try {
                    fr.close();
                    fw.close();
                } catch (Exception e2) {
                    // TODO: handle exception
                }
            }
        }
    
    }
    public class Demo12_6 {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    
            BufferedReader br=null;
            BufferedWriter bw=null;
            try {
                //先创建FileReder对象
                FileReader fr=new FileReader("e:\test.txt");
                br=new BufferedReader(fr);
                
                //创建FileWrite对象
                FileWriter fw=new FileWriter("f:\test.txt");
                bw=new BufferedWriter(fw);
                //循环读取文件
                String s="";
                while((s=br.readLine())!=null)
                {
                    //System.out.println(s);
                    //输出到磁盘
                    bw.write(s+"
    ");
                }
            } catch (Exception e) {
                // TODO: handle exception
            }finally{
                try {
                    br.close();
                    bw.close();
                } catch (Exception e2) {
                    // TODO: handle exception
                }
            }
        }
    
    }
  • 相关阅读:
    论文阅读笔记(四十五)【CVPR2020、AAAI2020】:Relation Feature 的应用
    论文阅读笔记(四十四)【ICCV2017】:Deeply-Learned Part-Aligned Representations for Person Re-Identification
    论文阅读笔记(四十三)【AAAI2020】:Rethinking Temporal Fusion for Video-based Person Re-identificationon Semantic and Time Aspect
    论文阅读笔记(四十二)【AAAI2019】:STA:Spatial-Temporal Attention for Large-Scale Video-based Person Re-Identification
    论文阅读笔记(四十一)【CVPR2017】:Learning Deep Context-aware Features over Body and Latent Parts for Person Re-identification
    论文阅读笔记(四十)【CVPR2017】:Human Semantic Parsing for Person Re-identification
    论文阅读笔记(三十九)【CVPR2017】:Spindle Net Person Re-identification with Human Body Region Guided Feature Decomposition and Fusion
    论文阅读笔记(三十八)【AAAI2020】:Semantics-Aligned Representation Learning for Person Re-identification
    学习笔记_SpringBoot2
    学习笔记_SpringBoot1
  • 原文地址:https://www.cnblogs.com/zyxiaohuihui/p/4534722.html
Copyright © 2011-2022 走看看