Java基础(十),流

来源:互联网 发布:销售类crm系统源码 编辑:程序博客网 时间:2024/06/13 06:52

java中的流的分类,
1.字节流
2.字符流
3.包装流

字节流是基础,inputStream和outputStream,可以处理任何文件。
将字节数组转换成字符串:byte [] bytes;String string = new String(bytes,”UTF-8”);//可以不指定编码。
将字符串转化成字节数组:byte[] bytes = str.getBytes(“utf-8”);


Reader的使用方法:
1.read();//读取单个字符

public static void main(String[] args) throws IOException {        File file = new File("src/bgzy/org/updatename/Test.java");        Reader reader = new FileReader(file);        int i = 0;        while (i != -1) {            if ((i = reader.read()) != -1) {                System.out.print((char) i);            }        }    }

1.read(char c);//读取单个字符

package bgzy.org.updatename;import java.io.File;import java.io.FileReader;import java.io.IOException;import java.io.Reader;public class ReadDemo {    public static void main(String[] args) throws IOException {        char c[] = new char[200];        File file = new File("src/bgzy/org/updatename/Test.java");        Reader reader = new FileReader(file);        int i = 0;        while (true) {            i = reader.read(c);            if (i == -1) {                break;            }            for (int j = 0; j < i; j++) {                System.out.print(c[j]);            }        }        reader.close();    }}

3.read(char c,int off,int len);//读取单个字符

public static void main(String[] args) throws IOException {        File file = new File("src/bgzy/org/updatename/Test.java");        Reader reader = new FileReader(file);        int i;        char c[] = new char[20];        int len = 10;        while ((len = reader.read(c, 0, c.length)) != -1) {            for (int j = 0; j < len; j++) {                System.out.print(c[j]);            }        }        reader.close();    }

小例子:拷贝一个文件

package bgzy.org.updatename;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.Reader;import java.io.Writer;public class ReaderWriter {    public static void main(String[] args) {        // 写好要读取和写入的文件        String inputPath = "src/bgzy/org/updatename/Test.java";        String outputPath = "TestCopy.java";        // 创建对应的File对象        File inFile = new File(inputPath);        File oupFile = new File(outputPath);        // 创建读取和写入的对象        Reader reader = null;        Writer writer = null;        try {            reader = new FileReader(inFile);            writer = new FileWriter(oupFile);            int len = -1;            char datas[] = new char[20];            // 边读边写            while ((len = reader.read(datas, 0, datas.length)) != -1) {                writer.write(datas, 0, len);            }        } catch (FileNotFoundException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } finally {            try {                writer.close();                reader.close();            } catch (IOException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }        }    }}

小例子:使用PrintWrite来打印

public static void main(String[] args) {        File file = new File("contact2.xml");        BufferedReader bufferedReader = null;        PrintWriter writer = null;        try {            bufferedReader = new BufferedReader(new FileReader(file));            writer = new PrintWriter(new FileWriter(file));            writer.println("<sdfghjk>");            // writer.write();        } catch (FileNotFoundException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } finally {            try {                bufferedReader.close();                writer.close();            } catch (IOException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }        }    }
0 0
原创粉丝点击