Java基础--IO流之其他流
2015-04-28 11:21
246 查看
凌风博客原创作品。转载请注明出处:http://blog.csdn.net/q549130180/article/details/45333479
打印流:
该流提供了打印方法,可以将各种数据类型的数据都原样打印
字节打印流:
PrintStream
构造函数可以接收的参数类型:
1.file对象, File
2.字符串路径。 String
3.字节输出流。 OutputStream
字符打印流
PrintWriter
构造函数可以接收的参数类型:
1.file对象, File
2.字符串路径。 String
3.字节输出流。 OutputStream
4.字符输出流。 Writer
[java] view
plaincopy
import java.io.*;
class PrintStreamDemo_1
{
public static void main(String[] args) throws IOException
{
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(new FileWriter("a.txt"),true);
String line = null;
while ((line=bufr.readLine())!=null)
{
if("over".equals(line))
break;
out.println(line.toUpperCase());
out.flush();
}
out.close();
bufr.close();
}
}
合并流
[java] view
plaincopy
import java.io.*;
import java.util.*;
class SequenceDemo_2
{
public static void main(String[] args) throws IOException
{
Vector<FileInputStream> v = new Vector<FileInputStream>();
v.add(new FileInputStream("c:\\1.txt"));
v.add(new FileInputStream("c:\\2.txt"));
v.add(new FileInputStream("c:\\3.txt"));
Enumeration<FileInputStream> en = v.elements();
//合并流对象
SequenceInputStream sis = new SequenceInputStream(en);
FileOutputStream fos = new FileOutputStream("c:\\4.txt");
byte[] buf = new byte[1024];
int len = 0;
while ((len=sis.read(buf))!=-1)
{
fos.write(buf,0,len);
}
fos.close();
sis.close();
}
}
切割文件
[java] view
plaincopy
import java.io.*;
import java.util.*;
class SplitFile_3
{
public static void main(String[] args) throws IOException
{
//splitFile();
merge();
}
//合并文件
public static void merge() throws IOException
{
ArrayList<FileInputStream> al = new ArrayList<FileInputStream>();
for (int x =1;x<=3 ;x++ )
{
al.add(new FileInputStream("D:\\abc\\a\\"+x+".part"));
}
//ArrayList无法创建Enumeration对象,使用Iterator来创建
final Iterator<FileInputStream> it = al.iterator();
//创建Enumeration对象
Enumeration<FileInputStream> en = new Enumeration<FileInputStream>()
{
public boolean hasMoreElements()
{
return it.hasNext();
}
public FileInputStream nextElement()
{
return it.next();
}
};
//创建合并流对象
SequenceInputStream sis = new SequenceInputStream(en);
//输出文件位置
FileOutputStream fos = new FileOutputStream("D:\\abc\\a\\0.mp3");
byte[] buf = new byte[1024];
int len = 0;
while ((len=sis.read(buf))!=-1)
{
fos.write(buf,0,len);
}
fos.close();
sis.close();
}
//切割文件
public static void splitFile() throws IOException
{
FileInputStream fis = new FileInputStream("D:\\abc\\1.mp3");
FileOutputStream fos = null;
byte[] buf = new byte[1024*1024];
int len = 0;
int count = 1;
while ((len=fis.read(buf))!=-1)
{
fos = new FileOutputStream("D:\\abc\\a\\"+(count++)+".part");
fos.write(buf,0,len);
fos.close();
}
fis.close();
}
}
对象的序列化
序列化
将对象的状态信息转换为可以存储或传输的形式的过程。在序列化期间,
对象将其当前状态写入到临时或持久性存储区。
以后,可以通过从存储区中读取或反序列化对象的状态,重新创建该对象。
[java] view
plaincopy
import java.io.*;
class ObjectStreamDemo_4
{
public static void main(String[] args) throws Exception
{
//writeObj();
readObj();
}
//将存储到持久性存储介质中的对象取出
public static void readObj() throws Exception
{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("obj.txt"));
Person p = (Person)ois.readObject();
System.out.println(p);
ois.close();
}
//将对象存储到持久性存储介质中
public static void writeObj() throws IOException
{
//创建使对象序列化的对象
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("obj.txt"));
//将对象存储
oos.writeObject(new Person("lisi",39));
oos.close();
}
}
管道流
先执行读取线程,但是没有数据,所以读取线程处于等待状态,
之后执行写入线程,将数据写入到读取线程的管道里,所以读取线程就可以读到数据了
[java] view
plaincopy
//读取线程
class Read implements Runnable
{
private PipedInputStream in;
Read(PipedInputStream in)
{
this.in = in;
}
public void run()
{
//读取数据
try
{
byte[] buf = new byte[1024];
System.out.println("读取前。。。没有数据,阻塞");
int len = in.read(buf); //read方法没有数据是就是等待
System.out.println("读到数据。。。阻塞结束");
String s = new String(buf,0,len);
System.out.println(s);
in.close();
}
catch (IOException e)
{
throw new RuntimeException("管道读取流失败");
}
}
}
//写入线程
class Write implements Runnable
{
private PipedOutputStream out;
Write(PipedOutputStream out)
{
this.out = out;
}
public void run()
{
//写入数据
try
{
System.out.println("开始写入数据前,等待6秒。。。");
Thread.sleep(6000);
out.write("piped lai le".getBytes());//getBytes,转换为字节流
out.close();
}
catch (Exception e)
{
throw new RuntimeException("管道输出流失败");
}
}
}
class PipedStreamDemo_5
{
public static void main(String[] args) throws IOException
{
PipedInputStream in = new PipedInputStream();
PipedOutputStream out = new PipedOutputStream();
in.connect(out);
Read r = new Read(in);
Write w = new Write(out);
new Thread(r).start();
new Thread(w).start();
}
}
RandomAccessFile随机读写访问
该类不算是IO体系中子类
而是直接继承自Object
但是它是IO包中的成员,因为它具备读和写功能
内部封装了一个数组,而且通过指针对数组的元素进行操作
可以通过getFilePointer获取指针位置
同时可以通过seek改变指针的位置
其实完成读写的原理就是内部封装了字节输入流和输出流
通过构造函数可以看出,该类只能操作文件
而且操作文件还有模式:只读r 读写rw等
如果模式为只读 r,不会创建文件,会去读取一个已存在文件,如果该文件不存在,则会出现异常
如果模式为rw,操作的文件不存在,会自动创建,如果存在则不会创建。
主要应用:实现多线程下载
[java] view
plaincopy
import java.io.*;
class RandomAccessFileDemo_6
{
public static void main(String[] args) throws IOException
{
//writeFile();
readFile();
}
//读取数据
public static void readFile() throws IOException
{
RandomAccessFile raf = new RandomAccessFile("ran.txt","r");
//调整对象中指针
//raf.seek(8*1);
//跳过指定的字节数,可以往下跳,不能往回走
raf.skipBytes(8);
byte[] buf = new byte[4];
raf.read(buf);
String name = new String(buf);
int age = raf.readInt();
System.out.println("name"+name);
System.out.println("age"+age);
raf.close();
}
//任意位置插入数据
public static void writeFile_2() throws IOException
{
RandomAccessFile raf = new RandomAccessFile("ran.txt","rw");
raf.write("周七".getBytes());
raf.writeInt(100);
raf.close();
}
//以读写模式写入数据
public static void writeFile() throws IOException
{
RandomAccessFile raf = new RandomAccessFile("ran.txt","rw");
raf.write("李四".getBytes());
raf.writeInt(97);
raf.write("王五".getBytes());
raf.writeInt(99);
raf.close();
}
}
DataInputStream与DataOutputStream
可以用于操作基本数据类型的数据的流对象。
[java] view
plaincopy
import java.io.*;
class DataStreamDemo_7
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
//使用专用的uft-8读取数据
public static void readUTFDemo() throws IOException
{
DataInputStream dis = new DataInputStream(new FileInputStream("utfdate.txt"));
String s = dis.readUTF();
System.out.println(s);
dis.close();
}
//使用专用的uft-8写入数据
public static void writeUTFDemo() throws IOException
{
DataOutputStream dos = new DataOutputStream(new FileOutputStream("utfdate.txt"));
//使用这种方法写入的数据,只有用对应的方法才能取出
dos.writeUTF("你好");
dos.close();
}
//读取数据
public static void readData() throws IOException
{
DataInputStream dis = new DataInputStream(new FileInputStream("data.txt"));
int num = dis.readInt();
boolean b = dis.readBoolean();
double d = dis.readDouble();
System.out.println("num="+num);
System.out.println("b="+b);
System.out.println("d="+d);
dis.close();
}
//写入数据
public static void writeData() throws IOException
{
DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.txt"));
dos.writeInt(234);
dos.writeBoolean(true);
dos.writeDouble(6465.325);
dos.close();
}
}
转换流的字符编码
[java] view
plaincopy
import java.io.*;
class EncodeStream_9
{
public static void main(String[] args) throws IOException
{
System.out.println("Hello World!");
}
//以特定编码表读取文件
public static void readText() throws IOException
{
InputStreamReader isr = new InputStreamReader(new FileInputStream("utf.txt"),"gbk");
char[] buf = new char[10];
int len = isr.read(buf);
String str = new String(buf,0,len);
System.out.println(str);
isr.close();
}
//以特定编码表写入文件
public static void writeText() throws IOException
{
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("utf.txt"),"UTF-8");
osw.write("你好");
osw.close();
}
}
打印流:
该流提供了打印方法,可以将各种数据类型的数据都原样打印
字节打印流:
PrintStream
构造函数可以接收的参数类型:
1.file对象, File
2.字符串路径。 String
3.字节输出流。 OutputStream
字符打印流
PrintWriter
构造函数可以接收的参数类型:
1.file对象, File
2.字符串路径。 String
3.字节输出流。 OutputStream
4.字符输出流。 Writer
[java] view
plaincopy
import java.io.*;
class PrintStreamDemo_1
{
public static void main(String[] args) throws IOException
{
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(new FileWriter("a.txt"),true);
String line = null;
while ((line=bufr.readLine())!=null)
{
if("over".equals(line))
break;
out.println(line.toUpperCase());
out.flush();
}
out.close();
bufr.close();
}
}
合并流
[java] view
plaincopy
import java.io.*;
import java.util.*;
class SequenceDemo_2
{
public static void main(String[] args) throws IOException
{
Vector<FileInputStream> v = new Vector<FileInputStream>();
v.add(new FileInputStream("c:\\1.txt"));
v.add(new FileInputStream("c:\\2.txt"));
v.add(new FileInputStream("c:\\3.txt"));
Enumeration<FileInputStream> en = v.elements();
//合并流对象
SequenceInputStream sis = new SequenceInputStream(en);
FileOutputStream fos = new FileOutputStream("c:\\4.txt");
byte[] buf = new byte[1024];
int len = 0;
while ((len=sis.read(buf))!=-1)
{
fos.write(buf,0,len);
}
fos.close();
sis.close();
}
}
切割文件
[java] view
plaincopy
import java.io.*;
import java.util.*;
class SplitFile_3
{
public static void main(String[] args) throws IOException
{
//splitFile();
merge();
}
//合并文件
public static void merge() throws IOException
{
ArrayList<FileInputStream> al = new ArrayList<FileInputStream>();
for (int x =1;x<=3 ;x++ )
{
al.add(new FileInputStream("D:\\abc\\a\\"+x+".part"));
}
//ArrayList无法创建Enumeration对象,使用Iterator来创建
final Iterator<FileInputStream> it = al.iterator();
//创建Enumeration对象
Enumeration<FileInputStream> en = new Enumeration<FileInputStream>()
{
public boolean hasMoreElements()
{
return it.hasNext();
}
public FileInputStream nextElement()
{
return it.next();
}
};
//创建合并流对象
SequenceInputStream sis = new SequenceInputStream(en);
//输出文件位置
FileOutputStream fos = new FileOutputStream("D:\\abc\\a\\0.mp3");
byte[] buf = new byte[1024];
int len = 0;
while ((len=sis.read(buf))!=-1)
{
fos.write(buf,0,len);
}
fos.close();
sis.close();
}
//切割文件
public static void splitFile() throws IOException
{
FileInputStream fis = new FileInputStream("D:\\abc\\1.mp3");
FileOutputStream fos = null;
byte[] buf = new byte[1024*1024];
int len = 0;
int count = 1;
while ((len=fis.read(buf))!=-1)
{
fos = new FileOutputStream("D:\\abc\\a\\"+(count++)+".part");
fos.write(buf,0,len);
fos.close();
}
fis.close();
}
}
对象的序列化
序列化
将对象的状态信息转换为可以存储或传输的形式的过程。在序列化期间,
对象将其当前状态写入到临时或持久性存储区。
以后,可以通过从存储区中读取或反序列化对象的状态,重新创建该对象。
[java] view
plaincopy
import java.io.*;
class ObjectStreamDemo_4
{
public static void main(String[] args) throws Exception
{
//writeObj();
readObj();
}
//将存储到持久性存储介质中的对象取出
public static void readObj() throws Exception
{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("obj.txt"));
Person p = (Person)ois.readObject();
System.out.println(p);
ois.close();
}
//将对象存储到持久性存储介质中
public static void writeObj() throws IOException
{
//创建使对象序列化的对象
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("obj.txt"));
//将对象存储
oos.writeObject(new Person("lisi",39));
oos.close();
}
}
管道流
先执行读取线程,但是没有数据,所以读取线程处于等待状态,
之后执行写入线程,将数据写入到读取线程的管道里,所以读取线程就可以读到数据了
[java] view
plaincopy
//读取线程
class Read implements Runnable
{
private PipedInputStream in;
Read(PipedInputStream in)
{
this.in = in;
}
public void run()
{
//读取数据
try
{
byte[] buf = new byte[1024];
System.out.println("读取前。。。没有数据,阻塞");
int len = in.read(buf); //read方法没有数据是就是等待
System.out.println("读到数据。。。阻塞结束");
String s = new String(buf,0,len);
System.out.println(s);
in.close();
}
catch (IOException e)
{
throw new RuntimeException("管道读取流失败");
}
}
}
//写入线程
class Write implements Runnable
{
private PipedOutputStream out;
Write(PipedOutputStream out)
{
this.out = out;
}
public void run()
{
//写入数据
try
{
System.out.println("开始写入数据前,等待6秒。。。");
Thread.sleep(6000);
out.write("piped lai le".getBytes());//getBytes,转换为字节流
out.close();
}
catch (Exception e)
{
throw new RuntimeException("管道输出流失败");
}
}
}
class PipedStreamDemo_5
{
public static void main(String[] args) throws IOException
{
PipedInputStream in = new PipedInputStream();
PipedOutputStream out = new PipedOutputStream();
in.connect(out);
Read r = new Read(in);
Write w = new Write(out);
new Thread(r).start();
new Thread(w).start();
}
}
RandomAccessFile随机读写访问
该类不算是IO体系中子类
而是直接继承自Object
但是它是IO包中的成员,因为它具备读和写功能
内部封装了一个数组,而且通过指针对数组的元素进行操作
可以通过getFilePointer获取指针位置
同时可以通过seek改变指针的位置
其实完成读写的原理就是内部封装了字节输入流和输出流
通过构造函数可以看出,该类只能操作文件
而且操作文件还有模式:只读r 读写rw等
如果模式为只读 r,不会创建文件,会去读取一个已存在文件,如果该文件不存在,则会出现异常
如果模式为rw,操作的文件不存在,会自动创建,如果存在则不会创建。
主要应用:实现多线程下载
[java] view
plaincopy
import java.io.*;
class RandomAccessFileDemo_6
{
public static void main(String[] args) throws IOException
{
//writeFile();
readFile();
}
//读取数据
public static void readFile() throws IOException
{
RandomAccessFile raf = new RandomAccessFile("ran.txt","r");
//调整对象中指针
//raf.seek(8*1);
//跳过指定的字节数,可以往下跳,不能往回走
raf.skipBytes(8);
byte[] buf = new byte[4];
raf.read(buf);
String name = new String(buf);
int age = raf.readInt();
System.out.println("name"+name);
System.out.println("age"+age);
raf.close();
}
//任意位置插入数据
public static void writeFile_2() throws IOException
{
RandomAccessFile raf = new RandomAccessFile("ran.txt","rw");
raf.write("周七".getBytes());
raf.writeInt(100);
raf.close();
}
//以读写模式写入数据
public static void writeFile() throws IOException
{
RandomAccessFile raf = new RandomAccessFile("ran.txt","rw");
raf.write("李四".getBytes());
raf.writeInt(97);
raf.write("王五".getBytes());
raf.writeInt(99);
raf.close();
}
}
DataInputStream与DataOutputStream
可以用于操作基本数据类型的数据的流对象。
[java] view
plaincopy
import java.io.*;
class DataStreamDemo_7
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
//使用专用的uft-8读取数据
public static void readUTFDemo() throws IOException
{
DataInputStream dis = new DataInputStream(new FileInputStream("utfdate.txt"));
String s = dis.readUTF();
System.out.println(s);
dis.close();
}
//使用专用的uft-8写入数据
public static void writeUTFDemo() throws IOException
{
DataOutputStream dos = new DataOutputStream(new FileOutputStream("utfdate.txt"));
//使用这种方法写入的数据,只有用对应的方法才能取出
dos.writeUTF("你好");
dos.close();
}
//读取数据
public static void readData() throws IOException
{
DataInputStream dis = new DataInputStream(new FileInputStream("data.txt"));
int num = dis.readInt();
boolean b = dis.readBoolean();
double d = dis.readDouble();
System.out.println("num="+num);
System.out.println("b="+b);
System.out.println("d="+d);
dis.close();
}
//写入数据
public static void writeData() throws IOException
{
DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.txt"));
dos.writeInt(234);
dos.writeBoolean(true);
dos.writeDouble(6465.325);
dos.close();
}
}
转换流的字符编码
[java] view
plaincopy
import java.io.*;
class EncodeStream_9
{
public static void main(String[] args) throws IOException
{
System.out.println("Hello World!");
}
//以特定编码表读取文件
public static void readText() throws IOException
{
InputStreamReader isr = new InputStreamReader(new FileInputStream("utf.txt"),"gbk");
char[] buf = new char[10];
int len = isr.read(buf);
String str = new String(buf,0,len);
System.out.println(str);
isr.close();
}
//以特定编码表写入文件
public static void writeText() throws IOException
{
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("utf.txt"),"UTF-8");
osw.write("你好");
osw.close();
}
}
相关文章推荐
- 黑马程序员——Java基础---io流(io中的其他对象)
- Java语言基础-IO流(输入输出流) 其他
- 黑马程序员——Java基础---IO流(File类、Properties类、其他流)
- Java基础——其他类对象,IO流
- 黑马程序员——Java语言基础——07.IO流(2)File类和其他流
- 黑马程序员java基础之io流中的其他流
- Java基础知识IO流(其他类)
- JAVA基础 day23 IO流的其他类 (对象的序列化 管道流 RandomAccessFile类(随机读取文件) DateStream(操作基本数据类型的流) 操作数组的流 )字符编码问题
- Java基础视频教程第21天_IO流之其他流对象、字符编码
- 黑马程序员--Java基础学习(其他对象和IO流)第十八天
- 黑马程序员--java 基础之IO流 其他IO类 --08
- 黑马程序员——java基础——IO流中的其他常用类
- Java基础进阶_day12_(其他IO流)
- 黑马程序员_Java基础_其他对象及IO流(一)_18
- Java基础视频教程第18天_其他常用对象、IO流一
- 黑马程序员——java基础知识之其他对象和IO流(一)
- 二十、Java基础--------IO流之其他对象
- 黑马程序员-JAVA基础-IO流其他类
- 黑马程序员_Java基础_IO流_打印流,合并流,分割文件,对象持久化,管道流...其他常用流
- 黑马程序员——java基础——IO流(2)File类,IO常用流对象及IO包中其他类