Java的输入流主要由:InputStream和Reader作为基类,把持久化数据读入内存。输出流由OutputStream和Write类作为父类。

其中读如内存的时候,不可能一下去全读进去,需要一个缓存区,一块一块的读。如果要再把数据复制到其他磁盘,也可以在循环读的时候把数据再写入目标地址。

Reader抽象类:下有InputStreamReader和BufferedReader(读取字符流文件,自带缓存区,有ReaderLine方法可每次读一行。需要和FileReader类一起使用);

InputStreamReader下有FileReader类(读取字符流数据)

InputStream抽象类有子类:FileInputStream类(读取字节流数据);FileInputStream类有子类:DataInputStream(用来读取二进制数据)类。

Writer抽象类:下有OutputStreamWriter和【BufferedWriter子类(需要和FileWriter类仪器连用)】;(和Reader类的子类相对应,写入的时候需要标识偏移量从哪开始读,读到哪里。OutputStream读到-1停止,BufferedWriter读到null停止)。

OutputStreamWriter类:下有FileWriter类。

OutputStream抽象类下有子类:FileOutputStream类(用来写入字节流数据);FileOutputStream类下有子类:DataOutputStream类(用来写入二进制数据);

几个案列加深记忆:

1:File的常用方法

File file=new File("D:\\我的青春谁做主.txt");
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
if(file.isFile()){
System.out.println("名称:"+file.getName());
System.out.println("绝对路径:"+file.getAbsolutePath());
System.out.println("文件大小:"+file.length());
}
if( file.delete()){
System.out.println("删除文件成功!");
}

2:用读取字节流的方法读取文件并把读到数据写入别的磁盘下

FileInputStream fis=null;//
FileOutputStream fos=null;
try {
fis=new FileInputStream("D:\\我的青春谁做主.txt");
fos=new FileOutputStream("E:\\lianxi.txt");
byte[] bytes=new byte[1024];
StringBuffer sb=new StringBuffer();
int size=0;
while((size=fis.read(bytes))!=-1){
String str=new String(bytes);
sb.append(str);
fos.write(bytes, 0, size);
}
System.out.println(sb);
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}finally{
try {
fis.close();
fos.close();
} catch (Exception e2) {
// TODO: handle exception
}
}

3:用读取字符流的方法读取文件并把读到数据写入别的磁盘下

BufferedReader br=null;
BufferedWriter bw=null;
try {

FileReader fr=new FileReader("D:\\pet.template.txt");
FileWriter fw=new FileWriter("E:\\pet.txt");
br=new BufferedReader(fr);
bw=new BufferedWriter(fw);
String line=null;
StringBuffer sb=new StringBuffer();
while((line=br.readLine())!=null){
sb.append(line);
}
System.out.println("替换前:"+sb);
String newStr=sb.toString().replace("{name}", "欧欧");
newStr=newStr.replace("{type}", "狗狗");
newStr=newStr.replace("{master}", "李伟");
fw.write(newStr);
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}finally{
try {
if(br!=null){
br.close();
}
if(bw!=null){
bw.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}

4:使用DataInputStream类和DataOutputStream类读取写入二进制文件(如:图片)

Scanner input=new Scanner(System.in);
System.out.println("请输入要复制的文件绝对路径:");
String filepath=input.nextLine();
System.out.println("请输入复制后的磁盘路径:");
String targetpath=input.nextLine();
DataInputStream dis=null;
DataOutputStream dos=null;
try {
FileInputStream fis=new FileInputStream(filepath);
dis=new DataInputStream(fis);
FileOutputStream fos=new FileOutputStream(targetpath);
dos=new DataOutputStream(fos);
byte[] bytes=new byte[1024];
StringBuffer sb=new StringBuffer();
int size=0;
while((size=dis.read(bytes))!=-1){
String str=new String(bytes);
sb.append(str);
dos.write(bytes);
}
System.out.println("复制成功!");
} catch (Exception e) {
// TODO: handle exception
}finally{
try {
if(dis!=null){
dis.close();
}
if(dos!=null){
dos.close();
}
} catch (Exception e2) {
// TODO: handle exception
}
}

Java里的File I/O的更多相关文章

  1. 模块(相当于Java里的包)

    Python提供丰富和强大的标准库和第三方库. sys库 在命令窗口中可以输入参数 若想将参数打印出来, 可以这样写: print(sys.argv[2]) os库 可以创建文件夹. 类似于Java里 ...

  2. 黑马程序员——JAVA基础之File类,递归,打印流,合并切割流

    ------- android培训.java培训.期待与您交流! ---------- File类 用来将文件或者文件夹封装成对象 方便对文件与文件夹的属性信息进行操作. File对象可以作为参数传递 ...

  3. Java 里的异常(Exception)详解

    作为一位初学者, 本屌也没有能力对异常谈得很深入.   只不过Java里关于Exception的东西实在是很多. 所以这篇文章很长就是了.. 一, 什么是java里的异常   由于java是c\c++ ...

  4. 关于spark入门报错 java.io.FileNotFoundException: File file:/home/dummy/spark_log/file1.txt does not exist

    不想看废话的可以直接拉到最底看总结 废话开始: master: master主机存在文件,却报 执行spark-shell语句:  ./spark-shell  --master spark://ma ...

  5. Sqoop 抽数报错: java.io.FileNotFoundException: File does not exist

    Sqoop 抽数报错: java.io.FileNotFoundException: File does not exist 一.错误详情 2019-10-17 20:04:49,080 INFO [ ...

  6. 小谈Java里的线程

    今天,我们来谈一谈Java里的线程. 一.进程与线程的基本概念 大家可能没听过线程这个概念,但是相信,用计算机的朋友都听过进程这个概念.打开电脑的任务管理器,我们就可以看到许多进程.它们主要分为三类, ...

  7. JMeter学习-027-JMeter参数文件(脚本分发)路径问题:jmeter.threads.JMeterThread: Test failed! java.lang.IllegalArgumentException: File distributed.csv must exist and be readable解决方法

    前些天,在进行分布式参数化测试的时候,出现了如题所示的错误报错信息.此文,针对此做一个简略的重现及分析说明. JMX脚本线程组参数配置如下所示: 参数文件路径配置如下所示: 执行JMX脚本后,服务器对 ...

  8. [Storm] java.io.FileNotFoundException: File '../stormconf.ser' does not exist

    This bug will kill supervisors Affects Version/s: 0.9.2-incubating, 0.9.3, 0.9.4 Fix Version/s: 0.10 ...

  9. java里的static和final

    本节介绍JAVA里static和final的作用和使用方法以及一些需要注意的问题. 一.static static表示"全局"或"静态",用来修饰成员变量和成员 ...

随机推荐

  1. Solr4.3之检索建议suggest

    原文链接:http://www.656463.com/article/Efm26v.htm 很多才学solr的人,都容易把solr spellcheck和solr suggest混淆,误以为他们是一样 ...

  2. MSVC和MinGW组件dll相互调用

    http://www.mingw.org/wiki/msvc_and_mingw_dlls MinGW调用VC: The other way is to produce the .a files fo ...

  3. [LeetCode]题解(python):079 Word Search

    题目来源 https://leetcode.com/problems/word-search/ Given a 2D board and a word, find if the word exists ...

  4. [LeetCode]题解(python):038-Count and Say

    题目来源 https://leetcode.com/problems/count-and-say/ The count-and-say sequence is the sequence of inte ...

  5. Android笔记:Socket客户端收发数据

    client.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" and ...

  6. JQuery 内容过滤选择器

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  7. sql 显示0001

  8. LeetCode Closest Binary Search Tree Value

    原题链接在这里:https://leetcode.com/problems/closest-binary-search-tree-value/ Given a non-empty binary sea ...

  9. php图片处理函数自定义画图和引入图片

    <?php //创建画布,就是画画的位置 imagecreate() //为图像分配颜色 imagecolorallocate() 可以把颜色填充到区域中,不能直接填充画布? //区域填充 bo ...

  10. JQuery 操作HTML元素 示例

    http://www.w3school.com.cn/jquery/jquery_dom_add.asp http://www.w3school.com.cn/jquery/jquery_dom_re ...