简易NIO流,与普通IO流,存数据,复制文件的Demo

下面经过FileChannel向文件中输入数据的一个例子

/*** * 用于将String存入指定路径下的文件 * @param str 内容 * @param toFile 目标文件 * @param fileName 文件名 */
    public static void saveFileChannel(String str, File toFile,String fileName) {
        FileOutputStream outputStream = null;
        FileChannel channel = null;
        //判断文件夹不存在,而且判断对象是不是一个文件夹
        if(!toFile.exists() && !toFile.isDirectory()){   //判断文件夹是否存在,不存在则建立
            toFile.mkdir();
        }
        try {
        	//outputStream = new FileOutputStream(toFile+"/" + fileName,true);不删除源文件
            outputStream = new FileOutputStream(toFile+"/" + fileName);
            channel = outputStream.getChannel();
            ByteBuffer buffer = ByteBuffer.allocate(1024);
            buffer.put(str.getBytes());
            //相似于flush()函数功能,将buffer里面的数据刷新出去
            buffer.flip();
            //检查是否还有数据未写入
            while (buffer.hasRemaining()) channel.write(buffer);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (channel != null) {
                    channel.close();
                }
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

这个是经过FileChannel进行文件的复制

/** * 用filechannel进行文件复制 * * @param fromFile 源文件 * @param toFile 目标文件 */
    public static void fileCopyWithFileChannel(File fromFile, File toFile) {
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        FileChannel fileChannelInput = null;
        FileChannel fileChannelOutput = null;
        //判断文件夹不存在,而且判断对象是不是一个文件夹
        if(!toFile.exists() && !toFile.isDirectory()){   //判断文件夹是否存在,不存在则建立
            toFile.mkdir();
        }
            try {
                fileInputStream = new FileInputStream(fromFile);
                fileOutputStream = new FileOutputStream(toFile+"/"+fromFile.getName());
                //获得fileInputStream的文件通道
                fileChannelInput = fileInputStream.getChannel();
                //获得fileOutputStream的文件通道
                fileChannelOutput = fileOutputStream.getChannel();
                //将fileChannelInput通道的数据,写入到fileChannelOutput通道
                fileChannelInput.transferTo(0, fileChannelInput.size(), fileChannelOutput);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (fileInputStream != null)
                        fileInputStream.close();
                    if (fileChannelInput != null)
                        fileChannelInput.close();
                    if (fileOutputStream != null)
                        fileOutputStream.close();
                    if (fileChannelOutput != null)
                        fileChannelOutput.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
    }

这里是经过BufferedWriter写入数据的

/** * * @param str 内容 * @param toFile 目标文件夹 如:E:/demo * @param fileName 文件名称 如:demo.txt */
    public synchronized static void saveFile(Object str, File toFile,String fileName){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = format.format(new Date());// 系统时间
        // 判断文件夹不存在,而且判断对象是不是一个文件夹
        if (!toFile.exists() && !toFile.isDirectory()) { // 判断文件夹是否存在,不存在则建立
            toFile.mkdir();
        }
        BufferedWriter out = null;
        try {
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(toFile + "/" + fileName, true),"UTF-8"));
            out.newLine();
            out.write(date + ":" + str);
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

这里是执行的main方法

public static void main(String[] args) throws IOException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String date = format.format(new Date());//系统时间
        //内容
        String string = "hello";
        //目标路径
        File toFile = new File("E:/IdeaProject/T");
        //将内容写入目标文件
        saveFileChannel(string, toFile,date+".txt");
        //------------------------------------------
        //源文件
        File file = new File("E:/IdeaProject/TEXT/DATA.txt");
        //将源文件复制到目标文件
        fileCopyWithFileChannel(file, toFile);
        //BufferedWriter测试的方法
        saveFile(string, toFile,"demo.txt");
    }