码迷,mamicode.com
首页 > 编程语言 > 详细

java读写

时间:2015-03-12 16:42:03      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:

IO流下分为字节流与字符流,每个流又分为输入输出以及读写。

字节流的两个基类为InputStream与OutputStream。

字符流为Reader和Writer

技术分享

一般的缓冲流读写字符:

BufferedReader br = new BufferedReader(new FileReader("b.txt"));

如果有编码格式的话使用:

BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("d:\\test\\xiong.txt"), "gbk"));

 

读取一个文本里面的内容那个,并随机输出一行内容。

public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("d:\\test\\xiong.txt"), "gbk"));
        ArrayList<String> list = new ArrayList<String>();
        String line = null;
        while ((line=br.readLine())!=null){
            list.add(line);
        }
        int random = new Random().nextInt(list.size());
        System.out.println(list.get(random));
    }

 复制文件夹下的文件,文件里有相片,视频所以使用字节流:

package cn;

import java.io.*;

public class FileDemo2 {
    public static void main(String[] args) throws IOException {
        File srcFolder = new File("d:\\demo");
        File destFolder = new File("d:\\test");
        if (!destFolder.exists()){
            destFolder.mkdir();
        }
        File[] files = srcFolder.listFiles();
        for (File file:files){
            File newFile = new File(destFolder, file.getName());
            copy(file, newFile);
        }
    }
    private static void copy(File file, File newFile) throws IOException{
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newFile));
        int len = 0;
        byte[] bys = new byte[1024];
        while ((len=bis.read(bys))!=-1){
            bos.write(bys, 0 , len);
        }
     bis.close()
    bos.close()
} }

 将一个文件夹下的以.java结尾的文本文件复制到另一个文件夹并改为以.jad结尾

package cn;

import java.io.*;
import java.util.ArrayList;
import java.util.Random;

/**
 * Created by Administrator on 2015/3/11.
 */
public class FileDemo {
    public static void main(String[] args) throws IOException {
        File srcFolder = new File("c:\\java");
        File destFolder = new File("c:\\jad");
        if(!(destFolder.exists())){
            destFolder.mkdir();
        }
        File[] fileArray = srcFolder.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                return new File(dir, name).isFile()&&name.endsWith(".java");
            }
        });
        for(File file:fileArray){
            String name = file.getName();
            File newFile = new File(destFolder, name);
            copy(file, newFile);
        }
        File[] destFileArray = destFolder.listFiles();
        for (File destFile:destFileArray){
            System.out.println(destFile);
            String name = destFile.getName();
            String newName = name.replace(".java", ".jad");
            File newFile = new File(destFolder, newName);
            destFile.renameTo(newFile);
        }
    }

    private static void copy(File file, File newFile) throws IOException{
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newFile));
        int len = 0;
        byte[] bys = new byte[1024];
        while ((len=bis.read(bys))!=-1){
            bos.write(bys, 0 , len);
        }
        bis.close();
        bos.close();
    }
}

 复制一个文件夹下的所有内容到指定目录

package cn;

import java.io.*;
import java.util.ArrayList;
import java.util.Random;

/**
 * Created by Administrator on 2015/3/11.
 */
public class FileDemo {
    public static void main(String[] args) throws IOException {
        File srcFile = new File("d:\\demos");
        File destFile = new File("c:\\");
        copyFolder(srcFile, destFile);
    }

    private static void copyFolder(File srcFile, File destFile) throws IOException{
        if(srcFile.isDirectory()){
            File newFolder = new File(destFile, srcFile.getName());
            newFolder.mkdir();
            File[] fileArray = srcFile.listFiles();
            for (File file:fileArray){
                copyFolder(file, newFolder);
            }
        }else{
            File newFile = new File(destFile, srcFile.getName());
            copyFile(srcFile, newFile);
        }
    }

    private static void copyFile(File srcFile, File newFile) throws IOException{
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newFile));
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
        int len = 0;
        byte[] bys = new byte[1024];
        while ((len=bis.read(bys))!=-1){
            bos.write(bys, 0, len);
        }
        bis.close();
        bos.close();
    }
}

 

java读写

标签:

原文地址:http://www.cnblogs.com/tuifeideyouran/p/4332528.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!