java文件相关

java文件相关

/*************************************************************************
    > File Name: Try_Wenjian.java
    > Author: yijiull
    > Mail: [email protected] 
    > Created Time: 2017年11月11日 星期六 17时17分02秒
 ************************************************************************/
//字节流处理
//import java.io.FileInputStream;
//import java.io.FileOutputStream;
import java.io.*;
import java.util.Scanner;
import java.io.IOException;
public class Try_Wenjian{
    public static void main(String[] args) throws IOException{
        int size;
        FileInputStream f = new FileInputStream("/home/yijiull/javacode/in.txt");
        FileOutputStream fout = new FileOutputStream ("/home/yijiull/javacode/copy.txt");
        //使用缓冲流
        BufferedInputStream bis = new BufferedInputStream(f);
        BufferedOutputStream bos = new BufferedOutputStream(fout);
        //可以将下面的f和fout换成bis和bos, 用缓冲流加速文件读写
        System.out.println("Total Available Bytes: " + (size = f.available()));;
        int n =size / 3;
        System.out.println("First " + n + " bytes of the file one read() at a time");
        //使用read() 和 write()
        for(int i = 0; i < n; i++){
            fout.write(f.read());
        }
        System.out.println("Still available " + f.available());
        //使用read(byte[] b) 和 write(byte[] b)
        System.out.println("Reading the next " + n + " bytes with one read(b[])");
        byte b[] = new byte[n];
        if(f.read(b) != n) {
            System.out.println("Could not read " + n + " bytes");
        }
        fout.write(b);
        System.out.println("Still Available " + f.available());
        //使用read(b, offset, len)和write(b, offset, len)
        System.out.println("Reading the rest bytes with read(b, offset, len)");
        int count = 0;
        while((count = f.read(b, 0, n)) != -1){
            fout.write(b, 0, count);
        }
        System.out.println("Still Available " + f.available());
        f.close();
        fout.flush();
        fout.close();




        //随机存取文件
        String filename = "temp.txt";
        RandomAccessFile raf = null;
        String s1 = "Happy double 11 day!";
        String s2 = "双十一快乐!";
        long length, pos;
        try{
            //构件对象
            raf = new RandomAccessFile(filename, "rw");
            raf.writeChars(s1);
            pos = raf.getFilePointer();
            length = s1.length();
            System.out.println("第一个字符串的长度: " + length);
            //一个字符用两个字节表示,内存中的表示和文件中的表示一致
            System.out.println("写入第一个字符串后,文件指针: " + pos);
            pos = raf.getFilePointer();
            raf.writeChars(s2);
            raf.seek(pos);
            for(int i = 0; i < s2.length(); i++){
                System.out.print(raf.readChar());
            }
            System.out.println();
            pos = raf.getFilePointer();
            System.out.println("文件写入" + s2.length() + "个字符后,文件指针: " + pos);
        }catch(FileNotFoundException ex){
            System.out.println("文件不存在");
        }catch(IOException ex){
        }


        //Scanner
        Scanner s = null;
        PrintWriter pw = null;
        try{
            //从源文件读入, 使用Scanner读入由空白字符分割的文本内容是很方便的
            s = new Scanner(new File("date.txt"));
            //使用PrintWriter进行格式化输出
            pw = new PrintWriter("dest.txt");
            while(s.hasNextLine()){
                String str = s.nextLine();
                int sum = getLinrSum(str);
                pw.println(str + '\t' + sum);
            }
        }catch(FileNotFoundException e){
            e.printStackTrace();
        }finally{
            if(s != null){
                s.close();
            }
            if(pw != null){
                pw.close();
            }
        }
   }   

   private static int getLinrSum(String str){
       Scanner cin = new Scanner(str);
        int sum = 0;
        while(cin.hasNextInt()){
            sum += cin.nextInt();
        }
        return sum;
   }
}
/*************************************************************************
    > File Name: try_file.java
    > Author: yijiull
    > Mail: [email protected] 
    > Created Time: 2017年11月11日 星期六 15时09分00秒
 ************************************************************************/
import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;
public class try_file{
    public static void main(String args[]){
        File dir = new File("/media/yijiull/DATA/acm");
        System.out.println("列出目录" + dir + "中的pdf文件");
        Filter filter = new Filter("pdf");
        File fs1[] = dir.listFiles(filter);
        display(fs1);
        System.out.println("列出" + dir + "中的所有子目录");
        DirFilter filter2 = new DirFilter();
        File fs2[] = dir.listFiles(filter2);
        display(fs2);

        //再创建一个子目录
        File f = new File(fs2[0].getPath() + "_newDir");
        System.out.println(f.mkdir());
        File fs3[] = dir.listFiles(filter2);
        display(fs3);
    }
    public static void display(File[] fs){
        for(int i = 0 ; i < fs.length; i++){
            if(fs[i].isDirectory()){
                System.out.println("目录  " + fs[i]);
            }else {
                System.out.println("文件  " + fs[i]);
            }
        }
    }
}

//定义扩展名过滤器
class Filter implements FilenameFilter{
    String extent;
    Filter(String extent){
        this.extent = extent;
    }
    public boolean accept(File die, String name){
        return name.endsWith("." + extent);
    }
}
//定义目录过滤器
class DirFilter implements FileFilter{
    public boolean accept(File path){
        return path.isDirectory();
    }
}

猜你喜欢

转载自blog.csdn.net/yijiull/article/details/78685620
今日推荐