• IO总结


    1.文件拷贝

    package com.mi.demo;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    public class IOTEST {
    
        public static void main(String[] args) {
            byte[] buffer = new byte[1024];
            int numberRead = 0;
            
            FileInputStream fis = null;
            FileOutputStream fos = null;
            try {
                
                File file1 = new File("C:/Users/admin/Desktop/CSXY.txt");
                File file2 = new File("C:/Users/admin/Desktop/copy.txt");
                System.out.println(file1.getTotalSpace()/(1024*1024*1024)+"G");
                System.out.println(file1.getFreeSpace()/(1024*1024*1024)+"G");
                System.out.println(file1.getUsableSpace()/(1024*1024*1024)+"G");
                System.out.println(file1.getName());
                System.out.println(file1.getParent());
                System.out.println(file1.getPath());
                System.out.println(file1.getCanonicalPath());
                System.out.println(file1.getParentFile());
                System.out.println(file1.getAbsolutePath());
                
                fis = new FileInputStream(file1);
                fos = new FileOutputStream(file2);
                
                while((numberRead=fis.read(buffer))!=-1){
                    fos.write(buffer, 0, numberRead);
                }
                
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                try {
                    fis.close();
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                
            }
            
        }
    }

     2.世轩上机题,时隔一年,嘛蛋还是这种方法

    package com.mi.demo;
    
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.OutputStreamWriter;
    import java.util.ArrayList;
    import java.util.Random;
    import java.util.Scanner;
    
    
    public class IOTEST {
        
    public static void main(String[] args) throws IOException {
        Random r = new Random();  
        int i=r.nextInt(1000001);  
        FileOutputStream foStream = new FileOutputStream(new File("D:/a.txt"));
        OutputStreamWriter pw=new OutputStreamWriter(foStream);
        BufferedWriter bufferedWriter  = new BufferedWriter(pw);
        for (int j = 0; j <i; j++) {
            bufferedWriter.newLine();
            bufferedWriter.write(r.nextInt(1000001));
        }
        pw.close();
        
            FileReader reader=new FileReader(new File("D:/a.txt"));
            ArrayList<Integer> n=new ArrayList<Integer>();
            int count=1;
            BufferedReader br = new BufferedReader (reader);
            String s;
            Scanner sc = new Scanner(System.in);
            s=sc.nextLine();
            if(IOTEST.valueOfCheck(s)){
            String number;
             while ((number = br.readLine() )!=null) {
                 
                 if(number.equals(s)){
                     n.add(count);
                 }
                 count++;
              }
             System.out.println(n.size());
             for (Integer integer : n) {
                System.out.println(integer);
            }
            }
            else System.out.println("输入的不是数字");
            reader.close();
        
    }
    
    public static boolean valueOfCheck(String s){
        try {
        int num=Integer.valueOf(s);//把字符串强制转换为数字
        return true;//如果是数字,返回True
        } catch (Exception e) {
        return false;
        }
    }
    
    }
  • 相关阅读:
    python-学习 补充模块;面向对象程序设计
    python学习- 常用模块与re正则
    python-学习 协程函数 模块与包
    python-学习 初级atm小脚本、函数嵌套、装饰器、生成器、迭代器、三元表达式
    模拟登陆古诗文网
    正则表达式(括号)、[中括号]、{大括号}的区别
    MongoDB的基本操作
    语音合成以及语音识别
    flask中的CBV,flash,Flask-Session,WTForms
    Flask基础2 蓝图,实例化配置,app对象配置,特殊装饰器
  • 原文地址:https://www.cnblogs.com/tingbogiu/p/5783954.html
Copyright © 2020-2023  润新知