文件输入输出操作(字节流)

上微信头条,C币就是你的CSDN社区之星徐宜生:爱分享、爱极客! 最流行的语言想学就学写博文,传代码,,赚C币

文件输入输出操作(字节流)

分类:java

java

package test;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.util.ArrayList;import java.util.List;public class ArraysDemo{public static void main(String args[]) throws IOException{write1();read1();write2();read2();}public static void write1() throws IOException{File f = new File("d:"+File.separator+"text.txt") ;// 实例化File类的对象OutputStream out=new FileOutputStream(f);out.write("hello".getBytes());out.close();}public static void write2() throws IOException{File f = new File("d:"+File.separator+"text.txt") ;// 实例化File类的对象OutputStream out=new FileOutputStream(f,true);out.write("\r\nhello".getBytes());//\r\n换行out.close();}public static void read1() throws IOException{File f = new File("d:"+File.separator+"text.txt") ;// 实例化File类的对象InputStream input=new FileInputStream(f);byte b[]=new byte[(int)f.length()];input.read(b);input.close();System.out.println("read1():\n"+new String(b));}public static void read2() throws IOException{File f = new File("d:"+File.separator+"text.txt") ;// 实例化File类的对象InputStream input=new FileInputStream(f);byte temp=0;List<Byte> list=new ArrayList<Byte>();while((temp=(byte) input.read())!=-1){list.add(temp);}byte[] b=new byte[list.size()];for(int i=0,j=list.size();i<j;i++){b[i]=list.get(i);}input.close();System.out.println("read2():\n"+new String(b));}};/**read1():helloread2():hellohello**/

上一篇RandomAccessFileDemo下一篇文件输入输出操作(字符流)

顶0踩0

主题推荐猜你在找

查看评论

* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

核心技术类目

使用双手头脑与心灵的是艺术家,只有合作双手

文件输入输出操作(字节流)

相关文章:

你感兴趣的文章:

标签云: