File类用于访问文件或目录的属性

流:指一连串流动的字符,是以先进先出的方式发送信息的通道。程序和数据源之间是通过流联系起来的。

第一套:字节流读取写入方案

FileInputStream :字节流方式读取文本文件

        FileInputStream fis=new FileInputStream("E:\\读取文件.txt");
byte[]bytes=new byte[1024];
int data;
while((data=fis.read(bytes))!=-1)
{
String str=new String(bytes,0,data);
System.out.println(str);
}
fis.close();
}

FileOutputStream:字节流写入硬盘

    FileOutputStream fos=new FileOutputStream("E:\\1.txt");
String word="高考是人生的分水岭";
byte[] bytes = word.getBytes();
fos.write(bytes);
fos.close();
System.out.println("写入成功!");
}
}

第二套:字符流读取写入方案

FileReader:字符流读取文本

    FileReader fr=new FileReader("E:\\读取文件.txt");
char[]chars=new char[1024];
int data;
while((data=fr.read(chars))!=-1)
{
String str=new String(chars);
System.out.println(str);
}
}

FileWriter:字符流写入文本

FileWriter fw=new FileWriter("E:\\2.txt");

        fw.write("新的6月");

        System.out.println("写入成功!");

        fw.close();
}

第三套:<BufferedReader、BufferedWriter>一般和FileReader和FileWriter结合使用

BufferedReader:自定义缓存大小,读取文本 8192个char

    FileReader fr=new FileReader("E:\\读取文件.txt");
BufferedReader br=new BufferedReader(fr);
String line;
while((line=br.readLine())!=null)
{
System.out.println(line);
}
br.close();
fr.close();
}

BufferedWriter:写入文本

    FileWriter fw=new FileWriter("E:\\5.txt");
BufferedWriter bw=new BufferedWriter(fw);
bw.write("OK!!"); bw.close();
fw.close(); System.out.println("写入成功!!");
}

第四套:可以读取二进制(img图片等 )

DataInputStream:将本地的img加载到内存中

FileInputStream fis=new FileInputStream("E:\\5.txt");
FileOutputStream fos=new FileOutputStream("D:\\55.txt"); DataInputStream dis=new DataInputStream(fis);
DataOutputStream dos=null; byte[]bytes=new byte[1024]; int data; while((data=dis.read(bytes))!=-1)
{
dos=new DataOutputStream(fos);
dos.write(bytes);
} dos.close();
dis.close();
fos.close();
fis.close(); System.out.println("copy succes!!!");
}

DataOutputStream:将内存中的二进制数据写入到硬盘上的某个文件中

    DataOutputStream out=null;
DataInputStream dis=null;
try {
//创建输入流对象
FileInputStream fis=new FileInputStream("c:\\范宁.jpg");
dis=new DataInputStream(fis);
//创建输出流对象
FileOutputStream outFile=new FileOutputStream("c:\\范宁小美女33.jpg");
out=new DataOutputStream(outFile);
int temp=dis.read();
while (temp!=-1) {
out.write(temp);
temp=dis.read();
}
System.out.println("复制成功");
fis.close();
outFile.close();
} catch (Exception e) {
System.out.println("文件不存在");
}finally{
try {
if (dis!=null) {
dis.close();
}
if (out!=null) {
out.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}

注:在java中,byte数组和String字符串如何转换?

1、string 转 byte[]

String str = "Hello";
byte[] srtbyte = str.getBytes();

2、byte[] 转 string

byte[] srtbyte;
String str = new String(srtbyte);
System.out.println(str);

四种读写方案IO流 (JAVA)的更多相关文章

  1. Java四种引用--《深入理解Java虚拟机》学习笔记及个人理解(四)

    Java四种引用--<深入理解Java虚拟机>学习笔记及个人理解(四) 书上P65. StrongReference(强引用) 类似Object obj = new Object() 这类 ...

  2. java 四种方式实现字符流文件的拷贝对比

    将D:\\应用软件\\vm.exe  拷贝到C:\\vm.exe   四种方法耗费时间对比  4>2>3>1 package Copy; import java.io.Buffere ...

  3. Java成长第四集--文本处理IO流

    Java IO流在实际业务中使用的频率还是蛮高的,一些业务场景比如,文件的上传和导出,文件的读取等基本都是通过操作IO流来实现的,所以IO流是我们现在学习过程中必须要掌握的技能之一,熟练的使用IO流, ...

  4. Java中的四套读写方案

    一.字节流读写方案 FileInputStream:字节流方式读取文本文件 FileoutputStream:字节流写入硬盘 二.字符流读写方案 FileReader:字符流读取文本 FileWrit ...

  5. java-mybaits-012-mybatis-Interceptor-拦截器读写分离四种实现方案

    一.概述 基本项目搭建 技术框架:spring web mvc .日志[slf4j.log4j2].mybatis.druid.jetty插件启动.mybatis-generator逆向配置生产dao ...

  6. 数据读写API——IO流

    理清一些概念 1.Java 中的IO是干啥的? IO指的是Input和Output,主要目的是实现数据在存储介质之间的传输.[流:数据流,类比与水流的流动] 2.IO分类 按照操作单元来划分,可以分为 ...

  7. 总结:视频播放的四种实现方案(Native)

    一.来自 AVFoundation的 AVPlayer对象 特点: 1. AVPlayer     > 优点:          可以自定义UI, 进行控制     > 缺点:      ...

  8. PHP四种序列化方案

    原文地址:https://t.ti-node.com/thread/... 数据的序列化是一个非常有用的功能,然而目测很多人跟我一样,在刚接触这玩意的时候压根就不理解这货色到底是干啥用的,反正老师说了 ...

  9. 基于redis实现的四种常见的限流策略

    引言 在web开发中功能是基石,除了功能以外运维和防护就是重头菜了.因为在网站运行期间可能会因为突然的访问量导致业务异常.也有可能遭受别人恶意攻击 所以我们的接口需要对流量进行限制.俗称的QPS也是对 ...

随机推荐

  1. HDU 5876 Sparse Graph BFS 最短路

    Sparse Graph Problem Description   In graph theory, the complement of a graph G is a graph H on the ...

  2. POJ 1984 Navigation Nightmare 带全并查集

    Navigation Nightmare   Description Farmer John's pastoral neighborhood has N farms (2 <= N <= ...

  3. java中如何实现类似goto的作法

    goto虽然是java中保留的keyword,但是对于跳转这个语法对新手来说这个确实好用.为了提高程序的可靠性和可读性,Java语言目前是不支持无条件跳转的goto语句!! 幸亏java中有高仿跳转的 ...

  4. 通信原理实践(三)——FM调制

    一.FM调制 1.代码如下: clc,clear; fm = ; % 调制信号频率(Hz) Am = 0.5; % 调制信号幅度 fc = 5e3; % 载波频率(Hz) Ac = ; % 载波幅度 ...

  5. Linux内核分析--操作系统是如何工作的

    “平安的祝福 + 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 ” 一.初 ...

  6. kafka producer源码

    producer接口: /** * Licensed to the Apache Software Foundation (ASF) under one or more * contributor l ...

  7. 【转】Struts2解决表单重复提交问题

    用户重复提交表单在某些场合将会造成非常严重的后果.例如,在使用信用卡进行在线支付的时候,如果服务器的响应速度太慢,用户有可能会多次点击提交按钮,而这可能导致那张信用卡上的金额被消费了多次.因此,重复提 ...

  8. c#基础系列(转)

    转:http://www.cnblogs.com/landeanfen/p/4953025.html C#基础系列——一场风花雪月的邂逅:接口和抽象类 前言:最近一个认识的朋友准备转行做编程,看他自己 ...

  9. 记一次小团队Git实践(下)

    在上篇中,我们已经能基本使用git了,接下来继续更深入的挖掘一下git. 更多的配置自定义信息 除了前面讲的用户名和邮箱的配置,还可以自定义其他配置: # 自定义你喜欢的编辑器,可选 git conf ...

  10. express-6 请求和响应对象(1)

    URL的组成部分 协议: 协议确定如何传输请求.我们主要是处理http和https.其他常见的协议还有file和ftp. 主机名: 主机名标识服务器.运行在本地计算机(localhost)和本地网络的 ...