Java中的四套读写方案
一.字节流读写方案
FileInputStream:字节流方式读取文本文件
FileoutputStream:字节流写入硬盘
二.字符流读写方案
FileReader:字符流读取文本
FileWriter:字符流写入文本
三.
BufferedReader:自定义缓存大小,读取文本8192个char
BufferedWriter:写入文本
一般和FileReader和FileWriter连用
四.可以读取二进制(img图片等)
DataInputStream:将本地的img加载到内存中
DataOutputStream::将内存中的二进制数据写入到硬盘上的某个文件中。
1.使用字节流读取文本文件
//字节输入流练习:从文本文件读取各种数据(字母,字符串都支持)
//声明流对象
try {
FileInputStream fis=new FileInputStream("c:\\ming.txt");
int data;
System.out.println("可读取的字节数:"+fis.available());
System.out.println("文件内容为:");
//循环读取数据
byte[] buffer=new byte[1024];
StringBuilder sb=new StringBuilder();
while ((data=fis.read(buffer))!=-1) {
//byte[]转为字符串
String str=new String(buffer,0,data,"gb2312");
sb.append(str);
}
System.out.println(sb.toString());
} catch (Exception e) {
}
3.使用字节流写文本文件
// 通过字节流将内存中的数据写入到硬盘上
FileOutputStream fos=null;
try {
String str="呵呵";
byte[] words=str.getBytes("gb2312");
//创建流对象,一追加方式写入文件
fos=new FileOutputStream("c:\\ming.txt",true);
//写入文件
fos.write(words,0,words.length);
System.out.println("文件已更新");
} catch (Exception e) {
System.out.println("创建文件时出错!");
}finally{
try {
if (fos!=null) {
fos.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
4.使用字符流读取文本文件
//使用字符流读取文本文件
Reader fr=null;
try {
fr=new FileReader("c:\\ming.txt");
char[] ch=new char[1024];//中转站,缓冲区
StringBuffer sbf=new StringBuffer();
int length=fr.read(ch);//将字符读入数组
//循环读取并追加字符
while (length!=-1) {
sbf.append(ch,0,length);
length=fr.read(ch);
}
System.out.print(sbf);
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if (fr!=null) {
fr.close();
}
} catch (Exception e2) {
// TODO: handle exception
}
}
5.使用字符流写文本文件
//使用字符流写文本文件
FileWriter fw=null;
try {
fw=new FileWriter("c:\\ming.txt",true);
//写入信息
String words="叶丽仪-上海滩";
fw.write(words);
fw.flush();
System.out.println("写入成功");
} catch (Exception e) {
System.out.println("文件不存在");
}finally{
try {
if (fw!=null) {
fw.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
// 字符输入流BufferedReader读取文件
FileReader fr=null;
BufferedReader br=null;
try {
//创建一个FileReader对象
fr=new FileReader("c:\\ming.txt");
//创建一个BufferedReader对象
br=new BufferedReader(fr);
//读取一行数据
String line=br.readLine();
while (line!=null) {
System.out.println(line);
line=br.readLine();
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if (br!=null) {
br.close();
}if (fr!=null) {
fr.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
使用MyBufferedWriter来写入文本文件
FileWriter fw=null;
BufferedWriter bw=null;
try {
fw=new FileWriter("c:\\ming.txt",true);
bw=new BufferedWriter(fw);
//写入信息
bw.write("故乡的原风景");
bw.newLine();
bw.write("城里的月光-许美静");
bw.flush();
fw.close();
//读取文件内容
FileReader fr=new FileReader("c:\\ming.txt");
BufferedReader br=new BufferedReader(fr);
String line=br.readLine();
while (line!=null) {
System.out.println(line);
line=br.readLine();
}
} catch (Exception e) {
System.out.println("文件不存在");
e.printStackTrace();
}finally{
try {
if (bw!=null) {
bw.close();
}
if (fw!=null) {
fw.close();
}
} catch (Exception e2) {
// TODO: handle exception
}
}
使用字节流类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中的四套读写方案的更多相关文章
- JAVA基础学习之throws和throw的区别、Java中的四种权限、多线程的使用等(2)
1.throws和throw的区别 throws使用在函数外,是编译时的异常,throw使用在函数内,是运行时的异常 使用方法 public int method(int[] arr) throws ...
- JAVA中的四种引用以及ReferenceQueue和WeakHashMap的使用示例
简介: 本文主要介绍JAVA中的四种引用: StrongReference(强引用).SoftReferenc(软引用).WeakReferenc(弱引用).PhantomReference(虚引用) ...
- Java中的四种引用
引用定义 实际上,Java中存在四种引用,它们由强到弱依次是:强引用.软引用.弱引用.虚引用.下面我们简单介绍下这四种引用: 强引用(Strong Reference):通常我们通过new来创建一个新 ...
- JAVA中的四种JSON解析方式详解
JAVA中的四种JSON解析方式详解 我们在日常开发中少不了和JSON数据打交道,那么我们来看看JAVA中常用的JSON解析方式. 1.JSON官方 脱离框架使用 2.GSON 3.FastJSON ...
- 四种读写方案IO流 (JAVA)
File类用于访问文件或目录的属性 流:指一连串流动的字符,是以先进先出的方式发送信息的通道.程序和数据源之间是通过流联系起来的. 第一套:字节流读取写入方案 FileInputStream :字节流 ...
- Java中“附近的人”实现方案讨论及代码实现
前言 在我们平时使用的许多app中有附近的人这一功能,像微信.qq附近的人,哈罗.街兔附近的车辆.这些功能就在我们日常生活中出现. 像类似于附近的人这一类业务,在Java中是如何实现的呢? 本文就简单 ...
- Java中的四种引用类型比较
1.引用的概念 引用这个概念是与JAVA虚拟机的垃圾回收有关的,不同的引用类型对应不同的垃圾回收策略或时机. 垃圾收集可能是大家感到难于理解的较难的概念之一,因为它并不能总是毫无遗漏地解决Java运行 ...
- Java 中的四种引用及垃圾回收策略
Java 中有四种引用:强引用.软引用.弱引用.虚引用: 其主要区别在于垃圾回收时是否进行回收: 1.强引用 使用最普遍的引用.如果一个对象具有强引用,那就 类似于必不可少的生活用品,垃圾回收器绝不会 ...
- K:java中properties文件的读写
Properties类与.properties文件: Properties类继承自Hashtable类并且实现了Map接口,也是使用一种键值对的形式来保存属性集的类,不过Properties有特殊 ...
随机推荐
- Location对象、History对象
Location对象: Window对象的location属性引用的是Location对象,它表示窗口中当前显示的内容的URL,Document对象的location属性也引用Location对象,w ...
- R语言学习
1.清屏 Ctrl + L 2.退出 q() 3.设置工作空间 getwd() setwd('D:\\Program Files\\RStudio\\workspace') 4.显档当前工作目录下的文 ...
- Android studio .9图片造成的错误总结
前言,今天早晨遇到一个很奇葩的问题,导致我花费了很长的时间来解决.从eclipse 项目里复制过来4张.9图片,粘贴到android studio 项目里, 一运行发现报错,开始是 找不到这几张.9图 ...
- Android pull解析xml文件
本文介绍android中使用pull来解析xml文件 先自己写一个xml文件,存一些天气信息 <?xml version="1.0" encoding="UTF-8 ...
- 你真的了解UIButton、UILabel 吗?
一:首先查看一下关于UIButton的定义 @class UIImage, UIFont, UIColor, UIImageView, UILabel; //设置UIButton的样式 typedef ...
- 移动Web开发(一)
1.浅谈Web标准 降低开发复杂度,覆盖的技术层面十分广泛,技术标准化. 以HTML为核心,扩展出几个大类的技术标准: a.程序访问: ECMAScript(ES) 3 . ES 5 . ES ham ...
- 【代码笔记】iOS-清除图片缓存UIActionSheet
一,效果图. 二,代码. RootViewController.m //点击任何处出现sheet -(void)touchesBegan:(NSSet *)touches withEvent:(UIE ...
- WWDC 2013 Session笔记 - UIKit Dynamics入门
本文涉及到的WWDC2013 Session有 1.Session 206 Getting Started with UIKit Dynamics 2.Session 221 Advanced Tec ...
- php文件下载
public function down() { $lang = strtolower(cookie('think_language')); if ($lang == 'en-us') { $file ...
- composer快速入门
composer.json 文件内容定义 ====================================================={ "require":{ &q ...