Java把这些不同来源和目标的数据都统一抽象为数据流;Java语言的输入输出功能是十分强大而灵活的;在Java类库中,IO部分的内容是很庞大的,因为它涉及的领域很广泛:标准输入输出,文件的操作,网络上的数据流,字符串流,对象流,zip文件流。

  这里介绍几种读写文件的方式:

一、InputStream、OutputStream(字节流)

//读取文件(字节流)
InputStream in = new FileInputStream("d:\\1.txt");
//写入相应的文件
OutputStream out = new FileOutputStream("d:\\2.txt");
//读取数据
//一次性取多少字节
byte[] bytes = new byte[2048];
//接受读取的内容(n就代表的相关数据,只不过是数字的形式)
int n = -1;
//循环取出数据
while ((n = in.read(bytes,0,bytes.length)) != -1) {
//转换成字符串
String str = new String(bytes,0,n,"GBK"); #这里可以实现字节到字符串的转换,比较实用
System.out.println(str);
//写入相关文件
out.write(bytes, 0, n);
}
//关闭流
in.close();
out.close();

二、BufferedInputStream、BufferedOutputStream(缓存字节流)使用方式和字节流差不多,但是效率更高(推荐使用)

//读取文件(缓存字节流)
BufferedInputStream in = new BufferedInputStream(new FileInputStream("d:\\1.txt"));
//写入相应的文件
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("d:\\2.txt"));
//读取数据
//一次性取多少字节
byte[] bytes = new byte[2048];
//接受读取的内容(n就代表的相关数据,只不过是数字的形式)
int n = -1;
//循环取出数据
while ((n = in.read(bytes,0,bytes.length)) != -1) {
//转换成字符串
String str = new String(bytes,0,n,"GBK");
System.out.println(str);
//写入相关文件
out.write(bytes, 0, n);
}
//清楚缓存
out.flush();
//关闭流
in.close();
out.close();

三、InputStreamReader、OutputStreamWriter(字节流,这种方式不建议使用,不能直接字节长度读写)。使用范围用做字符转换

//读取文件(字节流)
InputStreamReader in = new InputStreamReader(new FileInputStream("d:\\1.txt"),"GBK");
//写入相应的文件
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("d:\\2.txt"));
//读取数据
//循环取出数据
byte[] bytes = new byte[1024];
int len = -1;
while ((len = in.read()) != -1) {
System.out.println(len);
//写入相关文件
out.write(len);
}
//清楚缓存
out.flush();
//关闭流
in.close();
out.close();

四、FileReader、FileWriter(提供少量的文本读取和写入文本)

try{
//使用FileWriter类写文本文件
FileWriter writer=new FileWriter(fileName);
writer.write("Hello Kuka:\n");
writer.write(" My name is coolszy!\n");
writer.write(" I like you and miss you。");
writer.close();
} catch (IOException e){
e.printStackTrace();
} try{
//使用FileWriter类往文本文件中追加信息
FileWriter writer = new FileWriter(fileName, true);
SimpleDateFormat format = new SimpleDateFormat();
String time = format.format(new Date());
writer.write("\n\t" + time);
writer.close();
} catch(IOException e){
e.printStackTrace();
} int c=0;
try{
//使用FileReader类读文本文件
FileReader reader=new FileReader(fileName);
c=reader.read();
while(c!=-1) {
System.out.print((char)c);
c=reader.read();
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}

五、BufferedReader、BufferedWriter(缓存流,提供readLine方法读取一行文本)

//读取文件(字符流)
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream("d:\\1.txt"),"GBK"));#这里主要是涉及中文
//BufferedReader in = new BufferedReader(new FileReader("d:\\1.txt")));
//写入相应的文件
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("d:\\2.txt"),"GBK"));
//BufferedWriter out = new BufferedWriter(new FileWriter("d:\\2.txt"));
//读取数据
//循环取出数据
String str = null;
while ((str = in.readLine()) != null) {
System.out.println(str);
//写入相关文件
out.write(str);
out.newLine();  //注意\n不一定在各种计算机上都能产生换行的效果
} //清楚缓存 out.flush(); //关闭流 in.close(); out.close();

六、Reader、PrintWriter(PrintWriter这个很好用,在写数据的同事可以格式化)

//读取文件(字节流)
Reader in = new InputStreamReader(new FileInputStream("d:\\1.txt"),"GBK");
//写入相应的文件
PrintWriter out = new PrintWriter(new FileWriter("d:\\2.txt"));
//读取数据
//循环取出数据
byte[] bytes = new byte[1024];
int len = -1;
while ((len = in.read()) != -1) {
System.out.println(len);
//写入相关文件
out.write(len);
}
//清楚缓存
out.flush();
//关闭流
in.close();
out.close();

Java IO如何读写文件的更多相关文章

  1. 161228、Java IO流读写文件的几个注意点

    平时写IO相关代码机会挺少的,但却都知道使用BufferedXXXX来读写效率高,没想到里面还有这么多陷阱,这两天突然被其中一个陷阱折腾一下:读一个文件,然后写到另外一个文件,前后两个文件居然不一样? ...

  2. 161108、Java IO流读写文件的几个注意点

    平时写IO相关代码机会挺少的,但却都知道使用BufferedXXXX来读写效率高,没想到里面还有这么多陷阱,这两天突然被其中一个陷阱折腾一下:读一个文件,然后写到另外一个文件,前后两个文件居然不一样? ...

  3. Java IO流读写文件的几个注意点

     平时写IO相关代码机会挺少的,但却都知道使用BufferedXXXX来读写效率高,没想到里面还有这么多陷阱,这两天突然被其中一个陷阱折腾一下:读一个文件,然后写到另外一个文件,前后两个文件居然不 ...

  4. Java:IO流与文件基础

    Java:IO流与文件基础 说明: 本章内容将会持续更新,大家可以关注一下并给我提供建议,谢谢啦. 走进流 什么是流 流:从源到目的地的字节的有序序列. 在Java中,可以从其中读取一个字节序列的对象 ...

  5. java io流 对文件夹的操作

    java io流 对文件夹的操作 检查文件夹是否存在 显示文件夹下面的文件 ....更多方法参考 http://www.cnblogs.com/phpyangbo/p/5965781.html ,与文 ...

  6. java io流 创建文件、写入数据、设置输出位置

    java io流 创建文件 写入数据 改变system.out.print的输出位置 //创建文件 //写入数据 //改变system.out.print的输出位置 import java.io.*; ...

  7. Java IO学习--(二)文件

    在Java应用程序中,文件是一种常用的数据源或者存储数据的媒介.所以这一小节将会对Java中文件的使用做一个简短的概述.这篇文章不会对每一个技术细节都做出解释,而是会针对文件存取的方法提供给你一些必要 ...

  8. java io 读取写文件

    java 读取txt文件,汉字乱码,原因是因为文件的编码格式和程序编码采用了不同的编码格式.通常,假如自己不修改的话,windows自身采用的编码格式是gbk(而gbk和gb2312基本上是一样的编码 ...

  9. java几种读写文件的方式

    java.io的几种读写文件的方式 一.java把这些不同来源和目标的数据都统一抽象为数据流. Java语言的输入输出功能是十分强大而灵活的. 在Java类库中,IO部分的内容是很庞大的,因为它涉及的 ...

随机推荐

  1. linux 一个读写锁的异常导致的故障

    环境信息: WARNING: kernel version inconsistency between vmlinux and dumpfile KERNEL: vmlinux-47.90 DUMPF ...

  2. Haskell语言学习笔记(73)Existentials

    Existentials(存在类型) Existentially quantified types(Existentially types,Existentials)是一种将一组类型归为一个类型的方式 ...

  3. LeetCode OJ 56. Merge Intervals

    题目 Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6], ...

  4. LeetCode OJ 215. Kth Largest Element in an Array

    Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...

  5. 那些年,我们追过的PHP自加自减运算(1)

    ------------------------------------------------------------------------------------------- PHP的运算符号 ...

  6. 点击li往数组添加对应li的id再点击移除,根据是否有class判断

    if($(this).hasClass('click')){ $(this).removeClass('click'); var idAPP = $(this).attr('id'), index = ...

  7. Jupyter notebook 文件路径

    Jupyter notebook 文件路径 1. 默认工作路径:C:\Users\think 2. 修改工作路径: C:\Users\think\.jupyter路径下,无配置文件 打开命令提示符:( ...

  8. quartz 实例

    第一步:添加jar包 第二步:在spring配置文件中添加 <context:annotation-config/> 第三步:编写定时代码 我们通常做Java后台接口,是让前端访问的,让前 ...

  9. Promise 学习

    参考 https://www.jianshu.com/p/43f948051d65 // Promise里面传入一个函数类型的参数,这个函数类型的参数接收两个参数resolve reject var ...

  10. web访问命令行

    https://github.com/yudai/gotty go get github.com/yudai/gotty gotty -p 8000 -w kubectl exec -it mysql ...