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 ...
随机推荐
- sqlserver字符串拆分(split)方法汇总
--方法0:动态SQL法declare @s varchar(100),@sql varchar(1000)set @s='1,2,3,4,5,6,7,8,9,10'set @sql='select ...
- Html5 Video的使用
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Filemanager 的使用
filemanager的使用包括: 1.创建文件夹 2.删除文件夹 3.写入文件 4.复制文件 5.移动文件 6.删除文件 一.创建文件夹 首先宏的定义一个字符串作为地址的来获取当前的docum ...
- c# json处理(转)
一.C#处理简单json数据 json数据:{"result":"0","res_info":"ok","qu ...
- HTML与CSS绘制简单DIV布局
HTML代码<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF ...
- 【转】CentOS 使用yum命令安装出现错误提示”could not retrieve mirrorlist http://mirrorlist.centos.org ***”
源自:http://www.cnblogs.com/yangbingqi/p/3328610.html 刚安装完CentOS,使用yum命令安装一些常用的软件,使用如下命令:yum grouplist ...
- HDU 1076 An Easy Task
题解:枚举即可…… #include <cstdio> int main(){ int now,y,n,T,count; scanf("%d",&T); whi ...
- 2015 11 27编写JAVA程序
在任意文件下 ,建立一个文本文档,更改其txt格式为java格式, 打开此程序的同时打开eclipse可编写代码. public class 文件名{ public static void main( ...
- EGit with eclipse to clone project from GitHub(Step by step)
转载请注明出处! 1. To find your project URL inside GitHub: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveWF ...
- hdu 4738 Caocao's Bridges(2013杭州网络赛丶神坑)
就是求最小权值的桥..不过有好几个坑... 1:原图不连通,ans=0. 2: m<=n^2 显然有重边,重边必然不是桥,处理重边直接add(u, v, INF). 3: 最小桥边权为0的时 ...