Chinaunix首页 | 论坛 | 博客
  • 博客访问: 419386
  • 博文数量: 79
  • 博客积分: 2886
  • 博客等级: 少校
  • 技术积分: 968
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-16 10:33
文章分类

全部博文(79)

文章存档

2013年(7)

2012年(17)

2011年(28)

2010年(25)

2009年(1)

2008年(1)

我的朋友

分类: Java

2011-03-30 14:45:24

1.     

标准输入输出流对象 System类的静态成员变量

1)     System.in InputStream类型的,代表标准输入流,这个流是已经打开了的,默认状态对应于键盘输入。

2)     System.outPrintStream类型的,代表标准输出流,默认状态对应于屏幕输出

3)     System.errPrintStream类型的,代表标准错误信息输出流,默认状态对应于屏幕输出

2.     读取字节流.

//TestStandard.java

//从键盘读入信息并在显示器上显示

package com.io.standard;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class TestStandard {
    public static void main(String[] args) {
        BufferedReader in = new BufferedReader(newInputStreamReader(System.in));
        String s;
        try {
            while((= in.readLine()).length() != 0)
                System.out.println(s);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
/*
 output:
 hello 
 hello
 */


1)     ByteArrayInputStream可从字节数组中读取数据

public int read(byte b[],int off,int len);

 

 

eg: 

2)     ByteArrayOutputStream可向字节数组写入数据

public void write(int b)

 

public void write(byte b[],int off,int len)

 

public void wtiteTo(OutputStream out) throws IOException;

 

public byte[] toByteArray()

建立一个新的数组,将缓冲区的数据复制到该数组中,并返回存放当前输出流内容的数组。

public String toString()

根据操作系统默认的编码格式,将内部缓冲区的数据转换为一个字符串返回。

 

3)     FileInputStream

 

int read(byte[] buf, int offset, int count)

 

public int read(byte[] buf)

 


package com.io.standard;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class TestFileInputStream {
    public static void main(String[] args) {
        InputStream in = null;
        try{
            in=new FileInputStream(args[0]);//以指定文件数据作为数据流

            int total=0;
            while(in.read()!=-1)
                total++;
            System.out.println(total+"bytes");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block

            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block

            e.printStackTrace();
        } finally{
            if(in!=null)
                try{ in.close(); }
                catch(IOException e){ }
        }
    }
}

 

4)     FileOutputStream

void write(byte[] buf, int offset, int count)

 

public void write(byte[])

 

package com.io.standard;

import java.io.IOException;

public class TestFileOutputStream {
    public static void main(String[] args) {
        byte from=(byte)args[0].charAt(0);
        byte to=(byte)args[1].charAt(0);
        int b;
        try {
            while((b=System.in.read())!=-1)
             System.out.write(b==from ? to : b);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 

3.     读取Unicode字符流

1)     Reader

public int read()throws IOException

从输入流中读取一个字符

public int read(char c [])throws IOException

将输入的数据存放在指定的字符数组

public abstract int read(char c[],int offset,int len) throws IOException

 

从输入流中的offset位置开始读取len个字符,并存放在指定的数组中

 

2)     Writer

public void write(int c) throws IOException

 

写一个字符

 

public void write (char cbuf[])throws IOException

写一个字符数组

 

public abstract void write (char cbuf[],int offset,int len) throws IOException

 

将字符数组cbuf中从offset 位置开始的len个字符写到输出流中

public  void write(String str) throws IOException

写一个字符串

 

public void write (String str,int offset,int len)throws IOException

 

将字符串从offset位置开始,长度为len个字符数组的数据写到输出流中

 

 

3)     FileReaderFileWriter类用于字符文件的输入输出处理

public FileReader(File file) throws FileNotFoundException

 

 

public FileReader(String filename) throws FileNotFoundException

 

 

public FileWriter(File file) throws IOException

 

 

public FileWriter(String fileName,boolean append)throws IOException

 


//TestFileReader.java

//FileReader一个字符一个字符地读取整个文件。

package com.io.standard;
import java.io.FileReader;
public class TestFileReader {
    public static void main(String[] args) {
        FileReader fr;
        int ch;
        try{
            fr=new FileReader("d:\\newHello.txt");
            while((ch=fr.read())!=-1){
                 System.out.print((char)ch);
            }
          }catch(Exception e){
             e.printStackTrace();
         }
    }
}


//TestFileWriter.java

//字符流写入文件

package com.io.standard;

import java.io.FileWriter;
import java.io.IOException;

public class TestFileWriter {
    public static void main(String[] args) {
        String fileName = "d:\\oldHello.txt";
        try {
            FileWriter writer = new FileWriter(fileName, true);
            writer.write("Hello!\n");
            writer.write("This is my first text file,\n");
            writer.write("You can see how this is done. \n");
            writer.write("输入一行中文也可以\n");
            writer.close();
        } catch (IOException iox) {
            System.out.println("Problem writing" + fileName);
        }
    }
}

 

4)     BufferedReaderBufferedWriter类以缓冲区方式进行输入输出(FileReaderFileWriter类以字符为单位进行输入输出,无法进行整行输入与输出,数据的传输效率很低。)

readLine()

读取文本行

read()

读取字符

package com.io.standard;

import java.io.BufferedReader;
import java.io.FileReader;

public class TestBufferReader {
    public static void main(String[] args) {
        FileReader fr;
        BufferedReader br;
        int ch;
        String line;
        try {
            fr = new FileReader("d:\\newHello.txt");
            br = new BufferedReader(fr);
            line = br.readLine();
            //读取一行 

            System.out.println("begin read line:");
            while (line != null) {
                System.out.println(line);
                line = br.readLine();

            }
            //读取一个字符

            System.out.println("begin read char:");
            while ((ch = br.read()) != -1) {//此时已经读到文件尾,不再读文件

                System.out.print((char) ch);
            }
            fr.close();
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

4.     File

public File(String path):

使用指定路径构造一个对象。

public File(String path, String name): 

使用指定路径和字符串构造一个对象。

public File(File dir, String name):

使用指定文件目录和字符串构造一个对象。

getName()   

获取对象所代表的文件名

getParent()  

获取文件对象的路径的父类信息

getPath()     

获取对象所代表文件的路径名

canRead()   

测试能否从指定的文件中读数据

canWrite()  

测试能否对指定的文件写入数据

exists()        

测试文件是否存在

length()       

获取文件对象所代表的文件长度

list()            

获取文件对象指定的目录中文件列表

getAbsolutePath() 

获取文件的绝对路径

getCanonicalPath()

获取文件对象路径名的标准格式

isAbsolute()

测试此文件对象代表的文件是否是绝对路径

isDirectory()          

测试此文件对象代表的文件是否是一个目录

isFile()      

测试此对象所代表的是否是一个文件

delete()      

删除此对象指定的文件

makdir()    

创建一个目录,其路径名由此文件对象指定




package com.io.standard;

import java.io.File;

public class TestFile {
    public static void main(String[] args) {
        //创建文件Hello.txt,如果存在则删除旧文件,不存在则直接创建新的

        File f = new File("d:" + File.separator + "Hello.txt");
        if (f.exists())
            f.delete();
        else
            try {
                f.createNewFile();
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
    }
}


阅读(785) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~