1.用java自带的IO读写方法 官方API网站:http://docs.oracle.com/javase/7/docs/api/

2.Apache的Commons-io-2.4.jar中的方法,参考文档:http://commons.apache.org/proper/commons-io/javadocs/api-2.4/index.html

/*
* 用apache的ommons-io-2.4.jar的ileUtils类中的方法读写数据
* 常用方法请参考帮助文档
* */
public void readWriteCopyFile(){
File file1 = new File("c:/to.txt");
File file2 = new File("c:/to_new.txt");
try {
String input = FileUtils.readFileToString(file1,"gb2312");
System.out.println(input);
FileUtils.copyFile(file1, file2); } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String fileName = "C:/to.txt";
File file = new File(fileName);
String fileContent = ""; fileContent +="Helloworld";
try {
FileUtils.writeStringToFile(file, fileContent, "gb2312");
} catch (IOException e) {
e.printStackTrace();
}
} /*
* 用apache的ommons-io-2.4.jar的ileUtils类中的方法读写数据
* */
public void URLToFileTest(){
try {
URL url = new URL("http://www.163.com"); File file = new File("c:\\163.html"); FileUtils.copyURLToFile(url, file);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} /*
* 用apache的ommons-io-2.4.jar的ileUtils类中的方法读写url内容数据
* */
public void readURL() throws IOException{ InputStream in = new URL( "http://www.blogjava.net/ashutc/archive/2010/07/13/325933.html" ).openStream(); try { System.out.println( IOUtils.toString( in ) );
} finally {
IOUtils.closeQuietly(in);
} } /*
* 用apache的ommons-io-2.4.jar的ileUtils类中的方法读写url内容数据
* */
public void downTolocal(){ try {
InputStream in = new URL("http://img4.cache.netease.com/tech/2015/12/10/201512101531442cb6f_550.png").openStream(); byte [] gif = IOUtils.toByteArray(in);
//IOUtils.write(gif,new FileOutputStream(new File("c:/test.gif"))); //将字符串内容直接写到文件中
FileUtils.writeByteArrayToFile(new File("c:/test.png"),gif) ; //将字节数组内容写到文件中
System.out.println("done");
IOUtils.closeQuietly(in);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } /*
* 使用java自带的带有缓存区字节读写数据---提高读写速度
*
*/
public void RwByByteStream() { File fileIn = new File("D:/java.docx");
File fileOut = new File("D:/java_new.docx");
long before = System.currentTimeMillis();
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(fileIn);
fos = new FileOutputStream(fileOut);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} BufferedInputStream bis = new BufferedInputStream(fis,100000); //设置缓冲区及大小
BufferedOutputStream bos = new BufferedOutputStream(fos,100000); //设置输出流缓冲区及大小
byte [] buffer = new byte[10000];
int l;
int num = 0;
try {
while((l=bis.read(buffer))!=-1){
bos.write(buffer, 0, l);
num++;
}
System.out.println(System.currentTimeMillis()-before+"ms");
System.out.println(num);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
bos.close();
fos.close();
bis.close();
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} } /*
* 使用java字符流读写数据
*
* */
public void RWByCharStream() { try {
//File file = new File("d:/java.txt");
FileInputStream fis = new FileInputStream("d:/java.txt");
FileOutputStream fos = new FileOutputStream("d:/java_new.txt",true);
InputStreamReader isr = new InputStreamReader(fis, "gb2312");
OutputStreamWriter osw = new OutputStreamWriter(fos, "gb2312");
char input[] = new char[100];
int l = 0;
while ((l = isr.read(input)) != -1) {
//String inputString = new String(input,0,l);
osw.write(input,0,l);
}
isr.close();
fis.close();
osw.close();
fos.close();
System.out.println("done"); } catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} /*
* 使用java缓冲区字符流读写数据
*
* */
public void RWByBufferedCharStream() { try { //File file = new File("d:/java.txt");
FileInputStream fis = new FileInputStream("d:/java.txt");
FileOutputStream fos = new FileOutputStream("d:/java_new.txt",true); //加true表示追加写到这个文件中,否则覆盖此文件
InputStreamReader isr = new InputStreamReader(fis, "gb2312");
OutputStreamWriter osw = new OutputStreamWriter(fos, "gb2312"); BufferedReader br = new BufferedReader(isr); //设置缓冲区
BufferedWriter bw = new BufferedWriter(osw);
// PrintWriter pw = new PrintWriter(osw); String input;
while ((input = br.readLine()) != null) {
bw.write(input);
bw.newLine();
System.out.println(input);
// pw.println(input);
}
br.close(); //各流后打开的先关闭,先打开后关闭
bw.flush();
bw.close();
// pw.close();
isr.close();
fis.close();
osw.close();
fos.close();
System.out.println("done"); } catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} } }

IO流数据读写总结的更多相关文章

  1. Java API —— IO流(数据操作流 & 内存操作流 & 打印流 & 标准输入输出流 & 随机访问流 & 合并流 & 序列化流 & Properties & NIO)

    1.操作基本数据类型的流     1) 操作基本数据类型 · DataInputStream:数据输入流允许应用程序以与机器无关方式从底层输入流中读取基本 Java 数据类型.应用程序可以使用数据输出 ...

  2. C#常用IO流与读写文件

    .文件系统 ()文件系统类的介绍 文件操作类大都在System.IO命名空间里.FileSystemInfo类是任何文件系统类的基类:FileInfo与File表示文件系统中的文件:Directory ...

  3. C#常用IO流与读写文件 (转)

    源自https://www.cnblogs.com/liyangLife/p/4797583.html 谢谢 1.文件系统 (1)文件系统类的介绍 文件操作类大都在System.IO命名空间里.Fil ...

  4. 【原创】IO流:读写操作研究(输入流)

    默写代码(以下问题要求能默写,不翻书不百度) 输入 问题一:从文件abc.txt中读取数据到字节数组并打印出来. 分析:如果读取数据,首先第一个问题,数据有多少?如果数据量不确定,如果确定字节数组大小 ...

  5. JavaEE基础(二十二)/IO流

    1.IO流(序列流) 1.什么是序列流 序列流可以把多个字节输入流整合成一个, 从序列流中读取数据时, 将从被整合的第一个流开始读, 读完一个之后继续读第二个, 以此类推. 2.使用方式 整合两个: ...

  6. JavaEE基础(二十)/IO流

    1.IO流(IO流概述及其分类) 1.概念 IO流用来处理设备之间的数据传输 Java对数据的操作是通过流的方式 Java用于操作流的类都在IO包中 流按流向分为两种:输入流,输出流. 流按操作类型分 ...

  7. day20<IO流>

    IO流(IO流概述及其分类) IO流(FileInputStream) IO流(read()方法返回值为什么是int) IO流(FileOutputStream) IO流(FileOutputStre ...

  8. IO流简要总结

    IO流小总结 IO流的本质就是用于数据的传输,根据流的方向的不同,有输入流.输出流.根据数据类型的不同,又有字节流.字符流. 字节流 字节输入流   InputStream 字节输出流   Outpu ...

  9. day22<IO流+>

    IO流(序列流) IO流(序列流整合多个) IO流(内存输出流) IO流(内存输出流之黑马面试题) IO流(对象操作流ObjecOutputStream) IO流(对象操作流ObjectInputSt ...

随机推荐

  1. mac系统及xcode使用的SVN客户端升级

    mac系统及xcode使用的SVN客户端升级 当前的SVN版本已经升级到1.8.x了,但mac系统自带的以及xcode使用的SVN客户端版本没有跟着升级,还是1.6.x的版本.为了解决隐藏目录.svn ...

  2. SSD Buffer Pool Extension

    SSD Buffer Pool Extension 简介 SQL Server 2014中另一个非常好的功能是,可以将SSD虚拟成内存的一部分,来供SQL Server数据页缓冲区使用.通过使用SSD ...

  3. 关于ActiveMQ的一点总结

    ActiveMQ入门 作者:一路向北 摘要:本文主要讲述ActiveMQ的基本知识和使用方法,并简单结合spring使用ActiveMQ. 一.ActiveMQ特性和使用总览 企业消息软件从80年代起 ...

  4. Android系统移植(一)-让android系统在目标平台上运行起来

    编号),文件系统采用ubifs格式,控制台设备为ttyS1,波特率为115200 启动的第一个应用程序是/init (6)确保控制台的设置和硬件保持一致,如:硬件上串口用的是UART1,则内核启动参数 ...

  5. linux添加swap

    一. 起因 发现云服务器重装后没有swap分区 于是我们分出一些物理内存做swap分区.. 二. 经过 (1)创建块文件 sudo dd if=/dev/zero of=/tmp/big_swap b ...

  6. HMM 前向后向算法(转)

    最近研究NLP颇感兴趣,但由于比较懒,所以只好找来网上别人的比较好的博客,备份一下,也方便自己以后方便查找(其实,一般是不会再回过头来看的,嘿嘿 -_-!!) 代码自己重新写了一遍,所以就不把原文代码 ...

  7. MATLAB初体验

    好激动 要入MATLAB的大坑了 恩 很遗憾第一个程序并不是hello world 好 插入代码里并没有MATLAB这个选项 这是一种歧视 x=[:pi/:*pi]; y=sin(x); plot(x ...

  8. NGINX----源码阅读----init配置脚本

    /auto/init init脚本负责初始化各种目录环境变量值. 1.make文件.源文件.头文件.配置头文件路径变量初始化. NGX_MAKEFILE=$NGX_OBJS/Makefile NGX_ ...

  9. retrofit2学习

    http://www.cnblogs.com/wondertwo/p/5838528.html  使用要点

  10. Atom编辑器之加快React开发的插件汇总

    汇总下比较实用的atom插件[偏react开发的]-- 博主发现这个还是比较全面的! atom-react-autocomplete–项目内,组件名及状态的自动补全  autocomplete-js- ...