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

用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文件 是乱码。。。


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

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值