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.
|