黑马程序员--IO流(21天)
2014-02-05 20:40
295 查看
---------------------- ASP.Net+Android+IOS开发、.Net培训、期待与您交流! ----------------------
1.打印流:PrintWriter与PrintStream(可以直接操作输入流和文件)
2.序列流:SequenceInputStream(对多个流进行合并)
3.操作对象:ObjectInputStream与ObjectOutputStream(被操作的对象需要实现Serializable(标记接口))
4.没有方法的接口通常称为标记接口
5.静态的对象是不能被序列化的
6.public static final long serialVersionUID = 42l; //自行固定序列化;
7.RandomAccessFile (随机访问文件,自身具备读写的方法,同过skipBytes(int x) seek(int x)来达到随机访问)
8.管道流
PipedInputStream和PipedOutputStream(输入输出可以直接进行链接,通过饥饿和线程使用)
9.connect 是将两个管道进行连接。
10.Thread:线程
11.代码示例:
package earth;
import java.io.*;
public class Io_thread01 {
public static void main(String[] args) throws IOException{
//注册输入输出管道;
PipedInputStream pi = new PipedInputStream();
PipedOutputStream po = new PipedOutputStream();
pi.connect(po);
Read r = new Read(pi);
Write w = new Write(po);
//启动线程;
new Thread(r).start();
new Thread(w).start();
}
}
//数据读取
class Read implements Runnable{
//创建一个管道读取的变量;
private PipedInputStream pi;
Read(PipedInputStream pi){
this.pi = pi;
}
//运行的方法;
public void run(){
try{
byte[] arr = new byte[1024];
//创建字节数组;
int len = pi.read(arr);
//存储数组的长度;
String st = new String(arr, 0, len);
System.out.println(st);
}catch(IOException e){
throw new RuntimeException("管道数据读取失败");
}
}
}
//进行数据的写入
class Write implements Runnable{
private PipedOutputStream po;
Write(PipedOutputStream po){
this.po = po;
}
public void run(){
try{
po.write("first test".getBytes());
po.close();
}catch(IOException e){
throw new RuntimeException("管道数据输出流失败");
}
}
}
12.RandomAccessFile介绍:
1.该类不算是IO体系中子类,而是直接继承自object。
2.但它是IO包中成员,因为它具备读和写的功能。
3.内部还封装了一个数组,通过指针对数组的元素进行操作。可以通过getFilePointer
获取指针的位置,同时可以通过seek改变指针的位置。
4.完成读写的原理就是内部封装了字节输入流和输出流。
5.该类只能操作文件。
13.操作数据必然是流数据。
14.Runnable 接口使用方法:
1.由那些打算通过某一线程执行其实例的类来显示,其中类必须定义一个
称为run的无参方法。
2。Runnable为非thread提供了一种激活方式,通过示例化某个Thread实例
并将自身作为运行目标,就可以实现Runnable的类而无需创建Thread
的子类。
15.Integer类提供进制的转换方法。
16.代码示例:
package earth;
import java.io.*;
public class Io_RandomAccessFile {
public static void main(String[] args){
WriteFile();
}
//使用RandomAccessFile方法进行文件数据的读写
public static void WriteFile(){
try {
RandomAccessFile raf = new RandomAccessFile("IO_RandomAccessFile","rw");//"rw"是进行使用的权限;
raf.write("ok,this is RandomAccessFile".getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
}
17.而且该对象的构造函数要操作的文件不存在,会自动创建,如果存在则不会覆盖
18.如果模式为只读r,不会创建问件,会读取一个已存在文件,如果文件不存在,则会出现异常,
19.如果模式为rw,
20.操作基本数据类型:DataInputStream与DataOutputStream.(用于多线程是不安全的)
21.使用DataInputStream 必须按照数据类型的存储数据进行读取(写入的数据可以是UTF, UTF-8, gbk)
22.代码示例:
package earth;
import java.io.*;
public class Lang_Data01 {
public static void main(String[] args) throws IOException{
//此处要注意一定要按照先创建后读取的顺序进行注册;
DataOutput();
DataInput();
//这是进行gbk类型的操作
DataOutput01();
DataInput01();
}
//将数据流写入
public static void DataOutput() throws IOException{
//使用DataOutputStream 进行数据的写入
DataOutputStream dos = new DataOutputStream(new FileOutputStream("lang01.txt"));
dos.writeUTF("你好");
dos.close();
}
//读取数据流
public static void DataInput() throws IOException{
//使用 DataInputStream来读取数据
DataInputStream dis = new DataInputStream(new FileInputStream("lang01.txt"));
//获取数据
String s = dis.readUTF();
System.out.println(s);
dis.close();
}
//另外一种gbk的代码的 书写程序
public static void DataOutput01() throws IOException{
OutputStreamWriter dos1 = new OutputStreamWriter(new FileOutputStream("lang02.txt"),"UTF-8");
dos1.write("你好我是二号 ");
dos1.close();
}
public static void DataInput01() throws IOException{
DataInputStream dis01 = new DataInputStream(new FileInputStream("lang02.txt"));
String s01 = dis01.readUTF();
System.out.println(s01);
dis01.close();
}
}
23.操作字节数组:ByteArrayInputStream与ByteArrayOutputStream
24.ByteArrayInputStream:在构造的时候,需要接受数据源,而且数据源是一个字节数组。
25.ButeArrayOutputStream:在构造的时候不用定义数据目的,因为该对象中已经内部封装了可变长度的
字节数组,这就数据的目的。
26.因为这两个流对象都操作的数组,并没有使用系统资源,所以,不用进行close关闭。
27.源设备:
键盘 System.in , 硬盘 FileStream , 内存 ArrayStream
28.目的设备:
控制台 System.out , 硬盘 FileStream ,内存 ArrayStream
29.操作字符数组:CharArrayReader与CharArrayWrite
30.操作字符串:StringReader与StringWriter
31.字符编码示例代码:
package earth;
import java.io.*;
public class Io_UTF01 {
public static void main(String[] args) throws IOException{
WriteText();
ReadText();
}
//进行数据的输入
public static void WriteText() throws IOException{
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("osw.txt"),"gbk");
osw.write("你好");
osw.close();
}
//进行数据的输出
public static void ReadText() throws IOException{
InputStreamReader isr = new InputStreamReader(new FileInputStream("osw.txt"),"gbk");
//这是将字符流转化成字节流的专业的类
//创建数组
char[] ch = new char[10];
int len = isr.read(ch);
//将数据转化成字符串
String s = new String(ch, 0, len);
System.out.println(s);
//将数据输出;
isr.close();
//关闭数据
}
}
32.编码:字符串变成字节数组(String-->byte[]; str.getBytes())
32.解码:字节数组变成字符串(byte[]-->String; new String(byte[]))
33.代码示例:
package earth;
import java.io.*;
import java.util.Arrays;
public class Io_UTF02 {
public static void main(String[] args) throws IOException{
String s = "你好";
//建立一个字符数据
//进行输出测试,获取UFT的关于“你好”的编码
byte[] b = s.getBytes("UTF-8");
//里面要写入编码格式
System.out.println(Arrays.toString(b));
//“Arrays.toString”返回指定数组内容的字符串的表示形式
//另外创立一个也是以UTF-8进行编码的字符,并进行展示
String s1 = new String(b,"UTF-8");
//里面写入解码格式
System.out.println(s1);
}
}
34.Comparable<T>是比较此对象与指定对象的顺序
35.示例代码:
package earth;
import java.io.*;
import java.util.*;
public class Io_UTF03 {
public static void main(String[] args) throws IOException{
Set<Student> stu = StudentTool.getStudents();
StudentTool.StudnetOut(stu);
}
}
//创建一个学生类,同时调用自然比较方法的接口
class Student implements Comparable<Student>{
//创建需要用到的变量
private String name;
private int math,chinese,english;
private int sum; //总成绩
//进行示例化操作
public Student(String name, int math, int chinese, int english){
this.name = name;
this.math = math;
this.chinese = chinese;
this.english = english;
sum = math + chinese + english;
}
//覆盖方法,compareTo比较此对象与指定对象的顺序
public int compareTo(Student st){
int num = new Integer(this.sum).compareTo(new Integer(st.sum));
if(num == 0){
return this.name.compareTo(st.name);
}
return num;
}
//获取数值
public String getName(){
return name;
}
public int getSum(){
return sum;
}
//对于不确定的数值要去取值到哈希机中去
public int hashCode(){
return name.hashCode() + sum * 78;
}
//对数据中信息进行比对
public boolean equals(Object obj){
//如果身份不是学生测抛出异常
if(!(obj instanceof Student)){
throw new ClassCastException("类型不匹配");
}
Student st = (Student)obj;
//返回数据
return this.name.equals(st.name) && this.sum == st.sum;
}
//使用toString的方法来界定显示的格式
public String toString(){
return "student["+ name + "," + math +","+ chinese +"," + "english" + "]";
}
}
//建立一个学生类中要使用的工具
class StudentTool{
//创建学生集合
public static Set<Student> getStudents() throws IOException{
//读取键盘数值
BufferedReader bd = new BufferedReader(new InputStreamReader(System.in));
String line = null;
Set<Student> stu = new TreeSet<Student>();
while((line = bd.readLine()) != null){
//设置一个结束命令
if("over".equals(line)){
break;
}
String[] info = line.split(",");
Student st = new Student(info[0], Integer.parseInt(info[1]), Integer.parseInt(info[2]), Integer.parseInt(info[3]));
stu.add(st);
}
bd.close();
//关闭流;
return stu;
//返回数值;
}
//输出数据
public static void StudnetOut(Set<Student> stu)
throws IOException{
BufferedWriter bw = new BufferedWriter(new FileWriter("studentinformation.txt"));
//一次进行遍历
for(Student st : stu){
bw.write(st.toString() + "");
bw.write(st.getSum() + "");
bw.newLine();
bw.flush();
}
}
}
---------------------- ASP.Net+Android+IOS开发、.Net培训、期待与您交流! ----------------------
1.打印流:PrintWriter与PrintStream(可以直接操作输入流和文件)
2.序列流:SequenceInputStream(对多个流进行合并)
3.操作对象:ObjectInputStream与ObjectOutputStream(被操作的对象需要实现Serializable(标记接口))
4.没有方法的接口通常称为标记接口
5.静态的对象是不能被序列化的
6.public static final long serialVersionUID = 42l; //自行固定序列化;
7.RandomAccessFile (随机访问文件,自身具备读写的方法,同过skipBytes(int x) seek(int x)来达到随机访问)
8.管道流
PipedInputStream和PipedOutputStream(输入输出可以直接进行链接,通过饥饿和线程使用)
9.connect 是将两个管道进行连接。
10.Thread:线程
11.代码示例:
package earth;
import java.io.*;
public class Io_thread01 {
public static void main(String[] args) throws IOException{
//注册输入输出管道;
PipedInputStream pi = new PipedInputStream();
PipedOutputStream po = new PipedOutputStream();
pi.connect(po);
Read r = new Read(pi);
Write w = new Write(po);
//启动线程;
new Thread(r).start();
new Thread(w).start();
}
}
//数据读取
class Read implements Runnable{
//创建一个管道读取的变量;
private PipedInputStream pi;
Read(PipedInputStream pi){
this.pi = pi;
}
//运行的方法;
public void run(){
try{
byte[] arr = new byte[1024];
//创建字节数组;
int len = pi.read(arr);
//存储数组的长度;
String st = new String(arr, 0, len);
System.out.println(st);
}catch(IOException e){
throw new RuntimeException("管道数据读取失败");
}
}
}
//进行数据的写入
class Write implements Runnable{
private PipedOutputStream po;
Write(PipedOutputStream po){
this.po = po;
}
public void run(){
try{
po.write("first test".getBytes());
po.close();
}catch(IOException e){
throw new RuntimeException("管道数据输出流失败");
}
}
}
12.RandomAccessFile介绍:
1.该类不算是IO体系中子类,而是直接继承自object。
2.但它是IO包中成员,因为它具备读和写的功能。
3.内部还封装了一个数组,通过指针对数组的元素进行操作。可以通过getFilePointer
获取指针的位置,同时可以通过seek改变指针的位置。
4.完成读写的原理就是内部封装了字节输入流和输出流。
5.该类只能操作文件。
13.操作数据必然是流数据。
14.Runnable 接口使用方法:
1.由那些打算通过某一线程执行其实例的类来显示,其中类必须定义一个
称为run的无参方法。
2。Runnable为非thread提供了一种激活方式,通过示例化某个Thread实例
并将自身作为运行目标,就可以实现Runnable的类而无需创建Thread
的子类。
15.Integer类提供进制的转换方法。
16.代码示例:
package earth;
import java.io.*;
public class Io_RandomAccessFile {
public static void main(String[] args){
WriteFile();
}
//使用RandomAccessFile方法进行文件数据的读写
public static void WriteFile(){
try {
RandomAccessFile raf = new RandomAccessFile("IO_RandomAccessFile","rw");//"rw"是进行使用的权限;
raf.write("ok,this is RandomAccessFile".getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
}
17.而且该对象的构造函数要操作的文件不存在,会自动创建,如果存在则不会覆盖
18.如果模式为只读r,不会创建问件,会读取一个已存在文件,如果文件不存在,则会出现异常,
19.如果模式为rw,
20.操作基本数据类型:DataInputStream与DataOutputStream.(用于多线程是不安全的)
21.使用DataInputStream 必须按照数据类型的存储数据进行读取(写入的数据可以是UTF, UTF-8, gbk)
22.代码示例:
package earth;
import java.io.*;
public class Lang_Data01 {
public static void main(String[] args) throws IOException{
//此处要注意一定要按照先创建后读取的顺序进行注册;
DataOutput();
DataInput();
//这是进行gbk类型的操作
DataOutput01();
DataInput01();
}
//将数据流写入
public static void DataOutput() throws IOException{
//使用DataOutputStream 进行数据的写入
DataOutputStream dos = new DataOutputStream(new FileOutputStream("lang01.txt"));
dos.writeUTF("你好");
dos.close();
}
//读取数据流
public static void DataInput() throws IOException{
//使用 DataInputStream来读取数据
DataInputStream dis = new DataInputStream(new FileInputStream("lang01.txt"));
//获取数据
String s = dis.readUTF();
System.out.println(s);
dis.close();
}
//另外一种gbk的代码的 书写程序
public static void DataOutput01() throws IOException{
OutputStreamWriter dos1 = new OutputStreamWriter(new FileOutputStream("lang02.txt"),"UTF-8");
dos1.write("你好我是二号 ");
dos1.close();
}
public static void DataInput01() throws IOException{
DataInputStream dis01 = new DataInputStream(new FileInputStream("lang02.txt"));
String s01 = dis01.readUTF();
System.out.println(s01);
dis01.close();
}
}
23.操作字节数组:ByteArrayInputStream与ByteArrayOutputStream
24.ByteArrayInputStream:在构造的时候,需要接受数据源,而且数据源是一个字节数组。
25.ButeArrayOutputStream:在构造的时候不用定义数据目的,因为该对象中已经内部封装了可变长度的
字节数组,这就数据的目的。
26.因为这两个流对象都操作的数组,并没有使用系统资源,所以,不用进行close关闭。
27.源设备:
键盘 System.in , 硬盘 FileStream , 内存 ArrayStream
28.目的设备:
控制台 System.out , 硬盘 FileStream ,内存 ArrayStream
29.操作字符数组:CharArrayReader与CharArrayWrite
30.操作字符串:StringReader与StringWriter
31.字符编码示例代码:
package earth;
import java.io.*;
public class Io_UTF01 {
public static void main(String[] args) throws IOException{
WriteText();
ReadText();
}
//进行数据的输入
public static void WriteText() throws IOException{
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("osw.txt"),"gbk");
osw.write("你好");
osw.close();
}
//进行数据的输出
public static void ReadText() throws IOException{
InputStreamReader isr = new InputStreamReader(new FileInputStream("osw.txt"),"gbk");
//这是将字符流转化成字节流的专业的类
//创建数组
char[] ch = new char[10];
int len = isr.read(ch);
//将数据转化成字符串
String s = new String(ch, 0, len);
System.out.println(s);
//将数据输出;
isr.close();
//关闭数据
}
}
32.编码:字符串变成字节数组(String-->byte[]; str.getBytes())
32.解码:字节数组变成字符串(byte[]-->String; new String(byte[]))
33.代码示例:
package earth;
import java.io.*;
import java.util.Arrays;
public class Io_UTF02 {
public static void main(String[] args) throws IOException{
String s = "你好";
//建立一个字符数据
//进行输出测试,获取UFT的关于“你好”的编码
byte[] b = s.getBytes("UTF-8");
//里面要写入编码格式
System.out.println(Arrays.toString(b));
//“Arrays.toString”返回指定数组内容的字符串的表示形式
//另外创立一个也是以UTF-8进行编码的字符,并进行展示
String s1 = new String(b,"UTF-8");
//里面写入解码格式
System.out.println(s1);
}
}
34.Comparable<T>是比较此对象与指定对象的顺序
35.示例代码:
package earth;
import java.io.*;
import java.util.*;
public class Io_UTF03 {
public static void main(String[] args) throws IOException{
Set<Student> stu = StudentTool.getStudents();
StudentTool.StudnetOut(stu);
}
}
//创建一个学生类,同时调用自然比较方法的接口
class Student implements Comparable<Student>{
//创建需要用到的变量
private String name;
private int math,chinese,english;
private int sum; //总成绩
//进行示例化操作
public Student(String name, int math, int chinese, int english){
this.name = name;
this.math = math;
this.chinese = chinese;
this.english = english;
sum = math + chinese + english;
}
//覆盖方法,compareTo比较此对象与指定对象的顺序
public int compareTo(Student st){
int num = new Integer(this.sum).compareTo(new Integer(st.sum));
if(num == 0){
return this.name.compareTo(st.name);
}
return num;
}
//获取数值
public String getName(){
return name;
}
public int getSum(){
return sum;
}
//对于不确定的数值要去取值到哈希机中去
public int hashCode(){
return name.hashCode() + sum * 78;
}
//对数据中信息进行比对
public boolean equals(Object obj){
//如果身份不是学生测抛出异常
if(!(obj instanceof Student)){
throw new ClassCastException("类型不匹配");
}
Student st = (Student)obj;
//返回数据
return this.name.equals(st.name) && this.sum == st.sum;
}
//使用toString的方法来界定显示的格式
public String toString(){
return "student["+ name + "," + math +","+ chinese +"," + "english" + "]";
}
}
//建立一个学生类中要使用的工具
class StudentTool{
//创建学生集合
public static Set<Student> getStudents() throws IOException{
//读取键盘数值
BufferedReader bd = new BufferedReader(new InputStreamReader(System.in));
String line = null;
Set<Student> stu = new TreeSet<Student>();
while((line = bd.readLine()) != null){
//设置一个结束命令
if("over".equals(line)){
break;
}
String[] info = line.split(",");
Student st = new Student(info[0], Integer.parseInt(info[1]), Integer.parseInt(info[2]), Integer.parseInt(info[3]));
stu.add(st);
}
bd.close();
//关闭流;
return stu;
//返回数值;
}
//输出数据
public static void StudnetOut(Set<Student> stu)
throws IOException{
BufferedWriter bw = new BufferedWriter(new FileWriter("studentinformation.txt"));
//一次进行遍历
for(Student st : stu){
bw.write(st.toString() + "");
bw.write(st.getSum() + "");
bw.newLine();
bw.flush();
}
}
}
---------------------- ASP.Net+Android+IOS开发、.Net培训、期待与您交流! ----------------------
相关文章推荐
- 黑马程序员---Java基础--21天(IO流之四)
- 黑马程序员——IO流(二)
- 黑马程序员--IO流部分总结
- 黑马程序员--Java面向对象——IO流(字节流读取流)
- 黑马程序员--java技术blog---第六篇:IO流(5)
- 黑马程序员_Java语言_IO流(序列流)
- 黑马程序员_IO流学习心得
- 黑马程序员 IO流
- 黑马程序员-----IO流(3)File类、打印流、序列化、管道流、字符编码等
- 黑马程序员_java基础学习IO流学习笔记
- 黑马程序员--Java基础学习之IO流之字节流、字符流、读取写入文件、Copy文件、键盘输入输出、流操作的基本规律
- 黑马程序员-Java语言基础– IO流 第19天
- 黑马程序员——>第十九天<io流(BufferedWriter-装饰设计模式-流操作规律)>
- 黑马程序员————IO流(二)
- 黑马程序员--IO流2
- 黑马程序员_IO流
- 『黑马程序员』第六话<IO流>
- 黑马程序员——IO流
- 黑马程序员 IO流(二)
- 黑马程序员--Java_IO流(二)