java(IO操作)第一部分 --------I/O流
一、file文件操作
1、file类是唯一代表文件本身的对象,以下代码表示如果文件不存在则创建,若存在则删除
import java.io.* ;
public class IODemo04
{
public static void main(String args[]) throws Exception
{
File f = new File("f:\\abc.txt") ;
if(f.exists())
{
f.delete() ;
}
else
{
f.createNewFile() ;
}
}
};
2、列出某目录下的全部文件
import java.io.* ;
public class IODemo06
{
public static void main(String args[])
{
loop("d:\\") ;
}
public static void loop(String dir)
{
File f = new File(dir) ;
String str[] = null ;
if(f.isDirectory())
{
str = f.list() ;
for(int i=0;i<str.length;i++)
{
loop(dir+"\\"+str[i]) ;
}
}
else
{
System.out.println(dir) ;
}
}
};
今天想下午对代码完善了下,按层次输出。。。。。。。。。。
import java.io.File;
public class IODemo06
{
public static void main(String args[])
{
loop("d:\\常用软件",0);
}
public static void loop(String dir , int i)
{
File f = new File(dir) ;
String str[] = null;
String s = "";
int j;
j = i;
++j;
if(f.isDirectory())
{
for (int t = 1; t < j; t++) {
s = s + "--";
}
System.out.println(s + f);
str = f.list() ;
for(int k=0;k<str.length;k++)
{
loop(dir+"\\"+str[k],j) ;
}
}
else
{
for (int u = 1; u < j; u++) {
s = s + "--";
}
System.out.println(s + dir);
}
}
}
二、随机读取类 RandomAccessFile 介绍
1、随机读取
import java.io.* ;
public class IODemo07
{
public static void main(String args[]) throws Exception
{
// 随机读取
RandomAccessFile raf1 = new RandomAccessFile("f:\\mldn.txt","rw") ;
// 随机读取有一个限制,就是说如果要进行操作,则必须指定好数据的存储长度
// 保存姓名(8位字符串)和年龄(int 4):
String name = "zhangsan" ;
int age = 20 ;
raf1.write(name.getBytes()) ;
raf1.writeInt(age) ;
name = "lisi " ;
age = 30 ;
raf1.write(name.getBytes()) ;
raf1.writeInt(age) ;
name = "wangwu " ;
age = 33 ;
raf1.write(name.getBytes()) ;
raf1.writeInt(age) ;
raf1.close() ;
RandomAccessFile raf2 = new RandomAccessFile("f:\\mldn.txt","r") ;
// 读取第二个人的数据
raf2.skipBytes(12) ;
byte b[] = new byte[8] ;
raf2.read(b) ;
int age2 = raf2.readInt() ;
System.out.println(new String(b)+" --> "+age2) ;
}
};
三、节点流处理流综述
1、流的抽象根类
2、继承从上面四个类的节点流
3、从上面1)承来的处理流
四、节点流介绍
1、字节流FileOutputStream的使用
import java.io.* ;
public class IODemo08
{
public static void main(String args[])
{
// 1、表示要操作lxh.txt文件
File f = new File("f:\\lxh.txt") ;
OutputStream out = null ;
// 2、通过子类实例化
// 使用FileOutputStream子类
try
{
out = new FileOutputStream(f) ;
}
catch (Exception e)
{
}
// 将字符串转化为byte数组
String str = "HELLO MLDN ..." ;
byte b[] = str.getBytes() ;
// 3、将byte数组写入到文件之中,写的是byte数组中的内容
try
{
out.write(b) ;
}
catch (Exception e)
{
}
// 4、关闭文件操作
/*
try
{
out.close() ;
}
catch (Exception e)
{
}
*/
}
};
2、字节流FileInputStream的使用
import java.io.* ;
public class IODemo09
{
public static void main(String args[])
{
File f = new File("f:\\lxh.txt") ;
InputStream in = null ;
try
{
in = new FileInputStream(f) ;
}
catch (Exception e)
{
}
// 声明一个byte数组,用于接收内容
byte b[] = new byte[500] ;
int len = 0 ;
try
{
// 所有的数据都在byte数组中
len = in.read(b) ;
}
catch (Exception e)
{
}
try
{
in.close() ;
}
catch (Exception e)
{
}
System.out.println(new String(b,0,len)) ;
// 输出打印的内容
}
};
3、字符流FileWriter的使用
注意:字符流不同于字节流在程序的最后若没有调用out.close() ;
则内容不会被写入文件中,因为最根本的原因是字符流晕运用了缓存,所以可以在下面程序中
out.write(str) ;语句后面加 out.flush() ;// 表示清空缓存,立即写入文件。
import java.io.* ;
public class IODemo10
{
public static void main(String args[])
{
// 1、表示要操作lxh.txt文件
File f = new File("f:\\lxh.txt") ;
Writer out = null ;
// 2、通过子类实例化
try
{
out = new FileWriter(f) ;
}
catch (Exception e)
{
}
String str = "HELLO MLDN ..." ;
// 3、将字符串写入到文件之中
try
{
out.write(str) ;
}
catch (Exception e)
{
}
// 4、关闭文件操作
/*
try
{
out.close() ;
}
catch (Exception e)
{
}
*/
}
};
4、字符流FileReader的使用
import java.io.* ;
public class IODemo11
{
public static void main(String args[])
{
File f = new File("f:\\lxh.txt") ;
Reader in = null ;
try
{
in = new FileReader(f) ;
}
catch (Exception e)
{
}
// 声明一个char数组,用于接收内容
char b[] = new char[500] ;
int len = 0 ;
try
{
// 所有的数据都在byte数组中
len = in.read(b) ;
}
catch (Exception e)
{
}
try
{
in.close() ;
}
catch (Exception e)
{
}
System.out.println(new String(b,0,len)) ;
// 输出打印的内容
}
};
5、管道流PipedOutputStream 、PipedInputStream 使用
import java.io.* ;
// 定义一个发送者
class SendDemo implements Runnable
{
private PipedOutputStream out ;
public SendDemo()
{
out = new PipedOutputStream() ;
}
public PipedOutputStream getOut()
{
return this.out ;
}
public void run()
{
String str = "Hello MLDN" ;
try
{
out.write(str.getBytes()) ;
out.close() ;
}
catch (Exception e)
{
}
System.out.println("SendDemo --> 发送的内容:"+str) ;
}
};
class ReceDemo implements Runnable
{
private PipedInputStream in = null ;
public ReceDemo()
{
in = new PipedInputStream() ;
}
public PipedInputStream getIn()
{
return this.in ;
}
public void run()
{
byte b[] = new byte[1024] ;
int len = 0 ;
try
{
len = in.read(b) ;
in.close() ;
}
catch (Exception e)
{
System.out.println(e) ;
}
System.out.println("ReceDemo --> 收到的内容是:"+new String(b,0,len)) ;
}
};
public class IODemo12
{
public static void main(String args[])
{
SendDemo sd = new SendDemo() ;
ReceDemo rd = new ReceDemo() ;
Thread send = new Thread(sd) ;
Thread rece = new Thread(rd) ;
// 将两个线程进行连接
PipedOutputStream out = sd.getOut() ;
PipedInputStream in = rd.getIn() ;
// 将输出连接到输入
try
{
out.connect(in) ;
}
catch (Exception e)
{
}
send.start() ;
rece.start() ;
}
};
五、处理流介绍
1、PrintStream的使用(System.out 是一个PrintStream对象,PrintStream类型又是OutputStream子类
)
import java.io.* ;
public class IODemo14
{
public static void main(String args[]) throws Exception
{
// 通过子类完成不同的功能
OutputStream out = null ;
// System.out是PrintStream类型,是OutputStream子类
out = System.out ;
// 现在的out对象具备了向屏幕上打印内容的能力
String str = "HELLO MLDN --> LXH" ;
out.write(str.getBytes()) ;
out.close() ;
}
};
2、printStream/printWrite实际上是一个打印流,他比那些普通的流OutputStream/Write更加擅长打印。(可以打印或写入文件中各种类型)
但是需要在OutputStream/Write的基础上装配(即此处“处理流”需要装配在“节点流”上)
相当于java的装饰模式
import java.io.* ;
public class IODemo15
{
public static void main(String args[]) throws Exception
{
File f = new File("f:\\lxh.txt") ;
// 使用PrintWriter
PrintWriter out = new PrintWriter(new FileWriter(f)) ;
// 具备了向文件中打印数据的能力
out.println(true) ;
out.println(30) ;
out.println("HELLO MLDN") ;
out.close() ;
}
};
import java.io.* ;
public class IODemo15
{
public static void main(String args[]) throws Exception
{
//File f = new File("f:\\lxh.txt") ;
// 使用PrintWriter
// PrintWriter out = new PrintWriter(new FileWriter(f)) ;
PrintWriter out = new PrintWriter(System.out) ;
// 向屏幕中打印数据
out.println(true) ;
out.println(30) ;
out.println("HELLO MLDN") ;
out.close() ;
}
};
3、System.in的使用(System.in是InputStream的实例)
如以下两例就刚好出现了以上1、2两点情况:
import java.io.* ;
public class IODemo17
{
public static void main(String args[]) throws Exception
{
InputStream in = null ;
// 数据等待键盘的输入
in = System.in ;
byte b[] = new byte[7] ;
// 读的时候是等待用户的输入
int len = in.read(b) ;
in.close() ;
System.out.println("输入的内容为:"+new String(b,0,len)) ;
}
};
import java.io.* ;
public class IODemo18
{
public static void main(String args[]) throws Exception
{
InputStream in = null ;
// 数据等待键盘的输入
in = System.in ;
String str = "" ;
int c = 0 ;
while((c=in.read())!=-1)
{
str += (char)c;
}
in.close() ;
System.out.println("输入的内容为:"+str) ;
}
};
4、缓冲流BufferReader具有缓冲功能得到一次性读,刚好可以解决以上两个问题
但是BufferReader需要接受的是一个之类Reader对象,所以需要将System.in这个InputStream对象转换为Reader对象,刚好用到了很常用的转换类(如下图)
(上图说明: 存放在文件中的都是字节,而读到内存中才变成字符)
(此处“处理流”也是装配在“节点流”上)
import java.io.* ;
public class IODemo19
{
public static void main(String args[])
{
BufferedReader buf = null ;
// 此处只是准备了要从键盘中读取数据
buf = new BufferedReader(new InputStreamReader(System.in)) ;
String str = null ;
try
{
System.out.print("请输入内容:") ;
str = buf.readLine() ;
}
catch (Exception e)
{
}
System.out.println("输入的内容为:"+str) ;
}
};