首页 > 代码库 > java_io字节流操作

java_io字节流操作

看代码:

package wkl.file;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.FileWriter;import java.io.InputStream;import java.io.OutputStream;import java.io.Writer;import javax.imageio.stream.FileImageInputStream;/** * 字节流的输入流和输出流基础是InputStream和OutputStream这两个抽象类,字节流的输入输出操作由这两个类的子类实现。 * @author Administrator * */public class InoutStreamTest {    //字节流:向文中写入字符串    public static void write22() throws Exception{        File file = new File("G:\\xxx\\fd.txt");        OutputStream out = new FileOutputStream(file);        String string = "你好吗000990";        byte[] b = string.getBytes();//把字符串转换成字节数组        out.write(b);    //会把原来文件的内容先清除再写入,追加在后面讲        /*for(int i=0;i<b.length;i++){            out.write(b[i]);        }*/        out.close();    }         //向文件中追加新内容    public static void append() throws Exception{        File file = new File("G:\\xxx\\fd.txt");        OutputStream out = new FileOutputStream(file,true); //追加格式        String str = "2015你好";        byte[] b = str.getBytes();        out.write(b);        out.close();    }        //读文件的内容    public static void reads() throws Exception{        File file = new File("G:\\xxx\\fd.txt");        InputStream in =new FileInputStream(file);        int len =0;        byte [] b = new byte[1024] ;        while((len=in.read(b))!=-1){            System.out.println(new String(b));//可以正常输出中文        }    }            //字符流:写入数据    public static void wri() throws Exception{        File file = new File("G:\\xxx\\fd.txt");        Writer writer = new FileWriter(file);        String str = "我是字符流";        writer.write(str);  //先清除原来的内容再写入新的内容        writer.close();    }        public static void main(String args[]) throws Exception{        //write22();        //append();        //reads();        wri();    }}

 

java_io字节流操作