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等的更多相关文章

  1. Java基础知识二次学习--第八章 流

    第八章 流   时间:2017年4月28日11:03:07~2017年4月28日11:41:54 章节:08章_01节 视频长度:21:15 内容:IO初步 心得: 所有的流在java.io包里面 定 ...

  2. JAVA FILE or I/O学习 - 补充CopyFiles功能

    public class CopyFiles { public static void main(String[] args) { CopyFiles copyFiles = new CopyFile ...

  3. JAVA FILE or I/O学习 - Desktop本地程序学习

    public class DesktopKnow { public void know() { try { Desktop.getDesktop().open(new File("C:\\P ...

  4. JAVA FILE or I/O学习 - File学习

    public class FileKnow { public static void main(String[] args) { //构建file对象 ,参数表示文件所在的路径 File file = ...

  5. java IO操作:FileInputStream,FileOutputStream,FileReader,FileWriter实例

    FileInputStream <span style="font-family:Verdana;">import java.io.File; import java. ...

  6. NodeJS学习笔记 (28)流操作-stream(ok)

    模块概览 nodejs的核心模块,基本上都是stream的的实例,比如process.stdout.http.clientRequest. 对于大部分的nodejs开发者来说,平常并不会直接用到str ...

  7. 2017.12.20 Java中的 IO/XML学习总结 File类详细

    IO / XML 一.File类 1.定义/概念 Java是面向对象的语言,要想把数据存到文件中,就必须要有一个对象表示这个文件.File类的作用就是代表一个特定的文件或目录,并提供了若干方法对这些文 ...

  8. 201521123061 《Java程序设计》第九周学习总结

    201521123061 <Java程序设计>第九周学习总结 1. 本周学习总结 2. 书面作业 本次PTA作业题集异常 1.常用异常 题目5-1 1.1 截图你的提交结果(出现学号) 1 ...

  9. 201521123093 java 第十二周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 2. 书面作业 将Student对象(属性:int id, String name,int age,doubl ...

随机推荐

  1. Git基本应用

    1.创建SSH Key $ cd ~/.ssh $ ssh-keygen -t rsa -C "your_email@example.com" 拷贝id_rsa.pub文件到Set ...

  2. 经典SQL语句集锦

      下列语句部分是MsSql语句,不可以在access中使用. SQL分类: DDL—数据定义语言(CREATE,ALTER,DROP,DECLARE) DML—数据操纵语言(SELECT,DELET ...

  3. JavaScript 入門

    <html lang="en"> <head>   <meta charset="UTF-8">   <meta na ...

  4. (iOS)关于GCD死锁的问题

    - (void)viewDidLoad { [super viewDidLoad]; dispatch_sync(dispatch_get_main_queue(), ^{NSLog("); ...

  5. Codeforces Round#1

    A. Theatre Square 题目大意:有一个长宽为m和n的广场,用边长为a的正方形去铺盖,问铺满最少需要多少正方形 题解:题目分解为用长度为a的线条分别去覆盖长度为m和n的线条,计算两者的乘积 ...

  6. wordpress在window下完美实现301重定向的方法

    问题: 首先,简单说一下关于301重定向的问题,最简单的理解就是,假设你的主机上绑定有 www.uilike.cn, uilike.cn, www.uiseo.cn三个域名,当你想输入 uilike. ...

  7. UIKit封装的系统动画

    简介 在UIKit中,对UIView封装了很多类方法来进行简单的动画实现,在动画过程中,通过对属性值的修改来完成一系列的效果. 在IOS4以前,主要通过 + beginAnimation + setA ...

  8. MYSQL++之Connect类型

    原文转自:www.cnblogs.com/aicro mysqlpp:: Connect类型主要负责连接事宜,这是在所有开始mysql操作之前必须进行的(这是句废话). 该类型的主要的结果如下所示 m ...

  9. c#学习心得,慢慢添加,如果有错误希望大家留言,我刚开始学

    1.class类:相当于整个项目的一个功能性程序,为了阐述系统中某个对象的功能. 方法:相当于程序的一个功能部件.可以被其他方法或类调用?感觉这个问题有点复杂 c#框架结构:我目前接触到的 using ...

  10. 刚开始学HTML自己做的,求大神些多多指教。

    !DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> ...