JAVA FILE or I/O学习 - I/O流操作:FileInputStream、FileOutputStream、ObjectInputStream、ObjectOutputStream、InputStreamReader、OutputStreamWriter等
public class IOStreamKnow
{
/*********************************文件读写方式:字节流************************************/
/**
* 方式一:基本方式,文件读写方式的基础
*/
public void name()
{
try
{
//创建输入流,输入流用来读取文件字节信息
//参数表示读取的文件对象
FileInputStream input = new FileInputStream(new File("d:/a"));
//创建输入流,将信息进行输出
//参数表示输出的文件目标
FileOutputStream output = new FileOutputStream(new File("e:/a"));
//读取输入流中的信息
//读取的字节
int data; while((data = input.read()) != -1)
{
//将读取的信息通过输出流完成输出
output.write(data);
}
//清空输出流
output.flush();
//关闭输出流
output.close();
//关闭输入流
input.close(); } catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
/**
* 文件读写缓冲流:推荐方式一
* 该种方式需要依据FileInputStream为基础
*/
public void name1()
{
try
{
//缓冲字节流依赖于字节流来构建
BufferedInputStream buffInput = new BufferedInputStream(new FileInputStream(new File("d:/back.jpg")));
//缓冲输出流
BufferedOutputStream buffOutput = new BufferedOutputStream(new FileOutputStream(new File("e:/back.jpg")));
int data;
while((data = buffInput.read()) != -1)
{
buffOutput.write(data);
}
buffOutput.flush();
buffOutput.close();
buffInput.close();
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
/**
* 文件读写缓冲流:推荐方式二:常用方式
* 该种方式需要依据FileInputStream为基础,并且可以自行定制读写的量
*/
public void name2()
{
try
{
FileInputStream input = new FileInputStream(new File("d:/back.jpg"));
FileOutputStream output = new FileOutputStream(new File("e:/back.jpg"));
//缓冲字节数组
byte[] buffer = new byte[1024];
//将输入流读取的文件信息读入到缓冲区中,返回读取的长度
int len;
while((len = input.read(buffer)) != -1)
{ output.write(buffer,0,len);
}
output.flush();
output.close();
input.close();
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
/**
* 特定文件读取方式:按照指定的数据源类型,进行读取
*/
public void name3()
{
//data可以在读写的时候按照特定的java格式进行数据的读写
try
{
DataInputStream input = new DataInputStream(new FileInputStream(new File("d:/back.jpg")));
DataOutputStream output = new DataOutputStream(new FileOutputStream(new File("e:/back.jpg")));
int data;
while((data = input.read()) != -1)
{
output.write(data);
}
output.flush();
output.close();
input.close();
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
/*********************************对象的存储、读入方式:序列化、反序列化************************************/
/**
* 序列化写入(存储)对象数据
* 反序列化读取对象数据
*/
public void name4()
{
User user = new User(1001, "tom"); //通过序列化保存数据
try
{
ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream(new File("d:/temp.txt")));
output.writeObject(user);
output.flush();
output.close(); } catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
} //通过反序列化对之前保存的数据进行对象的转换
try
{
ObjectInputStream input = new ObjectInputStream(new FileInputStream(new File("d:/temp.txt")));
//读取文件中的对象
User user1 = (User)input.readObject();
input.close();
System.out.println(user1.getUserId());
System.out.println(user1.getUserName());
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
} catch (ClassNotFoundException e)
{
e.printStackTrace();
}
}
/*********************************文件读写方式:字符流,主要用于读取文本文件************************************/
/**
* 方式二:由字节流衍生而来,字符流读写方式的基础
*/
public void name5()
{
//方式一:基本方式,文件读写方式的基础
try
{
//创建字符输入流,字符流的构建依赖于字节流,InputStreamReadder是字节流通向字符流的桥梁
//不能使用字符流读取使用字节描述的文件信息,如图片,音频文件,视频文件
InputStreamReader reader = new InputStreamReader(new FileInputStream(new File("d:/test.txt")));
//创建字符输出流
OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(new File("e:/test.txt")));
int data;
while((data = reader.read()) != -1)
{
writer.write(data);
//System.out.println(data);
}
writer.flush();
writer.close();
reader.close(); } catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
/**
* 字符缓冲流:由字符流读写方式的基础而来
*/
public void name6()
{
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("d:/集合项目.txt")))); String str = "";
//每次读取一行
while((str = reader.readLine()) != null)
{
System.out.println(str);
}
reader.close();
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
try
{
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("d:/my.txt"))));
writer.write("自定义文本");
//换行
writer.newLine();
writer.write("hello niit");
writer.flush();
writer.close();
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
JAVA FILE or I/O学习 - I/O流操作:FileInputStream、FileOutputStream、ObjectInputStream、ObjectOutputStream、InputStreamReader、OutputStreamWriter等的更多相关文章
- Java基础知识二次学习--第八章 流
第八章 流 时间:2017年4月28日11:03:07~2017年4月28日11:41:54 章节:08章_01节 视频长度:21:15 内容:IO初步 心得: 所有的流在java.io包里面 定 ...
- JAVA FILE or I/O学习 - 补充CopyFiles功能
public class CopyFiles { public static void main(String[] args) { CopyFiles copyFiles = new CopyFile ...
- JAVA FILE or I/O学习 - Desktop本地程序学习
public class DesktopKnow { public void know() { try { Desktop.getDesktop().open(new File("C:\\P ...
- JAVA FILE or I/O学习 - File学习
public class FileKnow { public static void main(String[] args) { //构建file对象 ,参数表示文件所在的路径 File file = ...
- java IO操作:FileInputStream,FileOutputStream,FileReader,FileWriter实例
FileInputStream <span style="font-family:Verdana;">import java.io.File; import java. ...
- NodeJS学习笔记 (28)流操作-stream(ok)
模块概览 nodejs的核心模块,基本上都是stream的的实例,比如process.stdout.http.clientRequest. 对于大部分的nodejs开发者来说,平常并不会直接用到str ...
- 2017.12.20 Java中的 IO/XML学习总结 File类详细
IO / XML 一.File类 1.定义/概念 Java是面向对象的语言,要想把数据存到文件中,就必须要有一个对象表示这个文件.File类的作用就是代表一个特定的文件或目录,并提供了若干方法对这些文 ...
- 201521123061 《Java程序设计》第九周学习总结
201521123061 <Java程序设计>第九周学习总结 1. 本周学习总结 2. 书面作业 本次PTA作业题集异常 1.常用异常 题目5-1 1.1 截图你的提交结果(出现学号) 1 ...
- 201521123093 java 第十二周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 2. 书面作业 将Student对象(属性:int id, String name,int age,doubl ...
随机推荐
- c++实现单例
单例宏: //单件定义宏 //------------------------------------- // 在头文件中申明 // DECLARE_SINGLEOBJ( CSampleClass ) ...
- BZOJ 1415: [Noi2005]聪聪和可可( 最短路 + 期望dp )
用最短路暴力搞出s(i, j)表示聪聪在i, 可可在j处时聪聪会走的路线. 然后就可以dp了, dp(i, j) = [ dp(s(s(i,j), j), j) + Σdp(s(s(i,j), j), ...
- hadoop笔记之hdfs
1.HDFS设计基础与目标 1.HDFS设计基础与目标 (1)硬件错误是常态,因此需要冗余. (2)流式数据访问.即数据批量读取而非随机读写,Hadoop擅长做的是数据分析而不是事务处理. (3)大规 ...
- 一年开发ASP.NET MVC4项目经验总结
一年开发ASP.NET MVC4项目所用所学技术经验总结 阅读目录 文章背景 前端所用技术摘要 后端所用技术摘要 1. 文章背景 本人2014年从Java转行到C#从事BS项目的开发,刚开始接触的是A ...
- Mysql 笔记:
1:可以查看information_schema.index_statistics 来查看索引的使用信息.还可以使用pt-index-usage 这个工具来分析日志再结合explain 来分析使用的索 ...
- QTableView的表格项中加入图标的方法(重载View::mouseMoveEvent,并使用View::setIconSize函数设置图标的大小)
当在使用表格视图的时候,需要在表格每一行前面加入图标,应该怎么做呢?Qt中通过使用MVC的处理方式,很容易做到这一点,具体实现如下: 先贴出图,让大家一睹为快 下面我就来介绍一下,上图的灯泡是怎么实现 ...
- HTTP使用BASIC认证的原理及实现方法(还有NTLM方法,比较复杂)
一. BASIC认证概述 在HTTP协议进行通信的过程中,HTTP协议定义了基本认证过程以允许HTTP服务器对WEB浏览器进行用户身份证的方法,当一个客户端向HTTP服务 器进行数据请求时,如果客 ...
- 度小于所述过程:es.exe
在防火墙管理,见未知的过程"es.exe" 程序信息: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdGVzdGNzX2Ru/font ...
- Swift和Objective-C混合编程
假设你现在就是一个iOS程序员,你对Objective-C很熟悉,对iOS开发也很熟悉,然而,苹果公司在iOS 8之后推出了Swift语言.那么,如何才能快速地从Objective-C过渡到Swift ...
- 雪碧图(sprite)
雪碧图 是一种将网页上常用且不经常变动的小图标集中在一张大图中,根据网页需求来显示图片的技术. 可以提高网页加载速度,增加用户体验. 其原理是通过html块状元素建立一个满足需求的视图窗口,然后在窗口 ...