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;
    }
}

}

 

posted @ 2016-08-18 15:02  公众号java-codestack  阅读(202)  评论(0编辑  收藏  举报