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 ...
随机推荐
- 用for循环遍历DataTable中的数据
for (int i = 0; i < dataTable.Rows.Count; i++) { for (int j = 0; j < dataTable.Columns.Count; ...
- Windows Server 中开启 SQL Server 2008 的1433端口
在Windows Server2008 服务器上部署了Microsofit SQL Server2008 R2 ,想让远程机器能够访问,于是开放1433端口,进行了如下设置: 1.打开“本地安全策略” ...
- JSON.parse这个是啥?
var jsontext = '{"firstname":"Jesper","surname":"Aaberg",&qu ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 7篇Model View和4篇双缓冲
http://www.cnblogs.com/SkylineSoft/category/299475.html
- 关于cvScalar的那些事
CvScalar 可存放在1-,2-,3-,4-TUPLE类型的捆绑数据的容器 该函数包含4个浮点成员,可以用来表示B(Blue),G(Green),R(Red),Alpha(表示图像的透明度) ty ...
- 7kb的javascript日期操作类库(XDate)
A Modern JavaScript Date Library XDate is a thin wrapper around JavaScript's native Date object that ...
- os基础
命令:指计算机用户要求计算机系统为其工作的指示: 命令的表示形式: 1.字符形式: 2.菜单形式: 3.图形形式: 命令的使用方式:1. 脱机使用方式 off_line 2.联机使用方式 ...
- SDK无法更新
http://jingyan.baidu.com/article/da1091fbd232fe027949d653.html
- 腾讯云部署Flask应用
由于新浪云现在不免费了.而且云豆也用完了.所以去腾讯云申请了个学生云主机,一元一个月. 不过部署开发环境还是有点麻烦的,搞了好几天,终于部署成功了! 下面说部署过程: 我云主机用的是 Ubuntu 1 ...