Java流

//五个类

--|file类:直接与文件本身有关系

--|字节流 OutputStream InputStream 

--|字符流 Writer Reader

存放在文件中的都是字节。而读到内存中才可能变成字符

打印流:printStream/PrintWriter:提供了各种打印功能,可以输出任意的内容,根据实例化对象的不同

输出的位置也不同

package java11.IO;

 

import java.io.*;

public class FileTest

{

public static void main(String args[]){

makeDir("D://ACM");

}

//运用递归操作打印所有的目录

public static void makeDir(String dir){

try{

File f = new File(dir);

String str[] = null;

if(f.isDirectory()){

str = f.list();

for(int i =0;i<str.length ; i++){

//separatorChar=="//"

//makeDir(dir+"//"+str[i]);

makeDir(dir+f.separatorChar+str[i]);

}

}else{

System.out.println(dir);

}

}catch(Exception e){

}

}

}

 

package java11.IO;

 

import java.io.*;

public class OutputStreamTest

{

//OutputStream||InputStream都是操作字节要通过byte进行转换

//如果忘记关闭了。则是一样可以写入数据的。

//但是字符流不关闭,内存中的数据不会强制的输入到文件,字符流

//中使用了缓存,在关闭时会强制性清空缓存,可以通过flush方法

//进行手工清空。

 

//两者之间最大的区别在于,字符流使用了缓存,而字节流则没有用到

public static void main(String args []){

//writeOutputStream();

//readInputStream();

//outputWriter();

inputRead();

}

 

public static void writeOutputStream(){

//要操作的文件

File f = new File("D://outputStram.txt");

OutputStream out = null;

//通过子类FileOutputStream进行实例化

try{

out = new FileOutputStream(f);

}catch(Exception e){

}

//将字符串转化为byte数组

String name = "hao 123..";

byte [] b = name.getBytes();

try{

out.write(b);

}catch(Exception e){

}

try{

//关闭

out.close();

}catch(Exception e){

}

}

 

public static void outputWriter(){

//要操作的文件

File f = new File("D://outputStram.txt");

Writer out = null;

//通过子类FileOutputStream进行实例化

try{

out = new FileWriter(f);

}catch(Exception e){

}

//将字符串直接写入

String name = "hao 123..";

try{

out.write(name);

}catch(Exception e){

}

try{

//关闭

out.flush();

out.close();

}catch(Exception e){

}

}

 

public static void readInputStream(){

//要操作的文件

File f = new File("D://outputStram.txt");

InputStream in = null;

//通过子类FileOutputStream进行实例化

try{

in = new FileInputStream(f);

}catch(Exception e){

}

//声明一个数组接收,这个数组的大小就是一个局限性

byte [] b = new byte[500]; 

try{

int i= in.read(b);

System.out.println(new String(b,0,i));

}catch(Exception e){

}

try{

//关闭

in.close();

}catch(Exception e){

}

}

 

public static void inputRead(){

//要操作的文件

File f = new File("D://outputStram.txt");

Reader in = null;

//通过子类FileOutputStream进行实例化

try{

in = new FileReader(f);

}catch(Exception e){

}

 

char [] b = new char[500]; 

try{

int i= in.read(b);

System.out.println(new String(b,0,i));

}catch(Exception e){

}

try{

//关闭

in.close();

}catch(Exception e){

}

}

}

 

 

package java11.IO;

 

import java.io.*;

 

//管道流就是处理线程之间的通信。

class sendMessage implements Runnable {

PipedOutputStream out = null;

 

public sendMessage() {

out = new PipedOutputStream();

}

 

public PipedOutputStream getOut() {

 

return this.out;

}

 

public void run() {

 

String name = "hao hello word!";

byte[] b = name.getBytes();

try {

 

out.write(b);

 

out.close();

} catch (Exception e) {

 

}

System.out.println("发送send消息:" + name);

}

}

 

class resvMessage implements Runnable {

PipedInputStream in = null;

 

public resvMessage() {

in = new PipedInputStream();

}

 

public PipedInputStream getIn() {

 

return this.in;

}

 

public void run() {

 

byte[] b = new byte[1024];

try {

int i = in.read(b);

System.out.println("接收resv消息:" + new String(b, 0, i));

in.close();

} catch (Exception e) {

 

}

 

}

}

 

import java.io.*;

//内存中的读写字节类ByteArrayInputStreamm

public class ByteArrayTest

{

public static void main(String args[])throws Exception{

String str = "MDLN,LIUWEI";

//字符串大写要求变成小写

byte [] b = str.getBytes();

ByteArrayInputStream in = new ByteArrayInputStream(b);

ByteArrayOutputStream out = new ByteArrayOutputStream();

//向内存中写入数据

//只能一个一个字节的读

int c =0;

while((c=in.read())!=-1){

int ch = (int)Character.toLowerCase((char)c);

//向输出流中写

out.write(ch);

}

b= out.toByteArray();

System.out.println(new String(b));

}

}

 

 

package java11.IO;

 

import java.io.*;

public class RandAccessFileTest

{

public static void main(String args[])throws Exception{

read();

}

public static void read()throws Exception{

//随机读取数据

RandomAccessFile rf=new RandomAccessFile("D://RandAccessTest.txt","rw");

//随机读取有一个限制,就是说如果要操作的话,必须指定好数据存储的长度

//保存姓名(8位字符串)和年龄(int 4)

String name = "zhangsan";

int age = 30 ;

rf.write(name.getBytes());

rf.writeInt(age);

 

name = "lisi    ";

age = 20 ;

rf.write(name.getBytes());

rf.writeInt(age);

 

name = "wangwu";

age = 30 ;

rf.write(name.getBytes());

rf.writeInt(age);

 

 

rf.close();

 

/*RandomAccessFile rf1=new RandomAccessFile("D://RandAccessTest.txt","r");

 

byte[]b =new byte[8];

rf1.read(b);

age = rf1.readInt();

System.out.println(new String(b)+"-->"+age);

*/

//读取第二行的数据

RandomAccessFile rf1=new RandomAccessFile("D://RandAccessTest.txt","r");

rf1.skipBytes(12);

byte[]b1 =new byte[8];

rf1.read(b1);

age = rf1.readInt();

System.out.println(new String(b1)+"-->"+age);

rf1.close();

}

}

 

 

public class PipedOutAndIn {

public static void main(String args[]) {

 

sendMessage s = new sendMessage();

resvMessage r = new resvMessage();

 

Thread t = new Thread(s);

Thread t1 = new Thread(r);

 

PipedOutputStream out = s.getOut();

PipedInputStream in = r.getIn();

 

try {

// 连接管道

out.connect(in);

} catch (Exception e) {

 

}

 

t.start();

t1.start();

}

}

 

 

//用来合并两个文件

 

public class SequenceInputStreamTest

{

public static void main(String args[])throws Exception{

InputStream in1 = null;

InputStream in2 =null;

//创建一个输出流

 

OutputStream out = null;

out = new FileOutputStream(new File("c://lxhMax.txt"));

 

in1 = new FileInputStream(new File("c://lxh1.txt"));

in2 = new FileInputStream(new File("c://lxh2.txt"));

//此处相当于两个文件合并了

SequenceInputStream seq = new SequenceInputStream(in1,in2);

int c = 0;

while((c=seq.read())!=-1){

out.write(c);

}

in1.close();

in2.close();

out.close();

}

}

 

 

//对象序列化

//对象序列化是指把对象写入到流中,对象的反序列化是指从流中恢复对象

//其优点是:即使程序运行结束,对象仍然可以保存下来。必须实现Serializable接口

//一个类想实现序列号,必须实现Serializable接口

//如果某个属性不要求进行序列化,则用关键字transient。

class Person implements Serializable

{

private String name;

//private int age;

//如果某个属性不要求进行序列化

private transient int age;

public Person(String name,int age){

this.name = name;

this.age = age ;

}

public String toString(){

return this.name+" "+this.age ;

}

 

//Serializable接口

//此接口只是一个声明接口,表示实现此接口的类可以被实例化

//如果某个属性不要求进行序列化,则用关键字transient。

public class SerializableTest 

{

public static void main(String args[])throws Exception{

Person p = new Person("liuwei",23);

//序列化。谁为ObjectOutputStream对象实例化就向哪里输出

ObjectOutputStream oos = null;

oos = new ObjectOutputStream(new FileOutputStream(new File("c://123.txt")));

oos.writeObject(p);

//反序列化

ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("c://123.txt")));

Person p1 = (Person)ois.readObject();

System.out.println(p1);

ois.close();

oos.close();

}

}

 

 

//GBK 包含了繁体和简体中文

//GB2312只包含了简体中文

//ISO8859-1包含了国际通用的编码

 

//实际上乱码原因:如果两个编码不匹配,两个操作间的字符集没有统一起来

public class StreamProperties

{

public static void main(String args[])throws Exception{

//设置系统编码字符集

//System.getProperties().put("file.encoding","iso8859-1");

//列出全部的设置属性

//System.getProperties().list(System.out);

OutputStream out =null;

out = new FileOutputStream(new File("c://lxh.txt"));

String str = "huanying lai xiao xuexi 你好啊 " ;

//out.write(str.getBytes());

//转换编码

//out.write(str.getBytes("ISO8859-1"));

out.write(str.getBytes("GB2312"));

out.close();

}

}

 

 

public class Copy

{

public static void main(String args[])throws Exception{

if(args!=null){

String s1 = args[0];

String s2 = args[1];

copy(s1,s2);

}else{

System.out.println("没有路径输入");

}

}

 

public static void copy(String dir1,String dir2)throws Exception{

File f1 = new File(dir1);

File f2 = new File(dir2);

 

FileInputStream in = new FileInputStream(f1);

FileOutputStream out = new FileOutputStream(f2);

int c = 0;

while((c=in.read())!=-1){

out.write(c);

}

 

in.close();

out.close();

}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值