java IO流 读取文件 && 文件复制

来源:互联网 发布:sql sever 导入 excel 编辑:程序博客网 时间:2024/05/17 02:46

用InputStream读取文件

import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;public class TestInputStream {public static void main(String args[]){int b = 0;FileInputStream in = null;try{in = new FileInputStream("D:\\androidPractice\\J2SE\\src\\com\\why\\TestInputStream.java");}catch(FileNotFoundException e){System.out.println("cannot find the file!");System.exit(-1);}try{long num = 0;while((b=in.read())!= -1){System.out.print((char)b);num++;}in.close();System.out.println();System.out.println("一共读取了"+num+"个字符");}catch(IOException e1){System.out.println("文件读取错误!!");System.exit(-1);}}}
结果如图:

运用OutputStream 复制某文件内容到另一文件:

import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class TestOutputStream {public static void main(String args[]){int b = 0;FileInputStream in = null;FileOutputStream out = null;try{in = new FileInputStream("D:\\androidPractice\\J2SE\\src\\com\\why\\TestInputStream.java"); out = new FileOutputStream("D:\\androidPractice\\J2SE\\src\\com\\why\\TestInputStream.txt");while((b = in.read()) != -1){out.write(b);}in.close();out.close();}catch(FileNotFoundException e1){System.out.println("file is not found!");System.exit(-1);}catch(IOException e2){System.out.println("文件复制错误!!");System.exit(-1);}System.out.println("文件复制成功鸟~~!");}}
结果可以看到,代码复制到了TXT文件中:

用filereader读取文件  这时读取的是字符,用inputstream读取的是字节 ,所以那个读取的有一串问号“??????”  这些问号的地方是中文,中文是两个字节,而inputstream

每次读取的是一个字符,所以读不出来完整的字符,显示问号。

用filereader读取文件  示例:

import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;public class TestFileReader {public static void main(String args[]){FileReader fr = null;int n = 0;try{fr = new FileReader("D:\\androidPractice\\J2SE\\src\\com\\why\\TestFileReader.java");while((n = fr.read()) != -1){System.out.print((char)n);  //一定不要忘记类型转换,这里不要换行  不要ln了~~!}fr.close();  //一定不要忘记close !!close,close!!!}catch(FileNotFoundException e1){System.out.println("找不到指定文件!");}catch(IOException e2){System.out.println("文件读取错误!!");}}}
读取结果可以看到,中文也读了出来:


使用Print流  往指定文件 写数据

import java.io.FileOutputStream;import java.io.IOException;import java.io.PrintStream;public class TestPrintStream {public static void main(String args[]){PrintStream ps = null;try{FileOutputStream fos = new FileOutputStream("e:\\a.txt");ps = new PrintStream(fos);}catch(IOException e){e.printStackTrace();}if(ps != null){System.setOut(ps);}int ln = 0;for(char n=0; n<20000; n++){System.out.print(n+" ");if(ln++ >= 100){System.out.println(); ln = 0;}}}}

可以看见E盘生成了a.txt,打开文件,如下图所示:


运用逻辑思维,想象力 想象各种管道。

使用object流:

import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.Serializable;public class TestObjectIO {public static void main(String args[]) throws Exception{T t = new T();t.c = 8;FileOutputStream fos = new FileOutputStream("e:\\a.txt");ObjectOutputStream oos = new ObjectOutputStream(fos);oos.writeObject(t);oos.flush();oos.close();FileInputStream fis = new FileInputStream("e:\\a.txt");ObjectInputStream ois = new ObjectInputStream(fis);T tReaded = (T)ois.readObject();System.out.println("tReaded.a"+tReaded.a+" "+"tReaded.b"+tReaded.b+" "+"tReaded.c"+tReaded.c+" "+"tReaded.d"+tReaded.d+" ");}}class T implements Serializable{int a = 1;int b = 2;int c = 3;double d = 4.5;}
控制台输出:



但是打开E盘的a.txt文件 是乱码。。。


不知道怎么解决。。待续。。

原创粉丝点击