Java文件操作(一)按照字节读写

Java文件操作(一)按照字节读写

package com.exe;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
	
	/****************************************读***************************************/	
	//读取一个工程目录下已存在的文件in.txt
		
		try {
			FileInputStream fis=new FileInputStream("in.txt");
			int btmp;
			
			while( (btmp=fis.read() ) !=-1){
				System.out.println(btmp);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
	/****************************************写**************************************/
		// 创建一个工程目录下的out.txt 并向其中写入数据
		
		File file=new File("out.txt");
		try {
			file.createNewFile();
			FileOutputStream fos=new FileOutputStream("out.txt");
			for(int i=0;i<5;i++){
				fos.write(65+i);
			}
		} catch (IOException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
		
		
	}

}

in.txt:


out.txt:



控制台输出:

97    ----------------------a
98    ----------------------b
32    ----------------------空格
99    ----------------------c
100  ----------------------d
32    ----------------------空格
49   ----------------------1
50   ----------------------2
13   ----------------------CR
10   ----------------------LF
51   ----------------------3
52   ----------------------4

后边的是字符对应的ASCII


FileInputStream(File file)
Creates a FileInputStream by opening a connection to an actual file, the file named by the File object file in the file system.
FileInputStream(FileDescriptor fdObj)
Creates a FileInputStream by using the file descriptor fdObj, which represents an existing connection to an actual file in the file system.
FileInputStream(String name)
Creates a FileInputStream by opening a connection to an actual file, the file named by the path name name in the file system.



FileOutputStream(File file)
Creates a file output stream to write to the file represented by the specified File object.
FileOutputStream(File file, boolean append)
Creates a file output stream to write to the file represented by the specified File object.
FileOutputStream(FileDescriptor fdObj)
Creates a file output stream to write to the specified file descriptor, which represents an existing connection to an actual file in the file system.
FileOutputStream(String name)
Creates a file output stream to write to the file with the specified name.
FileOutputStream(String name, boolean append)
Creates a file output stream to write to the file with the specified name.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值