Java从文件读入以及读出至文件

RT(目前为了比赛使用,并未深究...)

读入1:

try {
//	创建读取字符数据的流对象。 
//	读取路径不正确时会抛 IOException 
//	用一个读取流对象关联一个已存在文件。   
//	用Reader中的read方法读取字符。 
	FileReader reader = new FileReader("in.txt");
	int ch = 0;
	while((ch = reader.read()) != -1) {  
//		System.out.print((char)ch); 
		if(ch == ' ') continue;
		System.out.print(ch-'0' + " ");
	}  
		reader.close();
	} 
	catch (IOException e) {
		e.printStackTrace();
}


读出1:

		try {
	打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
			FileWriter writer = new FileWriter("out.txt", true);
			for(int i = 1; i <= 10; ++i) {
				writer.write(i + " ");
			}
			writer.close();
		} 
		catch (IOException e) {
			e.printStackTrace();
		}


读入2:

File file = new File("in.txt");
Reader reader = null;
try {
	System.out.println("以字符为单位读取文件内容,一次读一个字节:");
	// 一次读一个字符
	reader = new InputStreamReader(new FileInputStream(file));
	int tempchar;
	while ((tempchar = reader.read()) != -1) {
		// 对于windows下,\r\n这两个字符在一起时,表示一个换行。
		// 但如果这两个字符分开显示时,会换两次行。
		// 因此,屏蔽掉\r,或者屏蔽\n。否则,将会多出很多空行。
		if (((char) tempchar) != '\r') {
		// System.out.print((char) tempchar);
			if(tempchar == ' ') continue;
			System.out.print(tempchar-'0');
		}
	}
	reader.close();
}
catch (Exception e) {
	e.printStackTrace();
}


读出2:

        try {
            // 打开一个随机访问文件流,按读写方式
            RandomAccessFile randomFile = new RandomAccessFile("out.txt", "rw");
            // 文件长度,字节数
            long fileLength = randomFile.length();
            //将写文件指针移到文件尾。
            randomFile.seek(fileLength);
            //randomFile.writeBytes("77654321");
            for(int i = 0; i < 10; ++i)
            	randomFile.writeBytes(i+" ");
            randomFile.close();
        } catch (IOException e) {
            e.printStackTrace();
        }


新增:

输出至文件方法:

PrintStream:为其他输出流添加了功能,是他们能够方便的打印各种数据值的表现形式。

1. 永远不会抛出IOException,异常情况可通过checkError()方法测试的内部结构;
2. 可自动刷新。

System.setOut(PrintStream ps):原本 system.out 是系统 standard output stream, 默认是向控制台输出信息,但是通过System.setOut方法就可以将输出定向到其他的文件或者地方。

比如:

PrintStream outputStream = new PrintStream(new File(“a.txt”));

System.setOut(outputStream);

//这时,系统的标志输出流就定向到了a.txt 文件中。

如果还想继续向控制台输出信息,则需要在setOut方法调用之前保存标准的输出流,即:

PrintStream originalOutputStream = System.out;

System.setOut(originalOutputStream);

import java.io.FileNotFoundException;
import java.io.PrintStream;

public class Main1 {  
     public static void main(String[] args) {  
 		try {
	    	 PrintStream original = System.out;
	         PrintStream pStream;
	         pStream = new PrintStream("D:/a.txt");
	         System.setOut(pStream);  
	         pStream.println("12321");  
	         System.setOut(original);  
	         System.out.println("控制台");  
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} 
     }  
} 


继续加油~

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值