java IO 流 读写文件 以及复制
来源:互联网 发布:centos lamp环境搭建 编辑:程序博客网 时间:2024/05/01 12:28
import java.io.BufferedOutputStream;import java.io.BufferedWriter;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;public class IOpractic01 {public static void main(String[] args) {//文件路径File file = new File("D:\\example.txt");//字节流写入文件outPut(file);//字节流读取文件input(file);//字符流写入文件writeOut(file);//字符流读取文件readerIn(file);//文件coppycopyFile("D:\\example.txt","D:\\example1.txt");bufferOutPut(file);writeOutPut(file);}/** * 字节输出流 * @param file */static void outPut(File file){String str="亲爱的 我的头有点晕乎";FileOutputStream out =null;try {out= new FileOutputStream(file);byte[] byt = str.getBytes();out.write(byt);out.close();} catch (IOException e) {e.printStackTrace();}} /** * 字节输出流 * @param file */static void input(File file){FileInputStream in =null; try {in = new FileInputStream(file);byte[] bte = new byte[1024]; int len =in.read(bte);System.out.println("文件中的信息是:"+ new String(bte,0,len));} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} }/** * 字符输入流 * @param file */static void readerIn(File file){FileReader read =null;try { read = new FileReader(file); char[] car = new char[1024]; int len = read.read(car); System.out.println("输出的文件结果为:"+new String(car,0,len));} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}/** * 字符输出流 */static void writeOut(File file){String str ="十年之前,你不认识我,我不认识你";FileWriter write =null;try {write = new FileWriter(file); write.write(str); write.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}/** * 文件复制 * @param dest * @param path */static void copyFile(String path,String dest){File file = new File(dest);FileInputStream in = null;FileOutputStream out = null;try {//如果文件不存在,则创建if(!file.exists()){file.createNewFile();}in = new FileInputStream(path);out = new FileOutputStream(file);byte[] byt = new byte[1024];int len;while((len=in.read(byt))!=-1){for(int i =0;i<len;i++){out.write(byt[i]);}}in.close();out.close();} catch (Exception e) {e.printStackTrace();}}/*____________________________下面是加入缓冲区_______________________________*/static void bufferOutPut(File file){String str="我是一个小例子";FileOutputStream out =null;try {//这里第二个参数true,是为了不覆盖原有文件,在原有文件后继续添加out = new FileOutputStream(file,true);BufferedOutputStream buffer = new BufferedOutputStream(out);byte[] byt =str.getBytes();buffer.write(byt);//清空缓冲区buffer.flush();//缓冲区关闭buffer.close();//输出流关闭out.close();} catch (IOException e) {e.printStackTrace();}}//字符流static void writeOutPut(File file){String str="我只是一个简单的例子1111";FileWriter out = null;try {out = new FileWriter(file,true);BufferedWriter buffer = new BufferedWriter(out);buffer.write(str);buffer.flush();buffer.close();out.close();} catch (Exception e) {e.printStackTrace();} }}
1 0
- java IO 流 读写文件 以及复制
- 【Java基础知识】IO流--字节流读写数据以及复制文件的几种方式
- 【Java基础知识】IO类--字符流读写数据以及复制文件的几种方式
- IO文件读写与复制
- java io流读写文件
- java IO流读写文件
- 《java----IO流---文件复制》
- 【Java】File类文件管理及IO读写、复制操作
- java中最简单的文件复制,读写(IO流操作)
- JAVA IO读写文件
- Java IO 读写文件
- JAVA-IO读写文件
- java IO读写文件
- Java IO 读写文件
- Java IO 读写文件
- Java IO文件读写
- java IO流 读取文件 && 文件复制
- java IO-文件流-文件内容复制
- 黑马程序员 JavaSE-06 面向对象(下)
- 断点续传的原理剖析与实例讲解
- INVALID_USER_SCODE
- debug和release版本dll混用导致的问题
- struts动态方法调用
- java IO 流 读写文件 以及复制
- HDU 5570 balls
- FT Introduction
- COMPSAC峰会
- 【poj2039】To and Fro
- 1017. Queueing at Bank
- 视图控制器中之间切换的几种方式 by 李梦珂
- NYOJ 42--一笔画问题【水题 && 欧拉路】
- RedHat&CentOS 6.4安装谷歌浏览器chrome