---------------------- android培训、java培训、期待与您交流! ----------------------
IO这部分说真的,我跟过的几个项目都很少用到,可能是做外包的缘故吧,所以我尽量把每个例子都练一练。
1.file类的基本应用
import java.io.File;
import java.io.IOException;
import java.util.Date;
public class FileTest {
public static void main(String[] args) throws IOException {
File f = new File("1.txt");
if(f.exists()){
f.delete();
}else{
f.createNewFile();
}
System.out.println("File name:" + f.getName());
System.out.println("File path:" + f.getPath());
System.out.println("File abs path:" + f.getAbsolutePath());
System.out.println("File parent:" + f.getParent());
System.out.println(f.exists()?"exist":"not exist");
System.out.println(f.canRead()?"read":"not read");
System.out.println(f.isDirectory()?"Directory":"not Directory");
System.out.println("File last modifyTime:" + new Date(f.lastModified()));
}
}
2.RandomAccessFile类支持随机访问方式,读写等长记录格式的文件时有很大优势。
应用举例:往文件中写入三名员工的信息,每个员工含有姓名和年龄两个字段,然后按照第二名、第一名、第三名的先后顺序读出员工信息。
public class Employee {
public String name;
public int age;
public static final int LEN=8;
public Employee(String name,int age){
if(name.length() > LEN){
name = name.substring(0, LEN);
}else{
while(name.length() < LEN){
name += "\u0000";
}
}
this.name = name;
this.age = age;
}
}
import java.io.IOException;
import java.io.RandomAccessFile;
public class RandomAccessFilexy {
public static void main(String[] args) throws IOException {
Employee e1 = new Employee("张三",23);
Employee e2 = new Employee("lisi",258);
Employee e3 = new Employee("wangwu",25);
RandomAccessFile ra = new RandomAccessFile("employee.txt","rw");
ra.writeChars(e1.name);
ra.writeInt(e1.age);
ra.writeChars(e2.name);
ra.writeInt(e2.age);
ra.writeChars(e3.name);
ra.writeInt(e3.age);
ra.close();
String strName = "";
RandomAccessFile raf = new RandomAccessFile("employee.txt","r");
raf.skipBytes(Employee.LEN*2 + 4);//跳转到相应的字节
for(int i=0;i < Employee.LEN;i++){
strName += raf.readChar();//读取一个字符
}
System.out.println(strName + ":" +raf.readInt());
raf.seek(0);//回到首字节
strName = "";
for(int i=0;i < Employee.LEN;i++){
strName += raf.readChar();
}
System.out.println(strName + ":" +raf.readInt());
raf.skipBytes(Employee.LEN*2 + 4);
strName = "";
for(int i=0;i < Employee.LEN;i++){
strName += raf.readChar();
}
System.out.println(strName + ":" +raf.readInt());
raf.close();
}
}
中文和英文一个字符转换成字节是不一样的,一个中文字符为两个字节,一个英文字符为一个字节。
3.流是字节序列的抽象概念。分为两大类:节点流类和过滤流类(处理流类)
4.flush()方法清空内存缓冲区并输出到IO设备中。内存缓冲区提高cpu的使用率,write方法并没有真正的写入IO设备,程序有机会撤回写入的部分数据,降低单个应用程序自身的效率
5.FileInputStream实例对象时,文件是存在和可读的。FileOutputStream实例对象时,如果文件存在,这个文件的原来内容被覆盖。
6.举例:用FileOutputStream类项文件中写入一个字符串,然后用FileInputStream读出写入的内容
import java.io.*;
public class FileStream {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
FileOutputStream out = new FileOutputStream("hello.txt");
out.write("www.xing.com".getBytes());
out.close();
File f = new File("hello.txt");
FileInputStream in = new FileInputStream(f);
byte[] buf = new byte[1024];
int len = in.read(buf);
System.out.println(new String(buf,0,len));
}
}
7.Reader和Writer是所有字符流类的抽象基类。用于读写文本文件,inputStream和outputStream用于读写二进制文件。
8. Reader和Writer举例
import java.io.*;
public class FileStream2 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
FileWriter out = new FileWriter("hello2.txt");
out.write("www.sss.com");
out.close();
char[] buf = new char[1024];
FileReader in = new FileReader("hello2.txt");
int len = in.read(buf);
System.out.println(new String(buf,0,len));
}
}
注:FileoutputStream的writer方法里面自动调用flush方法,但FileWriter不会。
9.PipedInputStream类与PipedOutputStream类用于在应用程序中的创建管道通信。(线程之间的通信)
10. PipedInputStream类与PipedOutputStream类举例:
import java.io.PipedOutputStream;
public class Sender extends Thread{
private PipedOutputStream out = new PipedOutputStream();
public PipedOutputStream getOutputStream(){
return out;
}
public void run(){
String strInfo = new String("hello,receiver");
try {
out.write(strInfo.getBytes());
out.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
import java.io.PipedInputStream;
public class Receiver extends Thread{
private PipedInputStream in = new PipedInputStream();
public PipedInputStream getInputStream(){
return in;
}
public void run(){
byte[] buf = new byte[1024];
try {
int len = in.read(buf);
System.out.println("the following message comes from" +
new String(buf,0,len));
in.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
import java.io.*;
public class PipedStreamTest {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Sender t1 = new Sender();
Receiver t2 = new Receiver();
PipedOutputStream out = t1.getOutputStream();
PipedInputStream in = t2.getInputStream();
out.connect(in);
t1.start();
t2.start();
}
}
11.使用管道流类,可以实现各个程序模块之间的松耦合通信。
12.ByteArrayInputStream与ByteArrayOutputStream,用于以IO流的方式来完成对字节数组内容的读写,来支持类似内存虚拟文件或者内存映像文件的功能。
13. ByteArrayInputStream与ByteArrayOutputStream举例:编写一个把输入流中的所有英文字母变成大写字母,然后将结果写入到一个输出流对象。用这个函数来将一个字符串中的所有字符转换成大写。
import java.io.*;
public class ByteArrayTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
String tmp = "abdfsgfwsegsfs";
byte[] src = tmp.getBytes();
ByteArrayInputStream input = new ByteArrayInputStream(src);
ByteArrayOutputStream output = new ByteArrayOutputStream();
transform(input,output);
byte[] result = output.toByteArray();
System.out.println(new String(result));
}
public static void transform(InputStream in,OutputStream out){
int ch;
try {
while ((ch = in.read()) != -1) {
int upperCh = Character.toUpperCase(ch);
out.write(upperCh);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
14.StringReader类和StringWriter类来以字符IO流的方式处理字符串。
15.System.in连接到键盘,是InputStream类型的实例对象,.System.out连接到显示器,是PrintStream类的实例对象。
16.在windows下,按下ctrl+z可以产生键盘输入流的结束标记。在linux下按ctrl+d。
17.编程举例:借助上一页编写的函数,将键盘上输入的内容转变成大写字母后打印在屏幕上. transform(System.in,System.out);
---------------------- android培训、java培训、期待与您交流! ----------------------
详细请查看:http://edu.csdn.net/heima